Skip to content

Commit

Permalink
gha - simplify by using comment trigger
Browse files Browse the repository at this point in the history
This will avoid the context mess as the workflow should be run in the right context this time
  • Loading branch information
cderv committed May 27, 2024
1 parent 05af549 commit 95cbb7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 73 deletions.
55 changes: 4 additions & 51 deletions .github/workflows/port-to-prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ jobs:
github.event.comment.user.id != 41898282 &&
startsWith(github.event.comment.body, '/sync-prerelease')
)
outputs:
pr-created: ${{ steps.synced-pr.outputs.was_successful }}
pr-number: ${{ steps.synced-pr.outputs.created_pull_numbers }}
pr-sha: ${{ steps.new-pr-info.outputs.sha }}
steps:
- uses: actions/checkout@v4

Expand All @@ -54,57 +50,14 @@ jobs:
label_pattern: ''
target_branches: prerelease

- name: New PR info
id: new-pr-info
- name: Trigger deploy using a comment
if: steps.synced-pr.outputs.was_successful == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "sha=$(gh api /repos/quarto-dev/quarto-web/pulls/${{ steps.synced-pr.outputs.created_pull_numbers }} -q '.head.sha')" >> "$GITHUB_OUTPUT"
shell: bash

- name: "Create a check run on the new PR"
uses: actions/github-script@v7
with:
script: |
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: "${{ steps.new-pr-info.outputs.sha }}",
name: "Deploying preview",
status: "in_progress",
details_url: "https://github.com/quarto-dev/quarto-web/actions/runs/${{ github.run_id }}"
});
# Trigger the preview workflow if a new PR was created
# because PR created from bot does not trigger pull_request workflow
publish-changes-prerelease:
needs: [ sync-to-prerelease ]
if: >
needs.sync-to-prerelease.result == 'success' &&
needs.sync-to-prerelease.outputs.pr-created == 'true'
uses: ./.github/workflows/preview.yml
with:
pr-number: ${{ needs.sync-to-prerelease.outputs.pr-number }}
prerelease: true
secrets: inherit

# This update the status check on the PR to completed
update-status:
needs: [sync-to-prerelease, publish-changes-prerelease]
if: needs.publish-changes-prerelease.result == 'success'
runs-on: ubuntu-latest
steps:
- name: "Create a check run on the new PR"
uses: actions/github-script@v7
with:
script: |
await github.rest.checks.create({
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: "${{ needs.sync-to-prerelease.outputs.pr-sha }}",
name: "Deploying preview",
status: "completed",
conclusion: "${{ needs.publish-changes-prerelease.result }}"
details_url: "https://github.com/quarto-dev/quarto-web/actions/runs/${{ github.run_id }}"
issue_number: "${{ steps.synced-pr.outputs.created_pull_numbers }}",
body: "/deploy-preview",
});
42 changes: 20 additions & 22 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@ on:
branches: [main, prerelease]
issue_comment:
types: [created]
workflow_call:
# This should be triggered by port-to-prelease workflow to trigger a preview
inputs:
pr-number:
required: true
description: 'The PR to preview'
type: string
prerelease:
required: true
description: 'Is it for prerelease website ?'
type: boolean
default: true

name: Deploy Preview

concurrency:
# Use github.event.pull_request.number on pull requests, so it's unique per pull request
# Use github.ref on other branches, so it's unique per branch
group: ${{ github.workflow }}-${{ inputs.pr-number || github.event.pull_request.number || github.event.issue.number || github.ref }}
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.issue.number || github.ref }}
cancel-in-progress: true

jobs:
Expand All @@ -39,18 +27,28 @@ jobs:
# Deploy a preview only if
# - the PR is not from a fork,
# - requested by PR comment /deploy-preview
# - requested by workflow_call trigger (from sync to prerelease workflow)
if: |
(inputs.pr-number) ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork != true) ||
(github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'COLLABORATOR') && startsWith(github.event.comment.body, '/deploy-preview'))
if: >
(
github.event_name == 'pull_request' &&
github.event.pull_request.head.repo.fork != true
) ||
(
github.event.issue.pull_request &&
(
github.event.comment.user.login == 'octocat' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'COLLABORATOR'
) &&
startsWith(github.event.comment.body, '/deploy-preview')
)
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
ref: refs/pull/${{ inputs.pr-number || github.event.pull_request.number || github.event.issue.number }}/merge
ref: refs/pull/${{ github.event.pull_request.number || github.event.issue.number }}/merge

- name: Get latest pre-release from github
id: github-release
Expand All @@ -69,7 +67,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ "${{ inputs.prerelease }}" == "true" -o "${{ github.event.pull_request.base.ref }}" == "prerelease" ]; then
if [ "${{ github.event.pull_request.base.ref }}" == "prerelease" ]; then
echo "is_prerelease_docs=true" >> "$GITHUB_OUTPUT"
elif [ -n "${{ github.event.issue.pull_request.url }}" ]; then
# This will trigger when not pull request event, so a PR comment event.
Expand Down Expand Up @@ -101,8 +99,8 @@ jobs:
production-deploy: false
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: |
Deploy from GHA: ${{ (inputs.pr-number && format('auto from PR {0}', inputs.pr-number)) || github.event.pull_request.title || format('manual from PR {0}', github.event.issue.number) }}
alias: deploy-preview-${{ inputs.pr-number || github.event.pull_request.number || github.event.issue.number }}
Deploy from GHA: ${{ github.event.pull_request.title || format('manual from PR {0}', github.event.issue.number) }}
alias: deploy-preview-${{ github.event.pull_request.number || github.event.issue.number }}
# these all default to 'true'
enable-pull-request-comment: true
enable-commit-comment: false
Expand Down

0 comments on commit 95cbb7a

Please sign in to comment.