|
6 | 6 | workflow_dispatch: |
7 | 7 | permissions: |
8 | 8 | contents: write |
| 9 | + pull-requests: write |
| 10 | + |
| 11 | +env: |
| 12 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 13 | + BRANCH: fetch-release-data-update |
| 14 | + |
9 | 15 | jobs: |
10 | 16 | fetch-release-data: |
11 | 17 | runs-on: ubuntu-latest |
12 | 18 | steps: |
13 | | - - uses: actions/checkout@v4 |
| 19 | + - name: Try checkout ${{ env.BRANCH }} |
| 20 | + uses: actions/checkout@v4 |
| 21 | + id: try-checkout |
| 22 | + continue-on-error: true |
14 | 23 | with: |
15 | | - sparse-checkout: | |
16 | | - scripts |
17 | | - static/data/releases |
18 | | - - run: | |
| 24 | + ref: ${{ env.BRANCH }} |
| 25 | + |
| 26 | + - name: Fallback to default branch if checkout failed |
| 27 | + if: steps.try-checkout.outcome == 'failure' |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + ref: ${{ github.event.repository.default_branch }} |
| 31 | + |
| 32 | + - name: Configure git |
| 33 | + run: | |
| 34 | + git config user.name "github-actions[bot]" |
| 35 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 36 | +
|
| 37 | + - name: Ensure branch exists or create it |
| 38 | + run: | |
| 39 | + git fetch origin $BRANCH || true |
| 40 | + if git rev-parse --verify $BRANCH; then |
| 41 | + git checkout $BRANCH |
| 42 | + else |
| 43 | + git checkout -b $BRANCH |
| 44 | + fi |
| 45 | +
|
| 46 | + - name: Fetch release data and update |
| 47 | + id: fetch-release-data |
| 48 | + run: | |
| 49 | + # fetch release data |
19 | 50 | ./scripts/fetch-releases.sh |
| 51 | +
|
| 52 | + # exit if there are no changes to be made |
20 | 53 | if git diff --quiet static/data/releases; then |
| 54 | + echo "skip_pr=true" >> $GITHUB_OUTPUT |
21 | 55 | echo "No changes to commit" |
22 | 56 | exit 0 |
23 | 57 | fi |
24 | | - git config user.name "github-actions[bot]" |
25 | | - git config user.email "github-actions[bot]@users.noreply.github.com" |
| 58 | +
|
| 59 | + job_url=$(gh run --repo ${{ github.repository }} view ${{ github.run_id }} --json jobs --jq '.jobs[] | select(.name == "${{ github.job }}") | .url') |
| 60 | +
|
| 61 | + # commit and push changes |
26 | 62 | git add static/data/releases |
27 | | - git commit -m "Update release data" |
28 | | - git push |
29 | | - env: |
30 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + git commit -m "Update release data" -m "Job: $job_url" |
| 64 | + git push origin $BRANCH |
| 65 | +
|
| 66 | + - name: Open a PR if needed |
| 67 | + if: ${{ steps.fetch-release-data.outputs.skip_pr != 'true' }} |
| 68 | + run: | |
| 69 | + prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$BRANCH" --base 'master' --json title --jq 'length') |
| 70 | + if ((prs == 0)); then |
| 71 | + gh pr create -B master -H $BRANCH --title 'Update Cadence Release Data' --body "Created by Github action" |
| 72 | + fi |
0 commit comments