Update RHDH Image #132
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update RHDH Image | |
| on: | |
| schedule: | |
| - cron: "0 3 * * *" | |
| workflow_dispatch: | |
| jobs: | |
| update-rhdh-image: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | |
| with: | |
| ref: development | |
| - name: Generate GitHub App token | |
| id: app-token | |
| uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1 | |
| with: | |
| app-id: ${{ secrets.APP_ID }} | |
| private-key: ${{ secrets.APP_PRIVATE_KEY }} | |
| # read the current tag from values.yaml, extract the MAJOR.MAJOR prefix | |
| # (e.g. "1.10" from "1.10-123"), then query quay.io for the highest minor | |
| # number available under that same prefix. Outputs update_needed, | |
| # current_tag, and latest_tag for use in subsequent steps. | |
| - name: Check for newer minor version of rhdh/rhdh-hub-rhel9 | |
| id: check-image | |
| run: | | |
| VALUES_FILE="charts/rhdh/values.yaml" | |
| CURRENT_TAG=$(grep -A3 'registry: quay.io' "$VALUES_FILE" | grep 'tag:' | head -1 | sed 's/.*tag: *"\?\([^"]*\)"\?.*/\1/') | |
| echo "Current tag: $CURRENT_TAG" | |
| # Extract the MAJOR.MAJOR prefix (everything before the last "-") | |
| TAG_PREFIX="${CURRENT_TAG%-*}-" | |
| echo "Looking for tags with prefix: $TAG_PREFIX" | |
| LATEST_TAG=$(curl -sf \ | |
| "https://quay.io/api/v1/repository/rhdh/rhdh-hub-rhel9/tag/?filter_tag_name=like:${TAG_PREFIX}&limit=100&onlyActiveTags=true" \ | |
| | jq -r --arg prefix "$TAG_PREFIX" \ | |
| '[.tags[].name | select(startswith($prefix) and test("^[^-]+-[0-9]+$"))] | |
| | sort_by(split("-")[-1] | tonumber) | last') | |
| if [ -z "$LATEST_TAG" ] || [ "$LATEST_TAG" = "null" ]; then | |
| echo "Failed to fetch tags from Quay.io" | |
| exit 1 | |
| fi | |
| echo "Latest tag: $LATEST_TAG" | |
| echo "latest_tag=$LATEST_TAG" >> "$GITHUB_OUTPUT" | |
| echo "current_tag=$CURRENT_TAG" >> "$GITHUB_OUTPUT" | |
| if [ "$CURRENT_TAG" = "$LATEST_TAG" ]; then | |
| echo "Image is already up to date." | |
| echo "update_needed=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "Update needed: $CURRENT_TAG -> $LATEST_TAG" | |
| echo "update_needed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| # patch values.yaml in-place: update only the tag, keeping rhdh/rhdh-hub-rhel9. | |
| - name: Update values.yaml | |
| if: steps.check-image.outputs.update_needed == 'true' | |
| run: | | |
| VALUES_FILE="charts/rhdh/values.yaml" | |
| LATEST_TAG="${{ steps.check-image.outputs.latest_tag }}" | |
| sed -i "s|tag: \"[^\"]*\"|tag: \"$LATEST_TAG\"|g" "$VALUES_FILE" | |
| echo "Updated $VALUES_FILE:" | |
| grep -A4 'registry: quay.io' "$VALUES_FILE" | head -5 | |
| # commit the updated values.yaml to a new branch and open a PR against development. | |
| # if a PR for this exact image tag is already open, do nothing. | |
| # if a PR for an older tag is open, close it and replace with the new one. | |
| - name: Open pull request | |
| if: steps.check-image.outputs.update_needed == 'true' | |
| env: | |
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
| run: | | |
| LATEST_TAG="${{ steps.check-image.outputs.latest_tag }}" | |
| CURRENT_TAG="${{ steps.check-image.outputs.current_tag }}" | |
| BRANCH="automation/rhdh-image-$LATEST_TAG" | |
| PR_TITLE="(chore): update rhdh image" | |
| EXISTING_PR=$(gh pr list --state open --json number,title,headRefName \ | |
| --jq ".[] | select(.title == \"$PR_TITLE\")" | head -1) | |
| if [ -n "$EXISTING_PR" ]; then | |
| EXISTING_PR_BRANCH=$(echo "$EXISTING_PR" | jq -r '.headRefName') | |
| if [ "$EXISTING_PR_BRANCH" = "$BRANCH" ]; then | |
| # A PR for this exact image tag is already open — nothing to do. | |
| echo "PR for $LATEST_TAG already exists, skipping." | |
| exit 0 | |
| fi | |
| # A PR for an older tag exists — close it and replace with the new one. | |
| EXISTING_PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number') | |
| echo "Closing outdated PR #$EXISTING_PR_NUMBER (branch: $EXISTING_PR_BRANCH)" | |
| gh pr close "$EXISTING_PR_NUMBER" --comment "Superseded by a newer automated update." | |
| git push origin --delete "$EXISTING_PR_BRANCH" || true | |
| fi | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git checkout -b "$BRANCH" | |
| git add charts/rhdh/values.yaml | |
| git commit -m "chore(rhdh): update RHDH image to $LATEST_TAG" | |
| git push origin "$BRANCH" | |
| WORKFLOW_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
| printf '## PR Description\n\nAutomated update triggered by the [Update RHDH Image](%s) workflow.\n\nUpdates the RHDH image in `charts/rhdh/values.yaml` from `%s` to:\n- `rhdh`: `quay.io/rhdh/rhdh-hub-rhel9:%s`\n' \ | |
| "$WORKFLOW_URL" "$CURRENT_TAG" "$LATEST_TAG" > /tmp/pr-body.md | |
| gh pr create \ | |
| --base development \ | |
| --head "$BRANCH" \ | |
| --title "$PR_TITLE" \ | |
| --body-file /tmp/pr-body.md |