Github actions on pull request and master branch - github

Github actions is still in beta and pretty new, but I hope someone can help regardless. I thought it would be possible to run github actions on the master branch and on pull requests, like this:
on:
pull_request
push:
branches: master
But this doesn't work, and throws the error
yaml: line 4: mapping values are not allowed in this context
. Instead I can only get it to work like this:
on: [pull_request, push]
What am I doing wrong? Thanks.

I think you are just missing a colon after pull_request. This works for me.
on:
pull_request:
push:
branches: master
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Test
run: echo "done"

Explanation
Each trigger has to be defined as a property that defines an object.
Each object defines overrides for default settings.
There are 3 possible syntax you can use:
Minimal syntax:
on:
pull_request:
push: { branches: [master] }
Explicit syntax:
on:
pull_request: {}
push: { branches: [master] }
Extendable syntax:
on:
pull_request:
push:
branches:
- master
When using a version control system the latter may be most useful as diff viewers can always easily distinguish* between different lines.
*Although modern diff viewers can also easily distinguish inline differences.

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

Block merge if github actions are failed

I have setup Github action for python app, it's located at
.github/workflows/python-app.yml
with details as
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
...
This executes fine however, We want to block if actions fails,
I'm unable to search this status under
Status checks that are required.
It was issue of name inside build which was not there in template
jobs:
build:
name: syntax-check
runs-on: ubuntu-latest

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.

Github Actions: using workflow_run based on new tags

I have two workflows: CI (for continuous integration) and CD (for continuous delivery). Both are working fine individually. My goal is to run the CD workflow only when:
A new tag like v1.1.1 is created on the master branch
The CI workflow is finished
To achieve my goal I'm using the workflow_run event. These are the snippets of my workflows files:
ci.yml:
name: CI
on:
push:
tags: v[1-9]+.[0-9]+.[0-9]+
pull_request:
branches: [develop, hotfix*]
cd.yml
name: CD
on:
workflow_run:
workflows: [CI]
branches: [master]
types:
- completed
The current behavior is: when a tag is created in the master branch only the CI workflow run. I've tried putting tags: v[1-9]+.[0-9]+.[0-9]+ in the workflow_run but the behavior is the same.
My question is: how can I achieve my goal? Is it possible?
According to docs you can only use branches option and not tags for workflow_run so I'm afraid that's why your current setting doesn't work.
You have some alternatives though:
You can turn your CD workflow into action and run it as part of your CI with condition:
.github/actiond/cd/action.yml:
name: CD
description: Run CD
runs:
using: composite
steps:
- run: echo "Success!"
shell: bash
CI:
name: CI
on:
push:
tags: v[1-9]+.[0-9]+.[0-9]+
pull_request:
branches: [develop, hotfix*]
jobs:
sucess:
name: Log success
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: echo "Success!"
- name: Run CD
if: github.event_name == 'push' && contains(github.event.ref, '/tags/')
uses: ./.github/actions/cd
Have it as a separate job that is dependant on CI job using needs option
Converting it to action makes for better encapsulation IMO although requires some work.
You need to put "" around the name of the triggering workflow in cd.yml:
name: CD
on:
workflow_run:
workflows: ["CI"]

Github Actions - How do you trigger a push when a specific directory in a branch gets an update?

I want my workflow to trigger on a push only if there are changes made to a specific directory. Is this possible, what am I missing? I tried doing something like what you see below, but it didn't fire the trigger.
name: ABC
on:
push:
branches: [master/my-directory]
pull_request:
branches: [ master ]
push has a property called paths:
name: ABC
on:
push:
branches:
- master
paths:
- my-directory/**
This will only trigger on pushes to the master branch with changes in the my-directory directory tree. See the filter pattern cheat sheet for all possible filter patterns.