From 132dfea5ac8e14c71957d772f4e9b754625d7d28 Mon Sep 17 00:00:00 2001 From: Federico Infanti Date: Tue, 17 Oct 2023 09:58:40 +0200 Subject: [PATCH] ARCH-130 try new cache approach --- .github/workflows/test-suite-buildx.yaml | 52 +++++++++++++++++++ .../{test-suite.yaml => test-suite.yaml_back} | 0 build-with-cache/action.yaml | 46 ++++++++++++++++ scripts/buildx.sh | 43 +++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 .github/workflows/test-suite-buildx.yaml rename .github/workflows/{test-suite.yaml => test-suite.yaml_back} (100%) create mode 100644 build-with-cache/action.yaml create mode 100755 scripts/buildx.sh diff --git a/.github/workflows/test-suite-buildx.yaml b/.github/workflows/test-suite-buildx.yaml new file mode 100644 index 0000000..fdeb67c --- /dev/null +++ b/.github/workflows/test-suite-buildx.yaml @@ -0,0 +1,52 @@ +name: Test all actions +on: [push] +jobs: + test-suite: + runs-on: ubuntu-latest + environment: testing + steps: + - name: Checkout + uses: actions/checkout@v3 + +# - name: Initialize cache +# uses: ./initialize-cache +# with: +# github_token: ${{ secrets.GH_TOKEN }} +# +# - name: Wipe images +# uses: ./wipe-workspace + + - name: Test build local image + uses: ./build-with-cache + with: + image_name: github-actions-test + build_context: https://github.com/mendhak/docker-http-https-echo.git + + - name: List images & containers + run: docker image ls && docker ps -a + + - name: Test non-e2e tests + uses: ./run-tests + with: + image_name: github-actions-test + image_tag: latest + static: echo 'test static' + unit: echo 'test unit' + functional: echo 'test functional' + + - name: Show image exposed ports + run: docker inspect --format='{{json .Config.ExposedPorts}}' "github-actions-test:latest" | jq 'keys[0]' | cut -d'/' -f1 | cut -d'"' -f2 + shell: bash + +# - name: Test e2e test +# uses: ./run-tests +# with: +# image_name: github-actions-test +# e2e: echo 'e2e test' + + - name: Test push to Humanitec + uses: ./humanitec-push-image + with: + image_name: github-actions-test + humanitec_token: ${{ secrets.HUMANITEC_TOKEN }} + humanitec_org: checkout-charlie diff --git a/.github/workflows/test-suite.yaml b/.github/workflows/test-suite.yaml_back similarity index 100% rename from .github/workflows/test-suite.yaml rename to .github/workflows/test-suite.yaml_back diff --git a/build-with-cache/action.yaml b/build-with-cache/action.yaml new file mode 100644 index 0000000..89df7bf --- /dev/null +++ b/build-with-cache/action.yaml @@ -0,0 +1,46 @@ +name: 'Build container image' +description: 'Build Docker image with docker cache' +inputs: + build_args: + description: 'Arguments passed to `docker build`' + required: false + default: '' + build_context: + description: 'Docker build context ' + required: false + default: '.' + dockerfile: + description: 'Dockerfile location' + required: false + default: 'Dockerfile' + image_name: + description: 'Image name. Defaults to the repository name. Set it if you build multiple images in the same repository' + required: false + default: ${{ github.event.repository.name }} + stage: + description: 'Docker stage in a multi-stage build' + required: false + is_testing: + description: 'Is this a testing image?' + required: false + default: "false" + +runs: + using: "composite" + steps: + - name: Set up builder + uses: docker/setup-buildx-action@v3 + - name: Load cache + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache-${{ inputs.image_name }} + key: ${{ inputs.image_name }}-buildx + restore-keys: | + ${{ inputs.image_name }}-buildx- + - name: Building ${{ inputs.stage }} ${{ inputs.image_name }} + run: ${{ github.action_path }}/../scripts/buildx.sh "${{ inputs.build_context }}" "${{ inputs.dockerfile }}" "${{ inputs.build_args }}" "${{ inputs.image_name }}" "${{ inputs.stage }}" "${{ inputs.is_testing }}" + shell: sh + name: Clear previous cache + run: | + rm -rf /tmp/.buildx-cache-${{ inputs.image_name }} + mv /tmp/.buildx-cache-new /tmp/.buildx-cache-${{ inputs.image_name }} diff --git a/scripts/buildx.sh b/scripts/buildx.sh new file mode 100755 index 0000000..0f15074 --- /dev/null +++ b/scripts/buildx.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +set -e + +BUILD_CONTEXT="$1" +DOCKERFILE="$2" +BUILD_ARGS="$3" +IMAGE_NAME="$4" +STAGE="$5" +IS_TESTING="$6" + +echo "Image before build:" +docker images + + +if [ -z "$STAGE" ]; then + STAGE_PART="" +else + STAGE_PART="--target $STAGE" +fi + +# check if is_testing + +if [ "$IS_TESTING" = "true" ] || [ "$STAGE" = "testing" ]; then + IMAGE_TAG="testing" +else + IMAGE_TAG="latest" +fi + +echo "Building image..." +docker buildx build $BUILD_ARGS --file "$DOCKERFILE" $STAGE_PART -t "$IMAGE_NAME:$IMAGE_TAG" "$BUILD_CONTEXT" --cache-from "type=local,src=/tmp/.buildx-cache-$IMAGE_NAME" --cache-to "type=local,dest=/tmp/.buildx-cache-new-$IMAGE_NAME,mode=max"|| exit 1 + +echo "Post-build image list:" +docker images +echo "Cleanup unused Docker resources.." +# Remove unused resources that would otherwise persist between builds by virtue of GH dependency caching +docker rmi -f $(docker images | awk '$2 != "latest" && $2 != "testing" {print $3}') >/dev/null 2>&1 || exit 0 +docker system prune -f +echo "Image list after cleanup:" +docker images +echo "List of containers:" +docker ps -a +echo "Done."