Skip to content

Commit 34f6afe

Browse files
authored
Add parser update workflow (#3222)
* Add parser update workflow * Update curl to use gh cli
1 parent 1e23b32 commit 34f6afe

File tree

2 files changed

+122
-10
lines changed

2 files changed

+122
-10
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Update parser version references
2+
3+
on:
4+
schedule:
5+
- cron: "0 6 * * *"
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
update-parser:
13+
if: github.repository_owner == 'redhat-best-practices-for-k8s'
14+
name: Update parser tag and open PR
15+
runs-on: ubuntu-24.04
16+
permissions:
17+
contents: write
18+
pull-requests: write
19+
env:
20+
SHELL: /bin/bash
21+
PARSER_RELEASES_URL: https://api.github.com/repos/redhat-best-practices-for-k8s/parser/releases/latest
22+
steps:
23+
- name: Check out code
24+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
25+
with:
26+
ref: main
27+
28+
- name: Set up Go
29+
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
30+
with:
31+
go-version-file: go.mod
32+
33+
- name: Determine current and latest parser versions
34+
id: versions
35+
env:
36+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
run: |
38+
set -euo pipefail
39+
40+
CURRENT=$(jq -r '.parserTag' version.json)
41+
echo "current=${CURRENT}" >> $GITHUB_OUTPUT
42+
43+
# Fetch latest release tag using GitHub CLI with retry and exponential backoff
44+
if ! command -v gh >/dev/null 2>&1; then
45+
echo "GitHub CLI (gh) not found on PATH" >&2
46+
exit 1
47+
fi
48+
49+
LATEST=""
50+
for attempt in {1..5}; do
51+
set +e
52+
# Prefer tagName to ensure we capture the actual tag (often 'vX.Y.Z')
53+
LATEST=$(gh release list -R redhat-best-practices-for-k8s/parser --limit 1 --json tagName,isLatest --jq '.[] | select(.isLatest) | .tagName')
54+
rc=$?
55+
set -e
56+
57+
if [[ ${rc} -eq 0 && -n "${LATEST}" ]]; then
58+
break
59+
fi
60+
61+
SLEEP=$((2 ** (attempt-1)))
62+
echo "Attempt ${attempt} failed (rc=${rc}). Retrying in ${SLEEP}s..."
63+
sleep "${SLEEP}"
64+
done
65+
66+
if [[ -z "${LATEST}" || "${LATEST}" == "null" ]]; then
67+
echo "Failed to resolve latest parser tag after retries" >&2
68+
exit 1
69+
fi
70+
echo "latest=${LATEST}" >> $GITHUB_OUTPUT
71+
72+
if [[ "${CURRENT}" == "${LATEST}" ]]; then
73+
echo "Parser already at latest (${LATEST})."; echo "should_update=false" >> $GITHUB_OUTPUT
74+
else
75+
echo "should_update=true" >> $GITHUB_OUTPUT
76+
fi
77+
78+
- name: Stop if already up to date
79+
if: steps.versions.outputs.should_update == 'false'
80+
run: echo "No update needed"
81+
82+
- name: Update version.json to latest parser version
83+
if: steps.versions.outputs.should_update == 'true'
84+
run: |
85+
set -euo pipefail
86+
LATEST=${{ steps.versions.outputs.latest }}
87+
88+
tmp=$(mktemp)
89+
jq --arg tag "${LATEST}" '.parserTag = $tag' version.json > "$tmp" && mv "$tmp" version.json
90+
91+
echo "Updated version.json to ${LATEST}:"
92+
git --no-pager diff -- version.json | cat
93+
94+
- name: Run unit tests
95+
if: steps.versions.outputs.should_update == 'true'
96+
run: make test
97+
98+
- name: Create PR
99+
if: steps.versions.outputs.should_update == 'true'
100+
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
101+
with:
102+
token: ${{ secrets.GITHUB_TOKEN }}
103+
commit-message: "Update parser to ${{ steps.versions.outputs.latest }}"
104+
title: "Update parser to ${{ steps.versions.outputs.latest }}"
105+
body: |
106+
- Bump parser to `${{ steps.versions.outputs.latest }}`
107+
- Updated `version.json`
108+
branch: update-parser-${{ steps.versions.outputs.latest }}
109+
110+

.github/workflows/probe-update.yaml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,33 @@ jobs:
3333

3434
- name: Determine current and latest probe versions
3535
id: versions
36+
env:
37+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3638
run: |
3739
set -euo pipefail
3840
3941
CURRENT=$(jq -r '.debugTag' version.json)
4042
echo "current=${CURRENT}" >> $GITHUB_OUTPUT
4143
42-
# Fetch latest release with retry and exponential backoff
43-
AUTH_HEADER=""
44-
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
45-
AUTH_HEADER="Authorization: Bearer ${GITHUB_TOKEN}"
44+
# Fetch latest release tag using GitHub CLI with retry and exponential backoff
45+
if ! command -v gh >/dev/null 2>&1; then
46+
echo "GitHub CLI (gh) not found on PATH" >&2
47+
exit 1
4648
fi
4749
4850
LATEST=""
4951
for attempt in {1..5}; do
50-
STATUS=$(curl -sS -H "Accept: application/vnd.github+json" ${AUTH_HEADER:+-H "$AUTH_HEADER"} -o /tmp/probe_latest.json -w "%{http_code}" "${PROBE_RELEASES_URL}" || true)
51-
if [[ "${STATUS}" == "200" ]]; then
52-
LATEST=$(jq -r .tag_name /tmp/probe_latest.json)
53-
fi
52+
set +e
53+
LATEST=$(gh release list -R redhat-best-practices-for-k8s/certsuite-probe --limit 1 --json tagName,isLatest --jq '.[] | select(.isLatest) | .tagName')
54+
rc=$?
55+
set -e
5456
55-
if [[ -n "${LATEST}" && "${LATEST}" != "null" ]]; then
57+
if [[ ${rc} -eq 0 && -n "${LATEST}" ]]; then
5658
break
5759
fi
5860
5961
SLEEP=$((2 ** (attempt-1)))
60-
echo "Attempt ${attempt} failed (status=${STATUS}). Retrying in ${SLEEP}s..."
62+
echo "Attempt ${attempt} failed (rc=${rc}). Retrying in ${SLEEP}s..."
6163
sleep "${SLEEP}"
6264
done
6365

0 commit comments

Comments
 (0)