fork/team

git clone https://git.julia.blue/fork/team

Marco IeniMerge pull request #1688 from Kobzol/dry-run-fail-pipefail9e6d700

remove-fuchsia
7.2 KiB182 linesraw
1# This workflow executes a dry-run of the sync-team tool after a push to any pull request.
2# This allows us to see which changes would be applied to live services after the PR
3# would be merged.
4#
5# The workflow uses the `workflow_run` trigger, which should always run in the default branch of
6# this repository. This is required so that the workflow has permissions to post PR comments.
7# We should not check out any code from the PR, as it could present a security hazard.
8# Instead, we simply download a GitHub artifact with a directory of JSON files and use that as
9# input for sync-team.
10# This artifact is created and uploaded on PR pushes using the CI workflow in `main.yml`.
11# Details about `workflow_run`:
12# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#workflow_run:
13name: sync-team dry-run
14
15on:
16  workflow_run:
17    workflows: [ CI ]
18    types:
19      - completed
20
21jobs:
22  dry-run:
23    runs-on: ubuntu-latest
24    if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
25    concurrency:
26      # Only run this once at a time on any given PR
27      group: dry-run-${{ github.event.workflow_run.head_branch }}
28      cancel-in-progress: true
29    permissions:
30      pull-requests: write
31    steps:
32      - uses: actions/checkout@v4
33        with:
34          repository: rust-lang/sync-team
35          persist-credentials: false
36
37      - name: Install Rust Stable
38        run: |
39          rustc -vV
40          rustup update stable
41          rustup default stable
42          rustc -vV
43
44      # Pinning a specific version to avoid surprises
45      - uses: Swatinem/rust-cache@27b8ea9368cf428f0bfe41b0876b1a7e809d9844
46
47      - name: Download built JSON API
48        uses: actions/download-artifact@v4
49        with:
50          name: team-api-output
51          path: team-api
52          run-id: ${{ github.event.workflow_run.id }}
53          github-token: ${{ secrets.GITHUB_TOKEN }}
54
55      # GitHub tokens generated from GitHub Apps can access resources from one organization,
56      # so we need to generate a token for each organization.
57      - name: Generate GitHub token (rust-lang)
58        uses: actions/create-github-app-token@v1
59        id: rust-lang-token
60        with:
61          # GitHub App ID secret name
62          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
63          # GitHub App private key secret name
64          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
65          # Set the owner, so the token can be used in all repositories
66          owner: rust-lang
67
68      - name: Generate GitHub token (rust-lang-ci)
69        uses: actions/create-github-app-token@v1
70        id: rust-lang-ci-token
71        with:
72          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
73          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
74          owner: rust-lang-ci
75
76      - name: Generate GitHub token (rust-lang-deprecated)
77        uses: actions/create-github-app-token@v1
78        id: rust-lang-deprecated-token
79        with:
80          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
81          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
82          owner: rust-lang-deprecated
83
84      - name: Generate GitHub token (rust-lang-nursery)
85        uses: actions/create-github-app-token@v1
86        id: rust-lang-nursery-token
87        with:
88          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
89          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
90          owner: rust-lang-nursery
91
92      - name: Generate GitHub token (bors-rs)
93        uses: actions/create-github-app-token@v1
94        id: bors-rs-token
95        with:
96          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
97          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
98          owner: bors-rs
99
100      - name: Generate GitHub token (rust-analyzer)
101        uses: actions/create-github-app-token@v1
102        id: rust-analyzer-token
103        with:
104          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
105          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
106          owner: rust-analyzer
107
108      - name: Generate GitHub token (rust-embedded)
109        uses: actions/create-github-app-token@v1
110        id: rust-embedded-token
111        with:
112          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
113          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
114          owner: rust-embedded
115
116      - name: Generate GitHub token (rust-dev-tools)
117        uses: actions/create-github-app-token@v1
118        id: rust-dev-tools-token
119        with:
120          app-id: ${{ secrets.SYNC_TEAM_GH_APP_ID }}
121          private-key: ${{ secrets.SYNC_TEAM_GH_APP_PRIVATE_KEY }}
122          owner: rust-dev-tools
123
124      - name: Run sync-team dry-run check
125        env:
126          GITHUB_TOKEN_RUST_LANG: ${{ steps.rust-lang-token.outputs.token }}
127          GITHUB_TOKEN_RUST_LANG_CI: ${{ steps.rust-lang-ci-token.outputs.token }}
128          GITHUB_TOKEN_RUST_LANG_DEPRECATED: ${{ steps.rust-lang-deprecated-token.outputs.token }}
129          GITHUB_TOKEN_RUST_LANG_NURSERY: ${{ steps.rust-lang-nursery-token.outputs.token }}
130          GITHUB_TOKEN_BORS_RS: ${{ steps.bors-rs-token.outputs.token }}
131          GITHUB_TOKEN_RUST_ANALYZER: ${{ steps.rust-analyzer-token.outputs.token }}
132          GITHUB_TOKEN_RUST_EMBEDDED: ${{ steps.rust-embedded-token.outputs.token }}
133          GITHUB_TOKEN_RUST_DEV_TOOLS: ${{ steps.rust-dev-tools-token.outputs.token }}
134        # This applies pipefail, so that the tee pipeline below fails when sync-team fails.
135        shell: bash
136        run: |
137          # Perform build and execution separately to avoid any potential output from
138          # cargo leaking into the output file.
139          cargo build --release
140          ./target/release/sync-team print-plan --services github --team-json team-api 2>&1 | tee -a output.txt
141
142      - name: Prepare comment
143        run: |
144          cat > comment.txt << EOL
145          <details>
146          <summary>Dry-run check results</summary>
147          <pre><code>
148          EOL
149          cat output.txt >> comment.txt
150          printf "</pre></code>\n</details>\n" >> comment.txt
151
152          cat comment.txt
153
154      - name: Extract PR number
155        run: |
156          # We read the PR number that is stored in the uploaded archive
157          # and check that it is an integer (as the workflow could upload whatever it wants).
158          UNSANITIZED_PR=`cat team-api/pr.txt`
159          if [[ ${UNSANITIZED_PR} =~ ^[0-9]+$ ]]; then
160            echo "PR_NUMBER=${UNSANITIZED_PR}" >> $GITHUB_ENV
161          else
162            echo "Invalid PR number passed: ${UNSANITIZED_PR}"
163            exit 1
164          fi
165
166      - name: Send comment
167        env:
168          GH_TOKEN: ${{ github.token }}
169        run: |
170          PR=${PR_NUMBER}
171          echo "Pull request ${PR}"
172
173          # --edit-last doesn't work if there is no previous comment, so we have to figure out
174          # if we should create a comment or not
175          if gh issue view ${PR} --repo rust-lang/team --json comments \
176            --jq '.comments.[].author.login' | grep --quiet --fixed-strings "github-actions"; then
177            echo "Editing comment"
178            gh pr comment ${PR} --repo rust-lang/team --body-file comment.txt --edit-last
179          else
180            echo "Creating new comment"
181            gh pr comment ${PR} --repo rust-lang/team --body-file comment.txt
182          fi