Reputation: 43
New to GitHub actions, I have a repository with 3 branches (test, QA, PROD).
I use a dispatch workflow that calls a specific workflow located in different branches depending on the name of the server that is calling the repository (e.g .if using the test server, it will call the test workflow).
name: Workflow Dispatch
on:
repository_dispatch:
types:
[deployment]
jobs:
deploy-main:
if: github.event.client_payload.ref == 'main'
uses:repository_name/.github/workflows/deploy_workflow.yml@main
deploy-qa:
if: github.event.client_payload.ref == 'qa'
uses: repository_name/.github/workflows/deploy_workflow.yml@qa
deploy-qa:
if: github.event.client_payload.ref == 'test'
uses: repository_name/.github/workflows/deploy_workflow.yml@test
The deploy workflow below will be triggered in each branch. Only the last line (run: .\scripttest.ps1 $env:resource_contact $env:var1
) differs.
Each branch has its own script, scripttest.ps1 (test), scriptqa.ps1 (QA), and scriptmain.ps1 (PROD).
name: workflow deploy
on:
workflow_call:
jobs:
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.client_payload.ref }}
- name: deployment
env:
var1: ${{ github.event.client_payload.var1}}
var2: ${{ github.event.client_payload.var2 }}
shell: pwsh
run: .\scripttest.ps1 $env:resource_contact $env:var1
My problem is that no matter what branch I am, the PROD script runs (scriptmain.ps1). I could create an action for each branch and run the code on each action. But the code is too long and it would be difficult to read/understand.
I tried using the filter "branches" as suggested in the GitHub documentation. But this does not work as it seems to be an option for on-push, on-pull methods only.
on:
pull_request:
# Sequence of patterns matched against refs/heads
branches:
- main
- 'mona/octocat'
- 'releases/**'
Is there anything to run the script for a specific branch?
Upvotes: 1
Views: 2774
Reputation: 43
I found the answer. I was using actions/checkout@v2
in the workflow deploy.
To set the context for the runner, I needed to use actions/checkout@v3
.
https://github.com/actions/checkout
- uses: actions/checkout@v3
with:
# The branch, tag or SHA to checkout. When checking out the repository that
# triggered a workflow, this defaults to the reference or SHA for that event.
# Otherwise, uses the default branch.
ref: ''
If I am running the workflow deployment in QA now, it will gather all the files in the QA branch and run scriptqa.ps1
.
Upvotes: 0