Skip to content

Commit 3b9abfc

Browse files
committed
Use docker publish action from starter workflows repo
1 parent 88eb56b commit 3b9abfc

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

.github/workflows/docker-build-and-publish.yml

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,49 @@ on:
44
push:
55
branches:
66
- main
7+
env:
8+
# Use docker.io for Docker Hub if empty
9+
REGISTRY: ghcr.io
10+
# github.repository as <account>/<repo>
11+
IMAGE_NAME: ${{ github.repository }}
12+
713

814
jobs:
9-
build-and-publish:
15+
build:
16+
1017
runs-on: ubuntu-latest
18+
permissions:
19+
contents: read
20+
packages: write
21+
1122
steps:
12-
- name: Checkout code
13-
uses: actions/checkout@v3
23+
- name: Checkout repository
24+
uses: actions/checkout@v2
1425

15-
- name: Login to GitHub Container Registry
16-
uses: docker/login-action@v3
26+
# Login against a Docker registry except on PR
27+
# https://github.com/docker/login-action
28+
- name: Log into registry ${{ env.REGISTRY }}
29+
if: github.event_name != 'pull_request'
30+
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
1731
with:
18-
registry: ghcr.io
32+
registry: ${{ env.REGISTRY }}
1933
username: ${{ github.actor }}
2034
password: ${{ secrets.GITHUB_TOKEN }}
2135

22-
- name: Build and Push Docker Image
23-
uses: docker/build-push-action@v3
36+
# Extract metadata (tags, labels) for Docker
37+
# https://github.com/docker/metadata-action
38+
- name: Extract Docker metadata
39+
id: meta
40+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
41+
with:
42+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43+
44+
# Build and push Docker image with Buildx (don't push on PR)
45+
# https://github.com/docker/build-push-action
46+
- name: Build and push Docker image
47+
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
2448
with:
2549
context: .
26-
push: true
27-
tags: ghcr.io/${{ github.repository }}:latest
28-
labels: |
29-
com.github.repository=${{ github.repository }}
30-
com.github.sha=${{ github.sha }}
31-
32-
- name: Image Details
33-
run: |
34-
echo "Image URL: ghcr.io/${{ github.repository }}:latest"
35-
echo "Repository: $GITHUB_REPOSITORY"
36-
echo "SHA: $GITHUB_SHA"
50+
push: ${{ github.event_name != 'pull_request' }}
51+
tags: ${{ steps.meta.outputs.tags }}
52+
labels: ${{ steps.meta.outputs.labels }}

0 commit comments

Comments
 (0)