diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml index 84d73c21..47c71f4b 100644 --- a/.github/workflows/docker-test.yml +++ b/.github/workflows/docker-test.yml @@ -18,6 +18,7 @@ jobs: env: SOURCE_ROOT: ${{ github.workspace }} ENTRYPOINT: ${{ github.workspace }}/docker/default-entry.sh + DOCKER_CACHE_DEST: ${{ github.workspace }}/.docker/buildx-cache steps: - uses: actions/checkout@v4 with: @@ -29,24 +30,43 @@ jobs: echo "OWNER_LC=$OWNER_LC" >>${GITHUB_ENV} echo "IMAGE=ghcr.io/$OWNER_LC/vs-fltk:build-env" >>${GITHUB_ENV} - - name: Install dependencies - run: sudo apt install -y docker-compose - - name: Check if Dockerfile has changed run: | - FILES=$(git log -1 -p ./ | grep +++ | cut -d '/' -f 2-| sed -e 's|dev/null||g') - if echo "$FILES" | grep -qE "^(Dockerfile)$"; then - echo "changed=true" >> $GITHUB_ENV - else - echo "changed=false" >> $GITHUB_ENV + echo "do_build=false" >> $GITHUB_ENV + if [ "${{ github.ref }}" != "refs/heads/master" ]; then + FILES=$(git diff --name-only HEAD~1 HEAD) + if echo "$FILES" | grep -qE "^docker/Dockerfile$"; then + echo "do_build=true" >> $GITHUB_ENV + fi fi + - name: Cache Docker layers + if: ${{ env.do_build == 'true' }} + id: docker-cache + uses: actions/cache@v3 + with: + path: ${{ env.DOCKER_CACHE_DEST }} + key: ${{ runner.os }}-docker-${{ github.ref_name }} + restore-keys: | + ${{ runner.os }}-docker- + + - name: Set up Docker Buildx + if: ${{ env.do_build == 'true' }} + uses: docker/setup-buildx-action@v3 + - name: Build Docker image - if: ${{ env.changed == 'true' }} - run: docker build -t $IMAGE -f docker/Dockerfile ./docker + if: ${{ env.do_build == 'true' }} + run: | + docker buildx build \ + ./docker \ + -f docker/Dockerfile + -t $IMAGE \ + --cache-to=type=local,dest=${{ env.DOCKER_CACHE_DEST }} \ + --cache-from=type=local,src=${{ env.DOCKER_CACHE_DEST }} \ + --load - name: Run commands in Docker container run: | export HOSTUID=$(id -u) export HOSTGID=$(id -g) - docker-compose -f docker/docker-compose.yml run --rm dev + docker compose -f docker/docker-compose.yml run --rm dev diff --git a/docker/Dockerfile b/docker/Dockerfile index 047b2b61..6bd71503 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -24,8 +24,7 @@ RUN \ sudo \ swiftlang \ unzip \ - wget && \ - update-ca-certificates -f + wget RUN useradd -m builder && passwd -d builder RUN echo "builder ALL=(ALL) ALL" >> /etc/sudoers