Skip to content

Instantly share code, notes, and snippets.

@rmoff
Last active January 17, 2024 12:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rmoff/6d06b0b258a65502828205733b6a8c8e to your computer and use it in GitHub Desktop.
name: "[Production] Build and Deploy to Cloudflare"
on:
# Publish the site when any changes are made to docs-platform
push:
branches:
- main
# Publish the site when triggered from the code repo
# (a GitHub Action on that repo triggers this one)
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App installation access token (IAT)
id: generate_iat
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.DOCS_APP_ID }}
private-key: ${{ secrets.DOCS_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Set the token as environment variable
run: echo "GIT_CREDENTIALS=https://x-access-token:${{ steps.generate_iat.outputs.token }}@github.com" >> "$GITHUB_ENV"
- name: Checkout repository
id: checkout_repo
uses: actions/checkout@v4
- name: Install Node.js
id: install_jodejs
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Antora with the Antora Lunr Extension
id: install_antora
run: npm i antora @antora/lunr-extension
- name: Generate Site
id: build_site
run: npx antora antora-playbook.yml
- name: Check Links
id: check_links
uses: lycheeverse/lychee-action@v1.8.0
with:
args: build/site --no-progress
fail: true
jobSummary: true
format: markdown
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Deploy to Cloudflare Pages
id: cloudflare_deploy
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
projectName: our-org-docs
directory: build/site
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
branch: main
name: "[PR Preview] Build and Deploy to Cloudflare"
on:
pull_request:
branches:
- main
workflow_dispatch:
inputs:
source_repo:
description: 'Source repo'
required: true
type: string
source_owner:
description: 'Source repo owner'
required: true
type: string
pr_num:
description: 'PR number'
required: true
type: string
pr_branch:
description: 'PR branch'
required: true
type: string
pr_url:
description: 'PR URL'
required: true
type: string
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App installation access token (IAT)
id: generate_iat
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.DOCS_APP_ID }}
private-key: ${{ secrets.DOCS_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Set the token as environment variable
run: echo "GIT_CREDENTIALS=https://x-access-token:${{ steps.generate_iat.outputs.token }}@github.com" >> "$GITHUB_ENV"
- name: Checkout repository
id: checkout_repo
uses: actions/checkout@v4
- name: Install Node.js
id: install_jodejs
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Antora with the Antora Lunr Extension
id: install_antora
run: npm i antora @antora/lunr-extension
- name: If not a platform PR, set the branch of the source repo for antora content
if: github.event_name != 'pull_request'
id: override_antora_playbook_yml
run: |
sed -i '7s/main/${{ inputs.pr_branch }}/' antora-playbook.yml
- name: Generate Site
id: build_site
run: npx antora antora-playbook.yml
- name: Check Links
id: check_links
uses: lycheeverse/lychee-action@v1.8.0
with:
args: build/site --no-progress
jobSummary: true
format: markdown
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Overlay PR message on each page
if: github.event_name != 'pull_request'
id: add_pr_to_html_files
working-directory: build/site
run: |
PR_URL=${{ inputs.pr_url }}
PR_NUMBER=${{ inputs.pr_num }}
# Find all .html files and store them in an array
html_files=()
while IFS= read -r -d '' file; do
html_files+=("$file")
done < <(find . -name '*.html' -print0)
# Loop through the array of files
for file in "${html_files[@]}"; do
sed -i -e "s|\(.*\)\(</body>\)|<div style=\"position: fixed; opacity: 75%; top: 5px; left: 5px; padding: 3px; background-color: #e8ac07; font-weight: bold; z-index: 9999; box-shadow: 0 0 10px rgba(0,0,0,0.5);\">ℹ️ This is a preview of PR <a href=\"$PR_URL\" style=\"color: black;\">#$PR_NUMBER</a></div>\n\1\2|" "$file"
done
- name: Set deployment name for passing to Cloudflare
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "DEPLOYMENT_NAME=platform-pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
else
echo "DEPLOYMENT_NAME=pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
fi
- name: Deploy to Cloudflare Pages
id: cloudflare_deploy
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
projectName: our-org-docs
directory: build/site
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ env.DEPLOYMENT_NAME }}
- name: Comment on the source PR issue that triggered the build
if: always()
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate_iat.outputs.token }}
script: |
const steps = ${{ toJson(steps) }};
console.log(steps);
let failureMessage = '';
for (const [key, value] of Object.entries(steps)) {
if (value.outcome === 'failure' || value.conclusion === 'failure') {
failureMessage += `Step \`${key}\` failed. `;
}
}
let body;
if (failureMessage) {
const url = '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}';
body = `## 🚨 Failure in build process.\n\n${failureMessage}\nPlease check the [action logs](${url}) for details.`;
} else {
const environment = '${{ steps.cloudflare_deploy.outputs.environment }}';
const deploymentId = '${{ steps.cloudflare_deploy.outputs.id }}';
const deploymentUrl = '${{ steps.cloudflare_deploy.outputs.url }}';
body = `🤖 🎉 The docs site has been built and deployed to the \`${environment}\` environment, id \`${deploymentId}\`.\n\n## 🔗 ${deploymentUrl}`;
}
if (context.eventName === 'workflow_dispatch') {
await github.rest.issues.createComment({
issue_number: context.payload.inputs.pr_num,
owner: context.payload.inputs.source_owner,
repo: context.payload.inputs.source_repo,
body: body
});
} else if (context.eventName === 'pull_request') {
await github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
};
name: "[Docs] Trigger Build & Deploy"
on:
push:
branches:
- main
paths:
- 'docs/**'
workflow_dispatch:
jobs:
trigger_build:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App installation access token (IAT)
id: generate_iat
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.DOCS_APP_ID }}
private-key: ${{ secrets.DOCS_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Trigger workflow on other repo
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate_iat.outputs.token }}
script: |
github.rest.actions.createWorkflowDispatch({
owner: '${{ github.repository_owner }}',
repo: 'docs-platform',
workflow_id: 'cloudflare.yaml',
ref: 'main'
})
name: "[Docs] PR: Trigger Preview Build & Deploy"
on:
pull_request:
branches:
- main
paths:
- 'docs/**'
jobs:
trigger_build:
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App installation access token (IAT)
id: generate_iat
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.DOCS_APP_ID }}
private-key: ${{ secrets.DOCS_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Trigger workflow on other repo
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate_iat.outputs.token }}
script: |
github.rest.actions.createWorkflowDispatch({
owner: '${{ github.repository_owner }}',
repo: 'docs-platform',
workflow_id: 'preview-cloudflare.yaml',
ref: 'main',
inputs: {
source_repo: context.repo.repo,
source_owner: context.repo.owner,
pr_num: '${{ github.event.number }}',
pr_url: '${{ github.event.pull_request.html_url }}',
pr_branch: '${{ github.head_ref }}'
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment