github: update docker build workflow to use cache.
Update the Docker build workflow to use premade actions so we can cache the build. https://github.com/docker/setup-buildx-action https://github.com/docker/login-action https://github.com/docker/metadata-action https://github.com/docker/build-push-action
This commit is contained in:
65
.github/workflows/test.yaml
vendored
65
.github/workflows/test.yaml
vendored
@@ -10,35 +10,56 @@ on:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
# Build Docker image and push to Github Packages registry.
|
||||
# Build Docker image and push to Github Container Registry.
|
||||
#
|
||||
# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry
|
||||
# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions
|
||||
# https://docs.github.com/en/actions/guides/publishing-docker-images
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
|
||||
env:
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
IMAGE_ID: ghcr.io/${{ github.repository }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
# https://github.com/docker/setup-buildx-action
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
|
||||
# https://github.com/docker/login-action
|
||||
- name: Login to Github Container Registry
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ github.token }}
|
||||
|
||||
# Generate Docker tags from Git tags.
|
||||
# https://github.com/docker/metadata-action
|
||||
- name: Generate Docker tags
|
||||
uses: docker/metadata-action@v3
|
||||
id: metadata
|
||||
with:
|
||||
images: ghcr.io/${{ github.repository }}
|
||||
tags: |
|
||||
type=sha,format=short,prefix=
|
||||
type=ref,event=branch
|
||||
type=ref,event=tag
|
||||
type=ref,event=pr
|
||||
# Tag `latest` on every commit pushed to master
|
||||
# https://github.com/docker/metadata-action/issues/112
|
||||
flavor: |
|
||||
latest=${{ github.ref == 'refs/heads/master' }}
|
||||
|
||||
# https://github.com/docker/build-push-action
|
||||
- name: Build image
|
||||
run: docker build . --file Dockerfile --tag "$IMAGE_NAME" --label "github_run_id=${{ github.run_id }}"
|
||||
uses: docker/build-push-action@v2
|
||||
with:
|
||||
push: true
|
||||
tags: ${{ steps.metadata.outputs.tags }}
|
||||
labels: ${{ steps.metadata.outputs.labels }}
|
||||
|
||||
- name: Log in to registry
|
||||
run: docker login ghcr.io -u "${{ github.repository_owner }}" -p "${{ github.token }}"
|
||||
|
||||
- name: Push image
|
||||
run: |
|
||||
REF=$(echo ${{ github.sha }} | cut -c1-9)
|
||||
|
||||
docker tag $IMAGE_NAME $IMAGE_ID:$REF
|
||||
docker tag $IMAGE_NAME $IMAGE_ID:latest
|
||||
docker push $IMAGE_ID:$REF
|
||||
docker push $IMAGE_ID:latest
|
||||
# https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#registry-cache
|
||||
# https://dev.to/pst418/speed-up-multi-stage-docker-builds-in-ci-cd-with-buildkit-s-registry-cache-11gi
|
||||
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache
|
||||
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max
|
||||
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
Reference in New Issue
Block a user