Skip to content

Commit ebbc8d8

Browse files
authored
More work on actions (#622)
1 parent 6add086 commit ebbc8d8

File tree

6 files changed

+245
-170
lines changed

6 files changed

+245
-170
lines changed

.github/actions/deploy-to-control-plane/action.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ inputs:
1717
description: 'Timeout in seconds for waiting for workloads to be ready'
1818
required: false
1919
default: '900'
20+
cpln_token:
21+
description: 'Control Plane token'
22+
required: true
23+
pr_number:
24+
description: 'Pull Request number'
25+
required: true
2026

2127
outputs:
2228
review_app_url:
@@ -50,11 +56,14 @@ runs:
5056
run: ${{ github.action_path }}/scripts/get-commit-sha.sh
5157
env:
5258
GITHUB_TOKEN: ${{ inputs.github_token }}
53-
PR_NUMBER: ${{ env.PR_NUMBER }}
59+
PR_NUMBER: ${{ inputs.pr_number }}
5460

5561
- name: Deploy to Control Plane
5662
id: deploy
5763
shell: bash
64+
env:
65+
CPLN_TOKEN: ${{ inputs.cpln_token }}
66+
PR_NUMBER: ${{ inputs.pr_number }}
5867
run: |
5968
echo "🚀 Deploying app for PR #${PR_NUMBER}..."
6069

.github/actions/help-command/action.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ inputs:
55
github-token:
66
description: 'GitHub token for posting comments'
77
required: true
8+
issue-number:
9+
description: 'PR/Issue number to post the comment to (optional, defaults to event context)'
10+
required: false
811

912
runs:
1013
using: "composite"
1114
steps:
1215
- name: Show Available Commands
13-
uses: actions/github-script
16+
uses: actions/github-script@v7
1417
with:
1518
github-token: ${{ inputs.github-token }}
1619
script: |
@@ -80,12 +83,14 @@ runs:
8083
'3. Open an issue in this repository',
8184
].join('\n');
8285
83-
const context = github.context;
84-
if (context.eventName === 'issue_comment') {
86+
const issueNumber = inputs['issue-number'] ||
87+
(context.eventName === 'issue_comment' ? context.payload.issue.number : null);
88+
89+
if (issueNumber) {
8590
await github.rest.issues.createComment({
8691
owner: context.repo.owner,
8792
repo: context.repo.repo,
88-
issue_number: context.payload.issue.number,
93+
issue_number: issueNumber,
8994
body: helpText
9095
});
9196
} else {

.github/actions/setup-environment/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ runs:
2222
- name: Install Control Plane CLI and cpflow gem
2323
shell: bash
2424
run: |
25-
sudo npm install -g @controlplane/[email protected].0
25+
sudo npm install -g @controlplane/[email protected].1
2626
cpln --version
2727
gem install cpflow -v 4.1.0
2828
cpflow --version

.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.

0 commit comments

Comments
 (0)