Skip to content

Instantly share code, notes, and snippets.

@vcanales
Last active November 13, 2023 14:46
Show Gist options
  • Save vcanales/3798d385d2094dfde3f02da29c3ea822 to your computer and use it in GitHub Desktop.
Save vcanales/3798d385d2094dfde3f02da29c3ea822 to your computer and use it in GitHub Desktop.
deploy_theme.yml
name: Deploy PR to GitHub Pages
on:
pull_request:
types: [opened, synchronize, ready_for_review]
jobs:
build-theme:
permissions:
actions: write
contents: read
pull-requests: write
name: 'Build theme zip'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build theme zip
run: |
zip -r pr-${{github.event.pull_request.number}}-preview.zip *
# Delete previous artifacts for this PR
- name: Delete Previous Artifacts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
ARTIFACTS=$(gh api repos/${{github.repository}}/actions/artifacts --paginate | jq '.artifacts[] | select(.name == "theme-preview-${{github.event.pull_request.number}}") | .id')
for ID in $ARTIFACTS; do
gh api repos/${{github.repository}}/actions/artifacts/$ID -X DELETE
done
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: theme-preview-${{github.event.pull_request.number}}
path: pr-${{github.event.pull_request.number}}-preview.zip
retention-days: 7
# Use the GitHub API to get the download URL for the artifact and store it as an output for use on the next step
- name: Get Download Link
id: get_download_link
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_DEBUG: 1
run: |
gh api repos/${{ github.repository }}/actions/artifacts --jq 'first(.artifacts[] | select(.name == "theme-preview-1") | .archive_download_url)' > download_url_call.json
OUTPUT=$(cat download_url_call.json)
echo "DOWNLOAD_URL=$OUTPUT" >> $GITHUB_OUTPUT
# Create an HTML file with the download URL
- name: Create HTML File
run: |
echo "<iframe id=\"wp-playground\" style=\"width: 1200px; height: 800px\"></iframe>
<script type=\"module\">
import { startPlaygroundWeb } from 'https://unpkg.com/@wp-playground/client/index.js';
const client = await startPlaygroundWeb({
iframe: document.getElementById('wp-playground'),
blueprint: {
landingPage: '/wp-admin/',
preferredVersions: {
php: '8.0',
wp: 'latest',
},
steps: [
{
step: 'login',
username: 'admin',
password: 'password',
},
{
step: 'installTheme',
themeZipFile: {
resource: 'url',
url: '${{ steps.get_download_link.outputs.DOWNLOAD_URL }}'
}
}
],
},
});
await client.isReady;
client.goTo('/');
</script>" > index-${{github.event.pull_request.number}}.html
- name: Upload HTML File
uses: actions/upload-artifact@v3
with:
name: theme-preview-html-${{github.event.pull_request.number}}
path: index-${{github.event.pull_request.number}}.html
retention-days: 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment