-
Notifications
You must be signed in to change notification settings - Fork 14
249 lines (217 loc) · 9.69 KB
/
scheduled.yml
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
name: Scheduled
on:
schedule:
- cron: '45 * * * *'
workflow_dispatch:
concurrency:
group: "scheduled"
cancel-in-progress: true
jobs:
check:
runs-on: ubuntu-latest
outputs:
configs: ${{ steps.repo_check.outputs.configs }}
steps:
- uses: actions/checkout@v4
- uses: mikefarah/yq@v4.35.1
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- id: repo_check
env:
CONCURRENT_IMAGE_PULL: ${{ vars.CONCURRENT_IMAGE_PULL }}
run: |
# This script reads config.yaml, platforms.yaml and runners.yaml to generate a list of configurations to build and deploy
# It will;
# - check the source respository for the latest commit hash
# - check if the built image exists in our dockerhub registry
# - generate a list of configurations to build and deploy
#
# Here is an example of generate output;
# [{
# "source": {
# "repository": "hyperledger/besu",
# "ref": "main"
# },
# "build_script": "./besu/build.sh",
# "target": {
# "tag": "main",
# "repository": "ethpandaops/besu"
# },
# "platforms": [
# {
# "platform": "linux/amd64",
# "runner": "ubuntu-latest",
# "slug": "linux-amd64"
# },
# {
# "platform": "linux/arm64",
# "runner": "ARM64",
# "slug": "linux-arm64"
# }
# ]
# },{
# "source": {
# "repository": "sigp/lighthouse",
# "ref": "unstable"
# },
# "target": {
# "tag": "unstable",
# "repository": "ethpandaops/lighthouse",
# "dockerfile": "./lighthouse/Dockerfile"
# },
# "platforms": [
# {
# "platform": "linux/amd64",
# "runner": "ubuntu-latest",
# "slug": "linux-amd64"
# },
# {
# "platform": "linux/arm64",
# "runner": "ARM64",
# "slug": "linux-arm64"
# }
# ]
# }]
CONFIG_FILE="config.yaml"
PLATFORMS_FILE="platforms.yaml"
RUNNERS_FILE="runners.yaml"
# Create a temporary directory for storing intermediate results
TEMP_DIR=$(mktemp -d)
# Ensure the temporary directory is removed when the script exits
trap "rm -rf $TEMP_DIR" EXIT
process_commits() {
local LINE=$1
local SOURCE_REPOSITORY=$2
local SOURCE_REF=$3
local TARGET_REPOSITORY=$4
local TARGET_TAG=$5
local CLIENT="${TARGET_REPOSITORY#*/}"
local RESPONSE=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${SOURCE_REPOSITORY}/commits/${SOURCE_REF}?per_page=1")
local COMMIT_HASH=$(echo "$RESPONSE" | jq -r '.sha' | cut -c1-7)
if [[ -z "$COMMIT_HASH" || "$COMMIT_HASH" == "null" ]]; then
# Log error but don't exit; just skip this configuration
echo "[LINE:$LINE] Error fetching commit hash for ${SOURCE_REPOSITORY}#${SOURCE_REF}, skipping."
return
fi
local configOutput="${TEMP_DIR}/${LINE}_commits.json"
touch $configOutput
echo "{\"line\": \"$LINE\", \"commit_hash\": \"$COMMIT_HASH\"}," >> $configOutput
}
process_image() {
local LINE=$1
local IMAGE=$2
local URL=$3
local imageOutput="${TEMP_DIR}/${LINE}_image.json"
touch $imageOutput
local exists=$(curl -s $URL | jq '.results | length > 0')
# check if exists == true
if [ "$exists" == "true" ]; then
exists=true
else
exists=false
fi
echo "{\"line\": \"$LINE\", \"image\": \"$IMAGE\", \"exists\": $exists}" >> $imageOutput
}
# Get commit hashes for each configuration in parallel
while IFS=$'\t' read -r LINE SOURCE_REPOSITORY SOURCE_REF TARGET_REPOSITORY TARGET_TAG; do
process_commits "$LINE" "$SOURCE_REPOSITORY" "$SOURCE_REF" "$TARGET_REPOSITORY" "$TARGET_TAG" &
done < <(yq -r 'to_entries | map_values({"value":.value, "index":.key}) | .[] | [.index, .value.source.repository, .value.source.ref, .value.target.repository, .value.target.tag] | @tsv' "$CONFIG_FILE")
wait
# Initialize JSON arrays
COMMITS="["
# Concatenate results, ensuring files exist before attempting to read
for file in $TEMP_DIR/*_commits.json; do
if [ -f "$file" ]; then
COMMITS+=$(cat "$file")
fi
done
# Remove trailing commas and close JSON arrays
COMMITS="${COMMITS%,}]"
echo "Checking if images exist in dockerhub..."
while IFS=$'\t' read -r LINE SOURCE_REPOSITORY SOURCE_REF TARGET_REPOSITORY TARGET_TAG; do
# get the image commit hash from LINE
COMMIT_HASH=$(echo "$COMMITS" | jq -r --arg LINE "$LINE" '.[] | select(.line == $LINE) | .commit_hash')
IMAGE_TAG="${TARGET_TAG}-${COMMIT_HASH}"
IMAGE="${TARGET_REPOSITORY}:${IMAGE_TAG}"
URL="https://hub.docker.com/v2/repositories/${TARGET_REPOSITORY}/tags?page_size=25&page=1&ordering=&name=${IMAGE_TAG}"
process_image $LINE $IMAGE $URL &
done < <(yq -r 'to_entries | map_values({"value":.value, "index":.key}) | .[] | [.index, .value.source.repository, .value.source.ref, .value.target.repository, .value.target.tag] | @tsv' "$CONFIG_FILE")
wait
declare -A images
# Concatenate results, ensuring files exist before attempting to read
for file in $TEMP_DIR/*_image.json; do
if [ -f "$file" ]; then
LINE=$(cat "$file" | jq -r '.line')
IMAGE=$(cat "$file" | jq -r '.image')
EXISTS=$(cat "$file" | jq -r '.exists')
images[$IMAGE]=$EXISTS
fi
done
CONFIGS="configs=["
echo "Generating configuration files..."
while IFS=$'\t' read -r LINE SOURCE_REPOSITORY SOURCE_REF TARGET_REPOSITORY TARGET_TAG; do
# get the image commit hash from LINE
COMMIT_HASH=$(echo "$COMMITS" | jq -r --arg LINE "$LINE" '.[] | select(.line == $LINE) | .commit_hash')
IMAGE_TAG="${TARGET_TAG}-${COMMIT_HASH}"
IMAGE="${TARGET_REPOSITORY}:${IMAGE_TAG}"
CLIENT="${TARGET_REPOSITORY#*/}"
if [ "${images[$IMAGE]}" == "false" ]; then
# Handle platforms and runners, ensuring output files are created even if empty
platforms=$(yq e ".$CLIENT[]" "$PLATFORMS_FILE")
platformsArr=""
for platform in $platforms; do
runner=$(yq e ".\"$platform\"" "$RUNNERS_FILE")
slug=$(echo "$platform" | tr '/' '-')
platformsArr+="{\\\"platform\\\": \\\"$platform\\\", \\\"runner\\\": \\\"$runner\\\", \\\"slug\\\": \\\"$slug\\\"},"
done
platformsArr="${platformsArr%,}"
# convert to string
platformsOutput="{\"platforms\": \"[$platformsArr]\"}"
CONFIGS+=$(echo "$(yq -r -o=json ".[${LINE}]" "$CONFIG_FILE" | jq --argjson plat "$platformsOutput" '. + $plat'),")
fi
done < <(yq -r 'to_entries | map_values({"value":.value, "index":.key}) | .[] | [.index, .value.source.repository, .value.source.ref, .value.target.repository, .value.target.tag] | @tsv' "$CONFIG_FILE")
# Remove trailing commas and close JSON arrays
CONFIGS="${CONFIGS%,}]"
echo "CONFIGS: $CONFIGS"
echo $CONFIGS >> $GITHUB_OUTPUT
deploy:
needs: check
if: ${{ needs.check.outputs.configs != '[]' && needs.check.outputs.configs != '' }}
uses: ./.github/workflows/deploy.yml
strategy:
fail-fast: false
matrix:
config: ${{fromJson(needs.check.outputs.configs)}}
name: ${{ matrix.config.source.repository }}#${{ matrix.config.source.ref }} ${{ matrix.config.target.tag }}
with:
source_repository: ${{ matrix.config.source.repository }}
source_ref: ${{ matrix.config.source.ref }}
build_script: ${{ matrix.config.build_script }}
build_args: "${{ matrix.config.build_args }}"
target_tag: ${{ matrix.config.target.tag }}
target_repository: ${{ matrix.config.target.repository }}
target_dockerfile: ${{ matrix.config.target.dockerfile }}
platforms: ${{ matrix.config.platforms }}
DOCKER_USERNAME: "${{ vars.DOCKER_USERNAME }}"
GOPROXY: "${{ vars.GOPROXY }}"
secrets:
DOCKER_PASSWORD: "${{ secrets.DOCKER_PASSWORD }}"
MACOS_PASSWORD: "${{ secrets.MACOS_PASSWORD }}"
notify:
name: Discord Notification
runs-on: ubuntu-latest
needs:
- check
- deploy
if: cancelled() || failure()
steps:
- name: Notify
uses: nobrayner/discord-webhook@v1
with:
github-token: ${{ secrets.github_token }}
discord-webhook: ${{ secrets.DISCORD_WEBHOOK }}