Github workflow executing unexpextedly - github

Ok, so I'm trying to get a workflow to execute on a specific branch, with a specific tag.
So I wrote this:
on:
push:
branches:
- 'branchname'
tags:
- 'tagname'
with the expectation that it would run whenever something on the branchname branch was taged with tagname. Instead it seems like it is being run on master whenever something is pushed, regardless of tags. Can someone explain why?

Related

GitHub Actions Workflow Not Triggering

I have a project in which I have two yml files under .github/workflows/ as below:
build.yml
release.yml
I use annotated tags to do releases and here is how the trigger looks like in build.yml:
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
tags:
- 'v*.*.*'
pull_request:
branches:
- master
And here is how it looks like in release.yml:
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- '[0-9]+.[0-9]+.[0-9]+'
I did the following to push a new annotated tag:
git tag -a v0.0.3-SNAPSHOT -m "My very third tag with release"
git push origin --tags
I was actually expecting my release.yml to get triggered, but it does not. Is there anything that I'm missing?
The regex will not match your tag 'v0.0.3-SNAPSHOT'. Missing the 'v' and the trailing text section. You could match it with the following:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
Example can be found here. Not sure why you cant use the '.*' as any character any number of times.
See working example here -> https://github.com/jnus/trigger-semver-tags/blob/main/.github/workflows/workflow.yml

How to exclude certain branches for a path pattern within Github Actions?

How to apply branches-ignore and paths-ignore on GitHub Actions? For example, if I have the following source tree:
|-src\
|----reports\
|-------<foo>DailyReports.ts
I want to only deploy that to production (master branch) but NOT staging (staging branch).
Any advice and insight is appreciated.
You won't be able to achieve what you want with ON trigger configuration alone. Therefore the implementation below wouldn't work as it would trigger for push to the specified path OR for push to the master branch, but not only for both:
on:
push:
path:
- 'src/reports/*DailyReports.ts'
branches:
- master
In that case, you could do it by using only one trigger at the workflow level, and check the other condition with an IF expression.
For example by doing this:
on:
push:
path:
- 'src/reports/*DailyReports.ts'
jobs:
job1:
runs-on: ...
if: github.ref == 'refs/heads/master' # run this job only for the master branch
steps:
...
Or by checking the branch at the workflow level, and then use something like this path-filter action to check if the file or directory has been updated or not before performing some operation.
I personally suggest the first option (cf example), as it's less verbose.
Note: If the src/reports/*DailyReports.ts path isn't working as expected, you can use the filter pattern cheat sheet to find the right expression.

Run Github Action only when a push to main comes from a branch with a certain name

I have a Github Action that deploys my app from the main branch when there is a push to it or a merge from a pull request:
'on':
push:
branches:
- main
jobs:
build_and_deploy:
What I am trying to accomplish is to only run the action if the pull request comes from a branch that does not contain the string "nodeploy/" in its name.
I tried this:
'on':
push:
branches:
- main
jobs:
build_and_deploy:
if: ${{ !startsWith(github.head_ref, 'nodeploy/') }}
but it didn't work.
I believe it doesn't work because the value of github.head_ref is always main in this case.
Are there any solutions to this?
I found a solution. As suggested in the comments, I checked the message in the head commit.
'on':
push:
branches:
- main
jobs:
build_and_deploy:
if: ${{ !contains(github.event.head_commit.message, 'nodeploy/') }}
And now it does what I wanted.
Be aware that if you change the automatically generated merge message it won't work.

Mark a conditional Github Action as required

I have a Github action in my repository that runs only when a specific file has changed. I have marked this action as required in repository settings so that whenever it fails the automatic merge gets blocked. Merge block is working fine but if a PR doesn't include a change to that specific file, Github still blocks the PR waiting for the job to complete which won't even run. Ideally, Github should ignore this action if it is not applicable.
I have put up a public repository that showcases this behavior. There are two PRs open: one that is working fine and the other one is blocked.
Any suggested workaround for this?
A workaround could be to update the workflow to run independently of the path, but check the path in the job execution with something like the path filter action, then return the message you want on the PR if the label isn't set and set an error to block the merge, or return that everything is ok if the file hasn't been updated.
Your workflow would in that case looks like below:
name: All Checked Verifier
on:
pull_request:
types: [labeled, unlabeled, opened, edited, synchronize]
jobs:
enforce-label:
runs-on: ubuntu-latest
steps:
- uses: dorny/paths-filter#v2
id: changes
with:
filters: |
public_api_views:
- '**/code.py'
- if: steps.changes.outputs.public_api_views == 'true'
uses: yogevbd/enforce-label-action#2.1.0
with:
REQUIRED_LABELS_ALL: "checked-everything"
REQUIRED_LABELS_ALL_DESCRIPTION: "Make sure we have checked everything and once done, add label 'checked-everything' to this PR."
It's weird because the https://github.com/sferhan/hello-github-actions/pull/6 not excuting the workflow
and i forked the repo https://github.com/bxb100/hello-github-actions, it's work fine... so i guess the action have problem.

How to trigger GitHub actions on push of current branch?

I have created the following GitHub Actions workflow.
name: Testing CI
on:
push:
branches: [ my-branch ]
Here in branches: [my-branch] I am putting the name of the current branch.
But I want the workflow file to automatically take the name of the current branch.
Is there a parameter I can pass branches: [my-branch] here to automatically take the branch name?
You should not specify a branch name, then, in order for your action to run on push for all branches: on: [push]
Then you can use EthanSK/git-branch-name-action in order to get the current branch name.
That action uses process.env.GITHUB_REF,
and is the equivalent of this solution:
export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\\///g).
Just use this And it will run on push of any branch:
on: [push]