1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This workflow builds and pushes a Docker image when a semver tag is pushed.
name: Docker Publish
on:
push:
tags:
- 'v*.*.*' # Trigger on semantic version tags (e.g., v1.0.0)
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
# Permissions needed to push to GHCR. May not be needed for Docker Hub depending on setup.
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
with:
install: true # Ensure buildx is installed
# --- Log in to GitHub Container Registry (GHCR) ---
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
# Replace with your image name on Docker Hub or GHCR
# Example GHCR: ghcr.io/your-org/mail-laser
images: |
ghcr.io/${{ github.repository_owner }}/mail-laser
tags: |
# Create tags based on the semver git tag
type=semver,pattern={{version}} # e.g., v1.2.3 -> 1.2.3
type=semver,pattern={{major}}.{{minor}} # e.g., v1.2.3 -> 1.2
type=semver,pattern={{major}} # e.g., v1.2.3 -> 1
# Add 'latest' tag only for semver tags
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
- name: Build and push Docker image
id: build-push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Enable GitHub Actions cache for Docker layers
cache-from: type=gha
cache-to: type=gha,mode=max
# Ensure platform is set for potentially cross-platform builds if needed later
# platforms: linux/amd64 # Add other platforms like linux/arm64 if required