Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 44 additions & 35 deletions .github/workflows/commit-built-file-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ jobs:
# generated below, so `GITHUB_TOKEN` only needs read access to the triggering workflow's artifacts.
actions: read # Required to list and download the artifact uploaded by the triggering workflow run.
steps:
- name: Download artifact
- name: Download artifacts
id: download-artifacts
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
Expand All @@ -54,43 +55,48 @@ jobs:
run_id: process.env.RUN_ID,
} );

const matchArtifact = artifacts.data.artifacts.filter( ( artifact ) => {
return artifact.name === 'pr-built-file-changes'
} )[0];
// Patches are uploaded with the `pr-built-file-changes` prefix. A single triggering
// workflow run can produce more than one (for example, one per default theme).
const matchArtifacts = artifacts.data.artifacts.filter( ( artifact ) => {
return artifact.name.startsWith( 'pr-built-file-changes' );
} );

if ( ! matchArtifact ) {
core.info( 'No artifact found!' );
if ( matchArtifacts.length === 0 ) {
core.info( 'No artifacts found!' );
core.setOutput( 'artifacts-found', 'false' );
return;
}

const download = await github.rest.actions.downloadArtifact( {
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
} );

const fs = require( 'fs' );
fs.writeFileSync( '${{ github.workspace }}/pr-built-file-changes.zip', Buffer.from( download.data ) )
const path = require( 'path' );
const downloadDir = path.join( process.env.GITHUB_WORKSPACE, 'built-file-changes' );
fs.mkdirSync( downloadDir, { recursive: true } );

for ( const artifact of matchArtifacts ) {
const download = await github.rest.actions.downloadArtifact( {
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
} );

fs.writeFileSync( path.join( downloadDir, `${ artifact.name }.zip` ), Buffer.from( download.data ) );
}

core.setOutput( 'artifacts-found', 'true' );
env:
RUN_ID: ${{ github.event.workflow_run.id }}

- name: Check for artifact
id: artifact-check
- name: Unzip the artifacts containing the PR data
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
run: |
if [ -f "pr-built-file-changes.zip" ]; then
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi

- name: Unzip the artifact containing the PR data
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
run: unzip pr-built-file-changes.zip
for zip in built-file-changes/*.zip; do
unzip "${zip}" -d "${zip%.zip}"
done

- name: Generate Installation Token
id: generate_token
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
env:
GH_APP_ID: ${{ vars.GH_PR_BUILT_FILES_APP_ID }}
GH_APP_PRIVATE_KEY: ${{ secrets.GH_PR_BUILT_FILES_PRIVATE_KEY }}
Expand Down Expand Up @@ -121,7 +127,7 @@ jobs:

- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
with:
repository: ${{ github.event.workflow_run.head_repository.full_name }}
ref: ${{ github.event.workflow_run.head_branch }}
Expand All @@ -130,18 +136,21 @@ jobs:
token: ${{ steps.generate_token.outputs.access-token }}
persist-credentials: true

- name: Apply patch
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
- name: Apply patches
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
run: git apply "$GITHUB_WORKSPACE/changes.diff"
run: |
for patch in "${GITHUB_WORKSPACE}"/built-file-changes/*/changes.diff; do
git apply "${patch}"
done

- name: Display changes to versioned files
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
run: git diff

- name: Configure git user name and email
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
env:
GH_APP_ID: ${{ vars.GH_PR_BUILT_FILES_APP_ID }}
Expand All @@ -150,17 +159,17 @@ jobs:
git config user.email "${GH_APP_ID}+wordpress-develop-pr-bot[bot]@users.noreply.github.com"

- name: Stage changes
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
run: git add .

- name: Commit changes
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
run: |
git commit -m "Automation: Updating built files with changes."

- name: Push changes
if: ${{ steps.artifact-check.outputs.exists == 'true' }}
if: ${{ steps.download-artifacts.outputs.artifacts-found == 'true' }}
working-directory: 'pr-repo'
run: git push
2 changes: 1 addition & 1 deletion .github/workflows/test-and-zip-default-themes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ jobs:
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
if: ${{ steps.built-file-check.outputs.uncommitted_changes == 'true' }}
with:
name: pr-built-file-changes
name: pr-built-file-changes-${{ matrix.theme }}
path: src/wp-content/themes/${{ matrix.theme }}/changes.diff

- name: Ensure version-controlled files are not modified or deleted
Expand Down
Loading