Skip to content

Commit bca8d6e

Browse files
committed
Update to allow specifying PR to deploy
1 parent 89e005d commit bca8d6e

File tree

2 files changed

+107
-16
lines changed

2 files changed

+107
-16
lines changed

.github/readme.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Developing and Testing Github Actions
2+
3+
Testing Github Actions on an existing repository is tricky.
4+
5+
The main issue boils down to the fact that Github Actions uses the workflow files in the branch where the event originates. This is fine for push events, but it becomes a problem when you want to test workflows that are triggered by comments on a pull request.
6+
7+
Here's a summary of the behavior:
8+
9+
Behavior of push and pull_request Events
10+
1. Push on a Branch:
11+
• When you push changes to a branch (e.g., feature-branch), GitHub Actions uses the workflow files in that same branch.
12+
• This is why changes to workflows work seamlessly when testing with push events.
13+
2. Pull Request Events:
14+
• For pull_request events (e.g., a PR from feature-branch into master), GitHub Actions will always use the workflow files from the target branch (e.g., master), not the source branch (e.g., feature-branch).
15+
• This is a security feature to prevent someone from introducing malicious code in a PR that modifies the workflow files themselves.
16+
17+
Impact on Comment-Triggered Workflows
18+
19+
When you want to trigger workflows via comments (issue_comment) in a pull request:
20+
• The workflow code used will always come from the master branch (or the default branch), regardless of the branch where the PR originates.
21+
• This means the PR’s changes to the workflow won’t be used, and the action invoked by the comment will also use code from master.
22+
23+
Workarounds to Test Comment-Triggered Workflows
24+
25+
If you want to test workflows in a way that uses the changes in the pull request, here are your options:
26+
27+
1. Use Push Events for Testing
28+
• Test your changes on a branch with push triggers.
29+
• Use workflow_dispatch to simulate the events you need (like invoking actions via comments).
30+
31+
This allows you to confirm that your changes to the workflow file or actions behave as expected before merging into master.
32+
33+
2. Merge the Workflow to master Temporarily
34+
35+
If you absolutely need the workflow to run as part of a pull_request event:
36+
1. Merge your workflow changes into master temporarily.
37+
2. Open a PR to test your comment-triggered workflows.
38+
3. Revert the changes in master if necessary.
39+
40+
This ensures the workflow changes are active in master while still testing with the pull_request context.
41+
42+
3. Add Logic to Detect the Source Branch
43+
44+
Use github.event.pull_request.head.ref to add custom logic in your workflow that behaves differently based on the source branch.
45+
• Example:
46+
47+
jobs:
48+
test-pr:
49+
runs-on: ubuntu-latest
50+
if: ${{ github.event.pull_request.head.ref == 'feature-branch' }}
51+
steps:
52+
- name: Checkout Code
53+
uses: actions/checkout@v3
54+
55+
- name: Debug
56+
run: echo "Testing workflow changes in feature-branch"
57+
58+
However, this still requires the workflow itself to exist in master.
59+
60+
4. Use a Fork or a Temporary Repo
61+
62+
Create a temporary repository or a fork to test workflows in isolation:
63+
• Push your workflow changes to master in the test repository.
64+
• Open a PR in the fork to test how workflows behave with issue_comment events and PR contexts.
65+
66+
Once confirmed, you can replicate the changes in your main repository.
67+
68+
6. Alternative Approach: Split Workflows
69+
70+
If your workflow includes comment-based triggers (issue_comment), consider splitting your workflows:
71+
• A base workflow in master that handles triggering.
72+
• A test-specific workflow for validating changes on a branch.
73+
74+
For example:
75+
1. The base workflow triggers when a comment like /run-tests is added.
76+
2. The test-specific workflow runs in response to the base workflow but uses the branch’s code.
77+
78+
Summary
79+
• For push events: The branch-specific workflow is used, so testing changes is easy.
80+
• For pull_request and issue_comment events: GitHub always uses workflows from the master branch, and there’s no direct way to bypass this.
81+
82+
To test comment-triggered workflows:
83+
1. Use push or workflow_dispatch to validate changes.
84+
2. Merge workflow changes temporarily into master to test with pull_request events.
85+
3. Use tools like act for local simulation.

.github/workflows/deploy-to-control-plane.yml

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
name: Deploy Review App to Control Plane
22

3-
run-name: ${{ github.event_name == 'issue_comment' && 'Deploying Review App' || format('Updating Review App for {0}', github.ref_name) }}
3+
run-name: ${{ github.event_name == 'workflow_dispatch' && format('Deploying Review App for PR #{0}', github.event.inputs.pr_number) || github.event_name == 'issue_comment' && 'Deploying Review App' || format('Updating Review App for {0}', github.ref_name) }}
44

55
on:
66
pull_request:
77
types: [opened, synchronize, reopened]
88
issue_comment:
99
types: [created]
10+
workflow_dispatch:
11+
inputs:
12+
pr_number:
13+
description: 'Pull Request number to deploy'
14+
required: true
15+
type: number
1016

1117
# Use concurrency to cancel in-progress runs
1218
concurrency:
13-
group: deploy-pr-${{ github.event.pull_request.number || github.event.issue.number }}
19+
group: deploy-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
1420
cancel-in-progress: true
1521

1622
env:
17-
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || github.event.issue.number }}
23+
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
1824
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }}
1925
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }}
20-
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}
26+
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number || github.event.inputs.pr_number }}
2127

2228
jobs:
2329
Process-Deployment-Command:
2430
if: |
2531
(github.event_name == 'pull_request') ||
32+
(github.event_name == 'workflow_dispatch') ||
2633
(github.event_name == 'issue_comment' &&
2734
github.event.issue.pull_request &&
2835
github.event.comment.body == '/deploy-review-app')
@@ -34,6 +41,17 @@ jobs:
3441
issues: write
3542

3643
steps:
44+
- name: Get PR HEAD Ref
45+
if: github.event_name == 'issue_comment' || github.event_name == 'workflow_dispatch'
46+
id: getRef
47+
run: |
48+
echo "PR_NUMBER=${{ github.event.issue.number || github.event.inputs.pr_number }}" >> $GITHUB_ENV
49+
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ github.event.issue.number || github.event.inputs.pr_number }}" >> $GITHUB_ENV
50+
# Get the PR head commit
51+
PR_DATA=$(gh pr view ${{ github.event.issue.number || github.event.inputs.pr_number }} --repo ${{ github.repository }} --json headRefName,headRefOid)
52+
echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT
53+
echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV
54+
3755
- uses: actions/checkout@v4
3856
with:
3957
fetch-depth: 0
@@ -45,18 +63,6 @@ jobs:
4563
token: ${{ env.CPLN_TOKEN }}
4664
org: ${{ env.CPLN_ORG }}
4765

48-
- name: Get PR HEAD Ref
49-
if: github.event_name == 'issue_comment'
50-
run: |
51-
echo "PR_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
52-
echo "APP_NAME=qa-react-webpack-rails-tutorial-pr-${{ github.event.issue.number }}" >> $GITHUB_ENV
53-
# For PR comments, get the actual PR head commit
54-
PR_DATA=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName,headRefOid)
55-
echo "PR_REF=$(echo "$PR_DATA" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
56-
echo "PR_SHA=$(echo "$PR_DATA" | jq -r '.headRefOid')" >> $GITHUB_OUTPUT
57-
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59-
6066
- name: Check if Review App Exists
6167
id: check-app
6268
if: github.event_name == 'push'

0 commit comments

Comments
 (0)