Skip to content

Commit 554f309

Browse files
Correct syntax in docker.yaml workflow (#159)
1 parent a54194c commit 554f309

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

.github/workflows/docker.yaml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ on:
77
tags: ["v*"]
88
branches: [ main ]
99
workflow_dispatch:
10-
# Run on pull requests to test docker build.
11-
# We explicitly do not push on pull requests (the job below is ended early).
10+
# Run on pull requests so that the docker build is tested.
11+
#
12+
# We explicitly do not push on pull requests (see `if` conditionals and `push`
13+
# attribute of `docker/build-push-action` below). This prevents us from filling
14+
# up the container registries with in-progress builds.
1215
#
1316
# note: secrets will not be populated on pull requests from external authors.
1417
pull_request:
@@ -38,32 +41,35 @@ jobs:
3841
type=sha,prefix=,format=long
3942
type=semver,pattern=v{{version}}
4043
type=semver,pattern=v{{major}}.{{minor}}
41-
42-
- name: Don't push to registry if this is a PR
43-
if: "${{ github.event_name }}" = "pull_request"
44-
run: |
45-
echo "Not pushing the image to any container registry as this workflow is running on a pull request"
46-
exit 0
4744
4845
- name: Log in to DockerHub
46+
if: github.event_name != 'pull_request'
4947
uses: docker/login-action@v2
5048
with:
5149
username: ${{ secrets.DOCKER_HUB_USERNAME }}
5250
password: ${{ secrets.DOCKER_HUB_TOKEN }}
5351

5452
- name: Log in to GHCR
53+
if: github.event_name != 'pull_request'
5554
uses: docker/login-action@v2
5655
with:
5756
registry: ghcr.io
5857
username: ${{ github.repository_owner }}
5958
password: ${{ secrets.GITHUB_TOKEN }}
6059

6160
- name: Build and push all platforms
62-
uses: docker/build-push-action@v4
61+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
6362
with:
64-
push: true
63+
# Always build, but don't push to container registries on PRs.
64+
push: ${{ github.event_name != 'pull_request' }}
6565
labels: "gitsha1=${{ github.sha }}"
6666
tags: "${{ steps.set-tag.outputs.tags }}"
6767
platforms: linux/amd64,linux/arm64
68-
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache
69-
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max
68+
# Cache to GitHub Actions backend (which is evicted after 7 days).
69+
# This doesn't require authentication (unlike a registry), so works
70+
# well with external PRs.
71+
#
72+
# This backend does prevent workflows on other repos from accessing it,
73+
# but that is not necessary right now.
74+
cache-from: type=gha
75+
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)