|
| 1 | +# Commits all missed changes to built files back to pull request branches. |
| 2 | +name: Commit Built File Changes (PRs) |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: |
| 7 | + - 'Check Built Files (PRs)' |
| 8 | + - 'Test Default Themes & Create ZIPs' |
| 9 | + types: |
| 10 | + - completed |
| 11 | + |
| 12 | +# Cancels all previous workflow runs for pull requests that have not completed. |
| 13 | +concurrency: |
| 14 | + # The concurrency group contains the workflow name and the branch name for pull requests |
| 15 | + # or the commit hash for any other events. |
| 16 | + group: ${{ github.workflow }}-${{ github.event_name == 'workflow_run' && format( '{0}-{1}', github.event.workflow_run.head_branch, github.event.workflow_run.head_repository.name ) || github.sha }} |
| 17 | + |
| 18 | +# Disable permissions for all available scopes by default. |
| 19 | +# Any needed permissions should be configured at the job level. |
| 20 | +permissions: {} |
| 21 | + |
| 22 | +jobs: |
| 23 | + # Checks a PR for uncommitted changes to built files. |
| 24 | + # |
| 25 | + # Performs the following steps: |
| 26 | + # - Attempts to download the artifact containing the PR diff. |
| 27 | + # - Checks for the existence of an artifact. |
| 28 | + # - Unzips the artifact. |
| 29 | + # - Generates a token for authenticating with the GitHub App. |
| 30 | + # - Checks out the repository. |
| 31 | + # - Applies the patch file. |
| 32 | + # - Displays the result of git diff. |
| 33 | + # - Configures the Git author. |
| 34 | + # - Stages changes. |
| 35 | + # - Commits changes. |
| 36 | + # - Pushes changes. |
| 37 | + update-built-files: |
| 38 | + name: Check and update built files |
| 39 | + runs-on: ubuntu-24.04 |
| 40 | + # Temporarily disabled while working on Gutenberg integration |
| 41 | + if: false |
| 42 | + # if: ${{ github.repository == 'wordpress/wordpress-develop' }} |
| 43 | + timeout-minutes: 10 |
| 44 | + permissions: |
| 45 | + contents: write |
| 46 | + steps: |
| 47 | + - name: Download artifact |
| 48 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 49 | + with: |
| 50 | + script: | |
| 51 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts( { |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + run_id: process.env.RUN_ID, |
| 55 | + } ); |
| 56 | +
|
| 57 | + const matchArtifact = artifacts.data.artifacts.filter( ( artifact ) => { |
| 58 | + return artifact.name === 'pr-built-file-changes' |
| 59 | + } )[0]; |
| 60 | +
|
| 61 | + if ( ! matchArtifact ) { |
| 62 | + core.info( 'No artifact found!' ); |
| 63 | + return; |
| 64 | + } |
| 65 | +
|
| 66 | + const download = await github.rest.actions.downloadArtifact( { |
| 67 | + owner: context.repo.owner, |
| 68 | + repo: context.repo.repo, |
| 69 | + artifact_id: matchArtifact.id, |
| 70 | + archive_format: 'zip', |
| 71 | + } ); |
| 72 | +
|
| 73 | + const fs = require( 'fs' ); |
| 74 | + fs.writeFileSync( '${{ github.workspace }}/pr-built-file-changes.zip', Buffer.from( download.data ) ) |
| 75 | + env: |
| 76 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 77 | + |
| 78 | + - name: Check for artifact |
| 79 | + id: artifact-check |
| 80 | + run: | |
| 81 | + if [ -f "pr-built-file-changes.zip" ]; then |
| 82 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 83 | + else |
| 84 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Unzip the artifact containing the PR data |
| 88 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 89 | + run: unzip pr-built-file-changes.zip |
| 90 | + |
| 91 | + - name: Generate Installation Token |
| 92 | + id: generate_token |
| 93 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 94 | + env: |
| 95 | + GH_APP_ID: ${{ secrets.GH_PR_BUILT_FILES_APP_ID }} |
| 96 | + GH_APP_PRIVATE_KEY: ${{ secrets.GH_PR_BUILT_FILES_PRIVATE_KEY }} |
| 97 | + run: | |
| 98 | + echo "$GH_APP_PRIVATE_KEY" > private-key.pem |
| 99 | +
|
| 100 | + # Generate JWT |
| 101 | + JWT=$(python3 - <<EOF |
| 102 | + import jwt, time |
| 103 | + private_key = open("private-key.pem", "r").read() |
| 104 | + payload = { |
| 105 | + "iat": int(time.time()), |
| 106 | + "exp": int(time.time()) + 600, # 10-minute expiration |
| 107 | + "iss": $GH_APP_ID |
| 108 | + } |
| 109 | + print(jwt.encode(payload, private_key, algorithm="RS256")) |
| 110 | + EOF |
| 111 | + ) |
| 112 | +
|
| 113 | + # Get Installation ID |
| 114 | + INSTALLATION_ID=$(curl -s -X GET -H "Authorization: Bearer $JWT" \ |
| 115 | + -H "Accept: application/vnd.github.v3+json" \ |
| 116 | + https://api.github.com/app/installations | jq -r '.[0].id') |
| 117 | +
|
| 118 | + # Request Installation Access Token |
| 119 | + ACCESS_TOKEN=$(curl -s -X POST -H "Authorization: Bearer $JWT" \ |
| 120 | + -H "Accept: application/vnd.github.v3+json" \ |
| 121 | + "https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens" | jq -r '.token') |
| 122 | +
|
| 123 | + echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> "$GITHUB_ENV" |
| 124 | +
|
| 125 | + rm -f private-key.pem |
| 126 | +
|
| 127 | + - name: Checkout repository |
| 128 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 129 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 130 | + with: |
| 131 | + repository: ${{ github.event.workflow_run.head_repository.full_name }} |
| 132 | + ref: ${{ github.event.workflow_run.head_branch }} |
| 133 | + path: 'pr-repo' |
| 134 | + show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} |
| 135 | + token: ${{ env.ACCESS_TOKEN }} |
| 136 | + |
| 137 | + - name: Apply patch |
| 138 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 139 | + working-directory: 'pr-repo' |
| 140 | + run: git apply ${{ github.workspace }}/changes.diff |
| 141 | + |
| 142 | + - name: Display changes to versioned files |
| 143 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 144 | + working-directory: 'pr-repo' |
| 145 | + run: git diff |
| 146 | + |
| 147 | + - name: Configure git user name and email |
| 148 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 149 | + working-directory: 'pr-repo' |
| 150 | + env: |
| 151 | + GH_APP_ID: ${{ secrets.GH_PR_BUILT_FILES_APP_ID }} |
| 152 | + run: | |
| 153 | + git config user.name "wordpress-develop-pr-bot[bot]" |
| 154 | + git config user.email ${{ env.GH_APP_ID }}+wordpress-develop-pr-bot[bot]@users.noreply.github.com |
| 155 | +
|
| 156 | + - name: Stage changes |
| 157 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 158 | + working-directory: 'pr-repo' |
| 159 | + run: git add . |
| 160 | + |
| 161 | + - name: Commit changes |
| 162 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 163 | + working-directory: 'pr-repo' |
| 164 | + run: | |
| 165 | + git commit -m "Automation: Updating built files with changes." |
| 166 | +
|
| 167 | + - name: Push changes |
| 168 | + if: ${{ steps.artifact-check.outputs.exists == 'true' }} |
| 169 | + working-directory: 'pr-repo' |
| 170 | + run: git push |
0 commit comments