Skip to content

Commit

Permalink
ARCH-130 try new cache approach
Browse files Browse the repository at this point in the history
  • Loading branch information
finfa committed Oct 17, 2023
1 parent 8a8975a commit 132dfea
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/test-suite-buildx.yaml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
46 changes: 46 additions & 0 deletions build-with-cache/action.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
43 changes: 43 additions & 0 deletions scripts/buildx.sh
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 132dfea

Please sign in to comment.