-
Notifications
You must be signed in to change notification settings - Fork 1.7k
109 lines (94 loc) · 4.19 KB
/
build-and-publish-image.yml
File metadata and controls
109 lines (94 loc) · 4.19 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: 'Build and Publish Docker Image'
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Docker image version/tag (e.g., 0.9.1, 0.9.2-rc.1)'
type: 'string'
required: false
publish:
description: 'Publish to GHCR'
type: 'boolean'
default: false
env:
REGISTRY: 'ghcr.io'
IMAGE_NAME: '${{ github.repository }}'
jobs:
build-and-push-to-ghcr:
runs-on: 'ubuntu-latest'
permissions:
contents: 'read'
packages: 'write'
steps:
- name: 'Checkout repository'
uses: 'actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8' # ratchet:actions/checkout@v5
with:
ref: '${{ github.ref }}'
- name: 'Process version'
id: 'version'
run: |
INPUT_VERSION="${{ github.event.inputs.version }}"
# For tag pushes, extract version from the tag
if [[ -z "$INPUT_VERSION" && "${{ github.ref_type }}" == "tag" ]]; then
INPUT_VERSION="${{ github.ref_name }}"
fi
# Strip 'v' prefix if present
CLEAN_VERSION="${INPUT_VERSION#v}"
# Extract major.minor for floating tag (e.g., 1.0.0 -> 1.0)
MAJOR_MINOR=$(echo "$CLEAN_VERSION" | grep -oE '^[0-9]+\.[0-9]+' || true)
echo "raw=${INPUT_VERSION}" >> "$GITHUB_OUTPUT"
echo "clean=${CLEAN_VERSION}" >> "$GITHUB_OUTPUT"
echo "major_minor=${MAJOR_MINOR}" >> "$GITHUB_OUTPUT"
echo "Input version: ${INPUT_VERSION}"
echo "Clean version: ${CLEAN_VERSION}"
echo "Major.minor: ${MAJOR_MINOR}"
- name: 'Debug inputs'
if: |-
${{ runner.debug == '1' }}
run: |
echo "Event name: ${{ github.event_name }}"
echo "Version input (raw): ${{ steps.version.outputs.raw }}"
echo "Version (clean): ${{ steps.version.outputs.clean }}"
echo "Major.minor: ${{ steps.version.outputs.major_minor }}"
echo "Publish input: ${{ github.event.inputs.publish }}"
echo "GitHub ref: ${{ github.ref }}"
- name: 'Set up QEMU'
uses: 'docker/setup-qemu-action@v3' # ratchet:exclude
- name: 'Set up Docker Buildx'
uses: 'docker/setup-buildx-action@v3' # ratchet:exclude
- name: 'Extract metadata (tags, labels) for Docker'
id: 'meta'
uses: 'docker/metadata-action@v5' # ratchet:exclude
with:
images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}'
tags: |
type=raw,value=${{ steps.version.outputs.clean }},enable=${{ steps.version.outputs.clean != '' }}
type=raw,value=${{ steps.version.outputs.major_minor }},enable=${{ steps.version.outputs.major_minor != '' }}
type=ref,event=branch,enable=${{ steps.version.outputs.clean == '' }}
type=ref,event=pr,enable=${{ steps.version.outputs.clean == '' }}
type=semver,pattern={{version}},enable=${{ steps.version.outputs.clean == '' }}
type=semver,pattern={{major}}.{{minor}},enable=${{ steps.version.outputs.clean == '' }}
type=sha,prefix=sha-,format=short,enable=${{ steps.version.outputs.clean == '' }}
- name: 'Log in to the Container registry'
if: |-
${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }}
uses: 'docker/login-action@v3' # ratchet:exclude
with:
registry: '${{ env.REGISTRY }}'
username: '${{ github.actor }}'
password: '${{ secrets.GITHUB_TOKEN }}'
- name: 'Build and push Docker image'
id: 'build-and-push'
uses: 'docker/build-push-action@v6' # ratchet:exclude
with:
context: '.'
platforms: 'linux/amd64,linux/arm64'
push: |-
${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }}
tags: '${{ steps.meta.outputs.tags }}'
labels: '${{ steps.meta.outputs.labels }}'
build-args: |
CLI_VERSION_ARG=${{ steps.version.outputs.clean || github.sha }}