Skip to content

Commit 1a73a3c

Browse files
committed
feat: add github workflow for build and publish docker image
1 parent 56b1bec commit 1a73a3c

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed
Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,85 @@
1-
name: Docker Publish
1+
# .github/workflows/docker-publish.yml
2+
name: Docker Build and Publish
23

34
on:
45
push:
5-
branches: ["main"]
6-
# Publish semver tags as releases.
7-
tags: ["v*.*.*"]
6+
branches:
7+
- main
8+
- develop
9+
- "feature/**"
10+
- "hotfix/**"
11+
tags:
12+
- "v*.*.*"
13+
pull_request:
14+
branches:
15+
- main
16+
- develop
817

918
env:
10-
# Use docker.io for Docker Hub if empty
1119
REGISTRY: ghcr.io
12-
# github.repository as <account>/<repo>
1320
IMAGE_NAME: ${{ github.repository }}
1421

1522
jobs:
16-
build:
23+
lint:
24+
name: Lint Dockerfile
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout code
28+
uses: actions/checkout@v4
29+
30+
- name: Lint Dockerfile
31+
uses: hadolint/[email protected]
32+
with:
33+
dockerfile: Dockerfile
34+
35+
build-and-push:
36+
name: Build and Push Docker image
37+
needs: lint
1738
runs-on: ubuntu-latest
1839
permissions:
1940
contents: read
2041
packages: write
2142

2243
steps:
23-
- name: Checkout repository
24-
uses: actions/checkout@v3
44+
- name: Checkout code
45+
uses: actions/checkout@v4
2546

26-
# Install the cosign tool except on PR
27-
28-
# Set up BuildKit Docker container builder to be able to build
29-
# multi-platform images and export cache
30-
# https://github.com/docker/setup-buildx-action
3147
- name: Set up Docker Buildx
32-
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
48+
uses: docker/setup-buildx-action@v3
3349

34-
# Login against a Docker registry except on PR
35-
# https://github.com/docker/login-action
36-
- name: Log into registry ${{ env.REGISTRY }}
37-
if: github.event_name != 'pull_request'
38-
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
50+
- name: Log in to GitHub Container Registry
51+
uses: docker/login-action@v3
3952
with:
4053
registry: ${{ env.REGISTRY }}
4154
username: ${{ github.actor }}
4255
password: ${{ secrets.GITHUB_TOKEN }}
4356

44-
# Extract metadata (tags, labels) for Docker
45-
# https://github.com/docker/metadata-action
46-
- name: Extract Docker metadata
57+
- name: Extract metadata for Docker
4758
id: meta
48-
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
59+
uses: docker/metadata-action@v5
4960
with:
5061
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
62+
tags: |
63+
type=sha,format=long
64+
type=ref,event=branch
65+
type=ref,event=pr
66+
type=semver,pattern={{version}}
67+
type=semver,pattern={{major}}.{{minor}}
68+
type=raw,value=latest,enable={{is_default_branch}}
69+
type=raw,value=dev,enable=${{ github.ref == 'refs/heads/develop' }}
5170
52-
# Build and push Docker image with Buildx (don't push on PR)
53-
# https://github.com/docker/build-push-action
5471
- name: Build and push Docker image
55-
id: build-and-push
56-
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
72+
uses: docker/build-push-action@v5
5773
with:
5874
context: .
59-
platforms: linux/amd64,linux/arm64
6075
push: ${{ github.event_name != 'pull_request' }}
6176
tags: ${{ steps.meta.outputs.tags }}
6277
labels: ${{ steps.meta.outputs.labels }}
6378
cache-from: type=gha
6479
cache-to: type=gha,mode=max
80+
81+
- name: Print image tags
82+
if: github.event_name != 'pull_request'
83+
run: |
84+
echo "Published tags:"
85+
echo "${{ steps.meta.outputs.tags }}" | tr ' ' '\n'

0 commit comments

Comments
 (0)