Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Workflow Dispatch running on branches other than main

I have made a CI/CD pipeline for a personal project. I have used the following to try to run the pipeline only on the main branch. The documentation suggests using workflow dispatch for this purpose and I have used the following snippet to get this behavior.

on:
  workflow_dispatch:
    branches: 
      - main

However, when I go to the Github actions screen to run the pipeline manually, I can also try to run it on the other branches. Ideally, I would like to run this job (publishing) only on the main and nothing else.

Is there any way I can enforce this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For reference, the pipeline is part of this YML. You can also see the publish job running on Test branch on the Actions screen.

>Solution :

You can add a if: github.ref == 'refs/heads/master' to your job that’ll prevent it from running on a given branch.

Or like if: startsWith(github.ref, 'refs/heads/release') if you want to only run on your release branches

Example:

jobs:
  Setup:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      ...

Alternatively you might also be able to use the workflow_run syntax, see https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#limiting-your-workflow-to-run-based-on-branches

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading