Repository navigation
176 lines (146 loc) · 4.7 KB
/
Copy pathdocker-image-dev.yml
File metadata and controls
176 lines (146 loc) · 4.7 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
name: Build & Push Dev Image
on:
release:
types: [published]
workflow_dispatch:
inputs:
base-tag:
description: Base Tag to be used (`base_tag-type` + `n`)
required: true
default: latest
type:
description: Type of Build
required: true
type: choice
default: dev
options:
- rc
- beta
- alpha
- dev
- feat
env:
TYPES: "rc,beta,alpha,dev,feat"
TYPE: ${{ inputs.type }}
BASE_TAG: ${{ inputs.base-tag }}
COUNT: -1
jobs:
check:
name: Check requirements
runs-on: ubuntu-latest
outputs:
continue: ${{ steps.collect.outputs.continue }}
steps:
- name: Check source
id: collect
env:
EVENT_NAME: ${{ github.event_name }}
REF: ${{ github.ref }}
TARGET: ${{ github.event.release.target_commitish }}
run: |
set -euo pipefail
if [[ "$EVENT_NAME" == "workflow_dispatch" && "$REF" == "refs/heads/main" ]]; then
echo "❌ This workflow cannot be run from the main branch."
exit 1
fi
if [[ "$EVENT_NAME" == "release" && "$TARGET" == "main" ]]; then
echo "❌ Releases targeting main are not allowed."
echo "continue=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "continue=true" >> "$GITHUB_OUTPUT"
echo "Continuing workflow."
resolve-tag:
needs: check
if: needs.check.outputs.continue == 'true'
runs-on: ubuntu-latest
name: Image Tag
outputs:
image_tag: ${{ steps.resolve.outputs.image_tag }}
steps:
- if: github.event_name == 'release'
name: Parse release tag
env:
TAG: ${{ github.ref_name }}
run: |
set -euo pipefail
COUNT="${TAG##*[!0-9]}"
TAG_PART="${TAG%"$COUNT"}"
BASE_TAG="${TAG_PART%-*}"
TYPE="${TAG_PART##*-}"
echo "TYPE=$TYPE" >> "$GITHUB_ENV"
echo "BASE_TAG=$BASE_TAG" >> "$GITHUB_ENV"
echo "COUNT=$COUNT" >> "$GITHUB_ENV"
- name: Login to Registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GH_PCKG_TOKEN }}
- name: Resolve Image Tag
id: resolve
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
REPOSITORY_NAME: ${{ github.event.repository.name }}
run: |
set -euo pipefail
if [ -z "$COUNT" ] || { [ "$COUNT" -lt 0 ] && [ "$COUNT" -ne -1 ]; }; then
echo "❌ Invalid count: $COUNT. May only be > 0."
exit 1
fi
read -ra ALLOWED_TYPES <<< "${TYPES//,/ }"
if [[ ! " ${ALLOWED_TYPES[*]} " =~ " ${TYPE} " ]]; then
echo "❌ Invalid type: $TYPE. Allowed types are $TYPES."
exit 1
fi
PREFIX="${BASE_TAG}-${TYPE}"
IMAGE="${REPOSITORY_OWNER}/${REPOSITORY_NAME}"
TOKEN="$(
curl -fsSL "https://ghcr.io/token?scope=repository:${IMAGE}:pull" |
jq -r '.token'
)"
TAGS=$(curl -fsSL \
-H "Authorization: Bearer ${TOKEN}" \
"https://ghcr.io/v2/${IMAGE}/tags/list" \
| jq -r '.tags[]?')
if [ -z "$COUNT" ] || [ "$COUNT" -eq -1 ]; then
MAX=0
for tag in $TAGS; do
if [[ "$tag" == "${PREFIX}"* ]]; then
NUM="${tag#"$PREFIX"}"
if [[ "$NUM" =~ ^[0-9]+$ ]]; then
(( NUM > MAX )) && MAX=$NUM
fi
fi
done
COUNT=$((MAX + 1))
fi
FINAL_TAG="${PREFIX}${COUNT}"
if echo "$TAGS" | grep -Fqx "$FINAL_TAG"; then
echo "⚠️ Tag $FINAL_TAG already exists."
if [ "${GITHUB_ACTOR}" != "${REPOSITORY_OWNER}" ]; then
echo "🚨 User $GITHUB_ACTOR is not allowed to overwrite existing image."
exit 1
else
echo "User $GITHUB_ACTOR is the owner - allowed to proceed."
fi
fi
echo "Resolved tag: $FINAL_TAG"
echo "image_tag=$FINAL_TAG" >> "$GITHUB_OUTPUT"
update:
needs: resolve-tag
runs-on: ubuntu-latest
name: Development Image
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: codeshelldev/gh-actions/docker-build-push@v1
with:
registry: ghcr.io
token: ${{ secrets.GITHUB_TOKEN }}
flavor: |
latest=false
tags: |
type=raw,value=${{ needs.resolve-tag.outputs.image_tag }}