|
| 1 | +--- |
| 2 | +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json |
| 3 | +name: Renovate Helm Hooks |
| 4 | + |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + branches: |
| 8 | + - develop |
| 9 | + paths: |
| 10 | + - 'charts/**' |
| 11 | + |
| 12 | +permissions: {} |
| 13 | + |
| 14 | +concurrency: |
| 15 | + group: renovate-helm-hooks-${{ github.ref }} |
| 16 | + cancel-in-progress: true |
| 17 | + |
| 18 | +jobs: |
| 19 | + renovate-post-run: |
| 20 | + name: Renovate Bump Chart Version |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + pull-requests: write |
| 25 | + if: github.actor == 'renovate[bot]' |
| 26 | + steps: |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + persist-credentials: false |
| 32 | + |
| 33 | + - uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4 |
| 34 | + id: app-token |
| 35 | + with: |
| 36 | + app-id: 2138788 |
| 37 | + private-key: ${{ secrets.APP_SEERR_HELM_PRIVATE_KEY }} |
| 38 | + |
| 39 | + - name: Set up chart-testing |
| 40 | + uses: helm/chart-testing-action@0d28d3144d3a25ea2cc349d6e59901c4ff469b3b # v2.7.0 |
| 41 | + |
| 42 | + - name: Run chart-testing (list-changed) |
| 43 | + id: list-changed |
| 44 | + run: | |
| 45 | + changed="$(ct list-changed --target-branch ${TARGET_BRANCH})" |
| 46 | + if [[ -n "$changed" ]]; then |
| 47 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 48 | + echo "changed_list=${changed//$'\n'/ }" >> "$GITHUB_OUTPUT" |
| 49 | + fi |
| 50 | + env: |
| 51 | + TARGET_BRANCH: ${{ github.event.repository.default_branch }} |
| 52 | + |
| 53 | + - name: Bump chart version |
| 54 | + if: steps.list-changed.outputs.changed == 'true' |
| 55 | + env: |
| 56 | + CHART: ${{ steps.list-changed.outputs.changed_list }} |
| 57 | + run: | |
| 58 | + if [[ ! -d "${CHART}" ]]; then |
| 59 | + echo "${CHART} directory not found" |
| 60 | + exit 0 |
| 61 | + fi |
| 62 | +
|
| 63 | + # Extract current appVersion and chart version from Chart.yaml |
| 64 | + APP_VERSION=$(grep -e "^appVersion:" "$CHART/Chart.yaml" | cut -d ":" -f 2 | tr -d '[:space:]' | tr -d '"') |
| 65 | + CHART_VERSION=$(grep -e "^version:" "$CHART/Chart.yaml" | cut -d ":" -f 2 | tr -d '[:space:]' | tr -d '"') |
| 66 | +
|
| 67 | + # Extract major, minor and patch versions of appVersion |
| 68 | + APP_MAJOR_VERSION=$(printf '%s' "$APP_VERSION" | cut -d "." -f 1) |
| 69 | + APP_MINOR_VERSION=$(printf '%s' "$APP_VERSION" | cut -d "." -f 2) |
| 70 | + APP_PATCH_VERSION=$(printf '%s' "$APP_VERSION" | cut -d "." -f 3) |
| 71 | +
|
| 72 | + # Extract major, minor and patch versions of chart version |
| 73 | + CHART_MAJOR_VERSION=$(printf '%s' "$CHART_VERSION" | cut -d "." -f 1) |
| 74 | + CHART_MINOR_VERSION=$(printf '%s' "$CHART_VERSION" | cut -d "." -f 2) |
| 75 | + CHART_PATCH_VERSION=$(printf '%s' "$CHART_VERSION" | cut -d "." -f 3) |
| 76 | +
|
| 77 | + # Get previous appVersion from the base commit of the pull request |
| 78 | + BASE_COMMIT=$(git merge-base origin/main HEAD) |
| 79 | + PREV_APP_VERSION=$(git show "$BASE_COMMIT":"$CHART/Chart.yaml" | grep -e "^appVersion:" | cut -d ":" -f 2 | tr -d '[:space:]' | tr -d '"') |
| 80 | +
|
| 81 | + # Extract major, minor and patch versions of previous appVersion |
| 82 | + PREV_APP_MAJOR_VERSION=$(printf '%s' "$PREV_APP_VERSION" | cut -d "." -f 1) |
| 83 | + PREV_APP_MINOR_VERSION=$(printf '%s' "$PREV_APP_VERSION" | cut -d "." -f 2) |
| 84 | + PREV_APP_PATCH_VERSION=$(printf '%s' "$PREV_APP_VERSION" | cut -d "." -f 3) |
| 85 | +
|
| 86 | + # Check if the major, minor, or patch version of appVersion has changed |
| 87 | + if [[ "$APP_MAJOR_VERSION" != "$PREV_APP_MAJOR_VERSION" ]]; then |
| 88 | + # Bump major version of the chart and reset minor and patch versions to 0 |
| 89 | + CHART_MAJOR_VERSION=$((CHART_MAJOR_VERSION+1)) |
| 90 | + CHART_MINOR_VERSION=0 |
| 91 | + CHART_PATCH_VERSION=0 |
| 92 | + elif [[ "$APP_MINOR_VERSION" != "$PREV_APP_MINOR_VERSION" ]]; then |
| 93 | + # Bump minor version of the chart and reset patch version to 0 |
| 94 | + CHART_MINOR_VERSION=$((CHART_MINOR_VERSION+1)) |
| 95 | + CHART_PATCH_VERSION=0 |
| 96 | + elif [[ "$APP_PATCH_VERSION" != "$PREV_APP_PATCH_VERSION" ]]; then |
| 97 | + # Bump patch version of the chart |
| 98 | + CHART_PATCH_VERSION=$((CHART_PATCH_VERSION+1)) |
| 99 | + fi |
| 100 | +
|
| 101 | + # Update the chart version in Chart.yaml |
| 102 | + CHART_NEW_VERSION="${CHART_MAJOR_VERSION}.${CHART_MINOR_VERSION}.${CHART_PATCH_VERSION}" |
| 103 | + sed -i "s/^version:.*/version: ${CHART_NEW_VERSION}/" "$CHART/Chart.yaml" |
| 104 | +
|
| 105 | + - name: Ensure documentation is updated |
| 106 | + if: steps.list-changed.outputs.changed == 'true' |
| 107 | + uses: docker://jnorwood/helm-docs:v1.14.2@sha256:7e562b49ab6b1dbc50c3da8f2dd6ffa8a5c6bba327b1c6335cc15ce29267979c |
| 108 | + |
| 109 | + - name: Commit changes |
| 110 | + if: steps.list-changed.outputs.changed == 'true' |
| 111 | + env: |
| 112 | + CHART: ${{ steps.list-changed.outputs.changed_list }} |
| 113 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
| 114 | + GITHUB_HEAD_REF: ${{ github.head_ref }} |
| 115 | + run: | |
| 116 | + # Define the target directory |
| 117 | + TARGET_DIR="$CHART" |
| 118 | +
|
| 119 | + # Fetch deleted files in the target directory |
| 120 | + DELETED_FILES=$(git diff --diff-filter=D --name-only HEAD -- "$TARGET_DIR") |
| 121 | +
|
| 122 | + # Fetch added/modified files in the target directory |
| 123 | + MODIFIED_FILES=$(git diff --diff-filter=ACM --name-only HEAD -- "$TARGET_DIR") |
| 124 | +
|
| 125 | + # Create a temporary file for JSON output |
| 126 | + FILE_CHANGES_JSON_FILE=$(mktemp) |
| 127 | +
|
| 128 | + # Initialize JSON structure in the file |
| 129 | + echo '{ "deletions": [], "additions": [] }' > "$FILE_CHANGES_JSON_FILE" |
| 130 | +
|
| 131 | + # Add deletions |
| 132 | + for file in $DELETED_FILES; do |
| 133 | + jq --arg path "$file" '.deletions += [{"path": $path}]' "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp" |
| 134 | + mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE" |
| 135 | + done |
| 136 | +
|
| 137 | + # Add additions (new or modified files) |
| 138 | + for file in $MODIFIED_FILES; do |
| 139 | + BASE64_CONTENT=$(base64 -w 0 <"$file") # Encode file content |
| 140 | + jq --arg path "$file" --arg content "$BASE64_CONTENT" \ |
| 141 | + '.additions += [{"path": $path, "contents": $content}]' "$FILE_CHANGES_JSON_FILE" > "$FILE_CHANGES_JSON_FILE.tmp" |
| 142 | + mv "$FILE_CHANGES_JSON_FILE.tmp" "$FILE_CHANGES_JSON_FILE" |
| 143 | + done |
| 144 | +
|
| 145 | + # Create a temporary file for the final JSON payload |
| 146 | + JSON_PAYLOAD_FILE=$(mktemp) |
| 147 | +
|
| 148 | + # Construct the final JSON using jq and store it in a file |
| 149 | + jq -n --arg repo "$GITHUB_REPOSITORY" \ |
| 150 | + --arg branch "$GITHUB_HEAD_REF" \ |
| 151 | + --arg message "fix: post upgrade changes from renovate" \ |
| 152 | + --arg expectedOid "$GITHUB_SHA" \ |
| 153 | + --slurpfile fileChanges "$FILE_CHANGES_JSON_FILE" \ |
| 154 | + '{ |
| 155 | + query: "mutation ($input: CreateCommitOnBranchInput!) { |
| 156 | + createCommitOnBranch(input: $input) { |
| 157 | + commit { |
| 158 | + url |
| 159 | + } |
| 160 | + } |
| 161 | + }", |
| 162 | + variables: { |
| 163 | + input: { |
| 164 | + branch: { |
| 165 | + repositoryNameWithOwner: $repo, |
| 166 | + branchName: $branch |
| 167 | + }, |
| 168 | + message: { headline: $message }, |
| 169 | + fileChanges: $fileChanges[0], |
| 170 | + expectedHeadOid: $expectedOid |
| 171 | + } |
| 172 | + } |
| 173 | + }' > "$JSON_PAYLOAD_FILE" |
| 174 | +
|
| 175 | + # Call GitHub API |
| 176 | + curl https://api.github.com/graphql -f \ |
| 177 | + -sSf -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 178 | + --data "@$JSON_PAYLOAD_FILE" |
| 179 | +
|
| 180 | + # Clean up temporary files |
| 181 | + rm "$FILE_CHANGES_JSON_FILE" "$JSON_PAYLOAD_FILE" |
0 commit comments