add check for cache #21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Main | |
on: [push, pull_request] | |
env: | |
should_push_image: |- | |
${{ | |
github.event_name == 'push' && ( | |
startsWith(github.event.ref, 'refs/tags/') | |
|| endsWith(github.event.ref, '/master') | |
) | |
}} | |
build_and_cache: |- | |
build_and_cache() { | |
arch="$1" | |
if ! docker buildx build \ | |
--platform linux/$arch \ | |
--cache-from type=local,src=/home/runner/tmp/$arch \ | |
--cache-to type=local,dest=/home/runner/tmp/$arch \ | |
--tag ${GITHUB_REPOSITORY,,}:$arch \ | |
--file Dockerfile . ; | |
then | |
echo "::error::Docker-Build Error. $arch Cache will be deleted now!" | |
rm -rf /home/runner/tmp/$arch/ | |
fi | |
} | |
check_if_safe_to_use_cache: |- | |
check_if_safe_to_use_cache() { | |
if [ -d "/home/runner/tmp/amd64" ] && [ -d "/home/runner/tmp/arm64" ]; then | |
cache="--cache-from type=local,src=/home/runner/tmp/amd64 --cache-from type=local,src=/home/runner/tmp/arm64 \" | |
else | |
cache="--no-cache" | |
fi | |
} | |
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@master | |
- name: update cache on every commit | |
uses: actions/cache@v3 | |
with: | |
path: /home/runner/tmp | |
key: docker-${{ github.run_id }} | |
restore-keys: | | |
docker- | |
- name: Compute Docker Tag | |
if: fromJSON(env.should_push_image) | |
id: compute_docker_tag | |
run: | | |
tag=${GITHUB_REF#refs/tags/} | |
branch=${GITHUB_REF#refs/heads/} | |
if [ "$tag" != "$GITHUB_REF" ]; then | |
tag=$(echo "$tag" | sed -e 's/[^a-zA-Z0-9\-\.]/-/g') | |
echo ::set-output name=DOCKER_TAG::${tag} | |
elif [ "$branch" != "$GITHUB_REF" ]; then | |
branch=$(echo "$branch" | sed -e 's/[^a-zA-Z0-9\-\.]/-/g') | |
echo ::set-output name=DOCKER_TAG::${branch} | |
else | |
echo "unable to determine docker tag" >&2 | |
exit 1 | |
fi | |
- name: Docker setup env | |
if: fromJSON(env.should_push_image) | |
run: | | |
echo "${{ secrets.DOCKER_HUB_PASSWORD }}" \ | |
| docker login -u "${{ secrets.DOCKER_HUB_USER }}" --password-stdin | |
docker buildx create --use | |
docker buildx inspect --bootstrap | |
- name: Fill AMD64 cache | |
run: | | |
${{ env.build_and_cache }} | |
build_and_cache amd64 | |
- name: Fill ARM64 cache | |
run: | | |
${{ env.build_and_cache }} | |
build_and_cache arm64 | |
- name: Docker build from cache and push | |
if: fromJSON(env.should_push_image) | |
run: | | |
${{ env.check_if_safe_to_use_cache }} | |
check_if_safe_to_use_cache | |
docker buildx build --push \ | |
--platform linux/amd64,linux/arm64 $cache | |
--tag ${GITHUB_REPOSITORY,,}:${{ steps.compute_docker_tag.outputs.DOCKER_TAG }} \ | |
--file Dockerfile . | |
- name: Docker build from cache & push latest | |
if: | | |
github.event_name == 'push' && ( | |
startsWith(github.event.ref, 'refs/tags/') | |
) | |
run: | | |
${{ env.check_if_safe_to_use_cache }} | |
check_if_safe_to_use_cache | |
docker buildx build --push \ | |
--platform linux/amd64,linux/arm64 $cache | |
--tag ${GITHUB_REPOSITORY,,}:latest \ | |
--file Dockerfile . |