-
Notifications
You must be signed in to change notification settings - Fork 5
170 lines (136 loc) · 4.45 KB
/
Copy pathbuild-cmd.yml
File metadata and controls
170 lines (136 loc) · 4.45 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
name: Build & Push Image via Command
on:
workflow_dispatch:
inputs:
number:
required: true
type: string
author:
required: true
type: string
username:
required: true
type: string
args:
required: true
type: string
raw_args:
required: false
type: string
default: ""
origin:
required: true
type: string
env:
BASE_TAG: ""
COUNT: -1
jobs:
resolve-tag:
runs-on: ubuntu-latest
name: Image Tag
outputs:
image_tag: ${{ steps.resolve.outputs.image_tag }}
ref: ${{ steps.parse.outputs.ref }}
steps:
- name: Parse Arguments
id: parse
run: |
set -euo pipefail
ARGS='${{ inputs.args }}'
BRANCH=$(echo "$ARGS" | jq -r '.[0]')
normalize_branch() {
echo "$1" \
| tr '[:upper:]' '[:lower:]' \
| sed -E 's/[^a-z0-9_.-]+/-/g' \
| sed -E 's/-+/-/g' \
| sed -E 's/^-+|-+$//g'
}
if [[ -n "$BRANCH" && "$BRANCH" != "null" ]]; then
REF="refs/heads/$BRANCH"
CUSTOM_NAME=$(echo "$ARGS" | jq -r '.[1]')
if [[ -n "$CUSTOM_NAME" && "$CUSTOM_NAME" != "null" ]]; then
BRANCH="$CUSTOM_NAME"
fi
BRANCH="$(normalize_branch "$BRANCH")"
BASE_TAG="ci-${BRANCH}-"
elif [[ "${{ inputs.origin }}" == "pull_request" ]]; then
REF="refs/pull/${{ inputs.number }}/merge"
BASE_TAG="pr-${{ inputs.number }}-"
elif [[ "${{ inputs.origin }}" == "issue" ]]; then
echo "::error::Missing branch argument at index 0 in command." && exit 1
fi
echo "BASE_TAG=$BASE_TAG" >> $GITHUB_ENV
echo "ref=$REF" >> "$GITHUB_OUTPUT"
- name: Login to Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GH_PCKG_TOKEN }}
- name: Resolve Image Tag
id: resolve
run: |
set -euo pipefail
if [ -z "$COUNT" ] || [ "$COUNT" -lt 0 ] && [ "$COUNT" -ne -1 ]; then
echo "::error::Invalid count: $COUNT. May only be > 0." && exit 1
fi
PREFIX="${{ env.BASE_TAG }}"
IMAGE=${{ github.repository_owner }}/${{ github.event.repository.name }}
TOKEN="$(
curl "https://ghcr.io/token?scope=repository:${IMAGE}:pull" |
awk -F'"' '$0=$4'
)"
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 -qx "$FINAL_TAG"; then
echo "::warn::Tag $FINAL_TAG already exists."
if [ "${GITHUB_ACTOR}" != "${{ github.repository_owner }}" ]; then
echo "::error::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
uses: codeshelldev/gh-actions/.github/workflows/docker-image.yml@main
name: Development Image
with:
registry: ghcr.io
flavor: |
latest=false
tags: |
type=raw,value=${{ needs.resolve-tag.outputs.image_tag }}
ref: ${{ needs.resolve-tag.outputs.ref }}
secrets:
GH_PCKG_TOKEN: ${{ secrets.GH_PCKG_TOKEN }}
output:
needs: [resolve-tag, update]
runs-on: ubuntu-latest
name: Output Image Tag
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Write outputs
run: |
echo "image_tag=${{ needs.resolve-tag.outputs.image_tag }}" > output.txt
- uses: actions/upload-artifact@v4
with:
name: output
path: output.txt