Added new CF deploy action. #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Publish | |
name: Publish | |
# Controls when the workflow will run | |
on: | |
# Triggers the workflow on push event, but only for main branch | |
push: | |
branches: | |
- main | |
# Triggers the workflow on pull request event, but only for pull request from not forked repo | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
# Triggers the workflow on pull request event, but only for pull request opened or pull request labeled with "🚀request-deploy" (from forked repo) | |
# pull_request is not allowed to use secrets, so we use pull_request_target instead (in forked repos) | |
pull_request_target: | |
types: | |
# When a created pull request from forked repo, it will be comment 'Should deploy to add label' | |
- opened | |
# When a labeled '🚀request-deploy' pull request from forked repo, it will be deploy to Cloudflare Pages | |
- labeled | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
permissions: | |
# default contents: read & write (in forked repos, only read) | |
contents: write | |
# default deployments: read & write (in forked repos, only read) | |
deployments: write | |
# default pull-requests: read & write (in forked repos, only read) | |
pull-requests: write | |
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | |
jobs: | |
# Deploy | |
deploy: | |
name: Deploy | |
runs-on: ${{ matrix.os }} | |
# push event in main branch | |
# workflow_dispatch event | |
# pull_request event from not forked repo | |
# pull_request_target event with label "🚀request-deploy" from forked repo | |
if: ${{ | |
github.event_name == 'push' || | |
github.event_name == 'workflow_dispatch' || | |
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false) || | |
(github.event_name == 'pull_request_target' && | |
github.event.action == 'labeled' && | |
github.event.pull_request.head.repo.fork == true && | |
contains(github.event.label.name, '🚀request-deploy')) | |
}} | |
strategy: | |
matrix: | |
os: [ubuntu-latest] | |
node: [18] | |
# Cancel previous runs that are not completed yet | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.pull_request.head.ref || github.ref }} | |
cancel-in-progress: true | |
timeout-minutes: 10 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.event.pull_request.head.sha || github.ref }} | |
fetch-depth: 0 | |
- name: Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node }} | |
- name: Get cache directory | |
id: npm-cache-directory | |
shell: bash | |
run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} | |
- name: Cache | |
id: npm-cache | |
uses: actions/cache@v3 | |
with: | |
path: ${{ steps.npm-cache-directory.outputs.dir }} | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Install dependencies | |
run: npm ci | |
- name: Run generate | |
run: npm run generate | |
- name: Publish to Cloudflare Pages | |
id: cloudflare-pages-deploy | |
uses: jaoafa/cloudflare-pages-action@main | |
with: | |
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
projectName: beta | |
directory: web | |
gitHubToken: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create commit comment | |
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | |
uses: peter-evans/commit-comment@v2 | |
with: | |
body: | | |
### ⚡ Successfully Cloudflare Pages deployed! | |
| Name | Link | | |
| :--- | :--- | | |
| <span aria-hidden="true">🔨</span> Latest commit | ${{ github.event.pull_request.head.sha || github.sha }} | | |
| <span aria-hidden="true">🔍</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
| <span aria-hidden="true">😎</span> Deploy Preview Url | [${{ steps.cloudflare-pages-deploy.outputs.url }}](${{ steps.cloudflare-pages-deploy.outputs.url }}) | | |
| <span aria-hidden="true">🌳</span> Environment | ${{ steps.cloudflare-pages-deploy.outputs.environment }} | | |
- name: Create PR comment | |
if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message: | | |
### ⚡ Successfully Cloudflare Pages deployed! | |
| Name | Link | | |
| :--- | :--- | | |
| <span aria-hidden="true">🔨</span> Latest commit | ${{ github.event.pull_request.head.sha || github.sha }} | | |
| <span aria-hidden="true">🔍</span> Latest deploy log | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | | |
| <span aria-hidden="true">😎</span> Deploy Preview Url | [${{ steps.cloudflare-pages-deploy.outputs.url }}](${{ steps.cloudflare-pages-deploy.outputs.url }}) | | |
| <span aria-hidden="true">🌳</span> Environment | ${{ steps.cloudflare-pages-deploy.outputs.environment }} | | |
- name: Remove label | |
if: ${{ github.event_name == 'pull_request_target' && contains(github.event.label.name, '🚀request-deploy') }} | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
github.rest.issues.removeLabel({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
name: ['🚀request-deploy'] | |
}) | |
# Comment on PR from the fork | |
comment: | |
name: Comment | |
runs-on: ubuntu-latest | |
# pull_request_target opened event from forked repo | |
if: ${{ | |
github.event_name == 'pull_request_target' && | |
github.event.action == 'opened' && | |
github.event.pull_request.head.repo.fork == true | |
}} | |
steps: | |
- name: Create PR comment | |
run: | | |
cat << EOF > comment.md | |
# ⚠️ Deployment requires \`🚀request-deploy\` label | |
For security reasons, deployments from forked repositories do not occur automatically. | |
To request a deployment, add the \`🚀request-deploy\` label to this pull request. | |
EOF | |
gh pr comment ${{ github.event.number }} -R ${{ github.repository }} -F comment.md | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |