|
| 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 | + |
0 commit comments