-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
212 lines (169 loc) · 5.92 KB
/
Copy pathaction.yml
File metadata and controls
212 lines (169 loc) · 5.92 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: Docker Publish Action Tool
description: Build and publish Docker images to Docker Hub
author: Sriman
branding:
icon: upload-cloud
color: blue
inputs:
context_path:
description: Build context path
required: false
default: "."
docker_username:
description: Docker Hub username
required: true
docker_password:
description: Docker Hub password or token
required: true
dockerfile_path:
description: Path to the Dockerfile
required: false
default: "./Dockerfile"
image_repo:
description: Repository for the Docker image (e.g. username/repo)
required: true
version:
description: Application version (e.g. 1.2.3, 1.2.3-beta.2, 1.2.3-34)
required: true
dry_run:
description: Build only, do not push images
required: false
default: "false"
summary:
description: Generate job summary
required: false
default: "true"
runs:
using: composite
steps:
- name: Validate inputs and prepare environment
shell: bash
run: |
set -eu
echo "::group::Docker authentication"
if [[ ! "${{ inputs.image_repo }}" =~ .+/.+ ]]; then
echo "::error::image_repo must be in the format <namespace>/<repository>"
exit 1
fi
if [[ "${{ inputs.dry_run }}" == "true" ]]; then
echo "::warning::Dry-run mode enabled, images will NOT be pushed"
fi
echo "::notice::Using GitHub Actions cache for Docker build layers"
echo "::notice::Logging in to Docker Hub"
echo "::endgroup::"
- name: Log in to Docker Hub
uses: docker/login-action@af1e73f918a031802d376d3c8bbc3fe56130a9b0 # v4
with:
username: ${{ inputs.docker_username }}
password: ${{ inputs.docker_password }}
- name: Log QEMU setup
shell: bash
run: echo "::notice::Setting up QEMU for multi-architecture builds"
- name: Set up QEMU
uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4
- name: Log Buildx setup
shell: bash
run: echo "::notice::Setting up Docker Buildx builder"
- name: Set up Buildx
uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4
- name: Parse version
id: version
shell: bash
run: |
set -eu
echo "::group::Version parsing"
RAW_VERSION="${{ inputs.version }}"
VERSION="${RAW_VERSION#v}"
echo "::notice::Raw version: $RAW_VERSION"
echo "::notice::Normalized version: $VERSION"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
if [[ "$VERSION" == *"-"* ]]; then
PRERELEASE="${VERSION#*-}"
echo "prerelease=true" >> "$GITHUB_OUTPUT"
if [[ "$PRERELEASE" =~ ^[0-9]+$ ]]; then
echo "kind=numeric" >> "$GITHUB_OUTPUT"
echo "::notice::Numeric prerelease detected → tag: next"
else
LABEL="${PRERELEASE%%.*}"
echo "kind=labeled" >> "$GITHUB_OUTPUT"
echo "label=$LABEL" >> "$GITHUB_OUTPUT"
echo "::notice::Labeled prerelease detected → tag: $LABEL"
fi
else
echo "prerelease=false" >> "$GITHUB_OUTPUT"
echo "kind=stable" >> "$GITHUB_OUTPUT"
echo "major=${VERSION%%.*}" >> "$GITHUB_OUTPUT"
echo "::notice::Stable release detected"
fi
echo "::endgroup::"
- name: Generate Docker tags
id: docker_tags
shell: bash
run: |
set -eu
echo "::group::Docker tag generation"
IMAGE="${{ inputs.image_repo }}"
VERSION="${{ steps.version.outputs.version }}"
KIND="${{ steps.version.outputs.kind }}"
echo "$IMAGE:$VERSION" > tags.txt
if [[ "$KIND" == "stable" ]]; then
echo "$IMAGE:latest" >> tags.txt
echo "$IMAGE:${{ steps.version.outputs.major }}" >> tags.txt
elif [[ "$KIND" == "numeric" ]]; then
echo "$IMAGE:next" >> tags.txt
elif [[ "$KIND" == "labeled" ]]; then
echo "$IMAGE:${{ steps.version.outputs.label }}" >> tags.txt
fi
echo "::notice::Generated Docker tags"
cat tags.txt
{
echo "tags<<EOF"
cat tags.txt
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "::endgroup::"
- name: Validate tag strategy (fail fast)
shell: bash
run: |
set -eu
echo "::group::Fail-fast validation"
KIND="${{ steps.version.outputs.kind }}"
if [[ "$KIND" != "stable" ]] && grep -q ":latest" tags.txt; then
echo "::error::Invalid tag strategy: prereleases must NOT publish 'latest'"
exit 1
fi
if [[ ! -s tags.txt ]]; then
echo "::error::No Docker tags were generated"
exit 1
fi
echo "::notice::Tag strategy validated"
echo "::endgroup::"
- name: Build and push Docker image
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7
with:
context: ${{ inputs.context_path }}
file: ${{ inputs.dockerfile_path }}
push: ${{ inputs.dry_run != 'true' }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Job summary
if: inputs.summary == 'true'
shell: bash
run: |
{
echo "## Docker Publish Summary"
echo ""
echo "**Image:** \`${{ inputs.image_repo }}\`"
echo "**Version:** \`${{ steps.version.outputs.version }}\`"
echo "**Strategy:** \`${{ steps.version.outputs.kind }}\`"
echo "**Mode:** $([[ '${{ inputs.dry_run }}' == 'true' ]] && echo 'dry-run' || echo 'publish')"
echo ""
echo "### Tags"
sed 's/^/- `/' tags.txt | sed 's/$/`/'
echo ""
echo "### Platforms"
echo "- linux/amd64"
echo "- linux/arm64"
} >> "$GITHUB_STEP_SUMMARY"