Github action jobs triggered by different events - github-actions

My release pipeline in Github action has two jobs (1) Testing (2) Release
This is what I am aim to achieve:
The testing job of the release pipeline will be triggered by creating a pull-request, the release job of the pipeline will be rung when pull-request is merged
name: OC-API CD
on:
pull_request:
types: [merged]
branches:
- master
jobs:
testing:
runs-on: ubuntu-latest
steps:
...
release:
runs-on: ubuntu-latest
steps:
...
It seems all jobs has to be triggered by the same events via on,
(1) Is there any way that different job can be triggered by different events?
(2) How can I add dependency between testing and release, AKA, the release job is depend on the successfully runs the testing job

You can control each job by adding if, in your case it will be like this:
jobs:
testing:
if: ${{ github.event.action }} == 'opened'
runs-on: ubuntu-latest
steps:
...
Keep in mind you have to add opened to a on: pull_request: types: array of course :)
For the second part you can add dependency using needs, like this:
jobs:
testing:
runs-on: ubuntu-latest
steps:
...
release:
needs: testing
runs-on: ubuntu-latest
steps:
...

Related

Run a certain job depending on the source branch from a PR

I have a GitHub action workflow for a swift package that runs whenever something is merged to the main branch.
My goal is to check the for a source branch and depending on its prefix (i.e. patch/something) determine wow to tag and release it.
This is how I have it configured so far:

name: Release
on:
push:
branches:
- main
jobs:
build:
runs-on: macos-latest
patch-release-on-push:
needs: build
if: contains(github.head_ref, 'patch')
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- id: release
uses: rymndhng/release-on-push-action#master
with:
bump_version_scheme: patch
The problem with this is that github.head_ref is null. I only get a value on it if I switch the workflow to be triggered on a pull_request instead of push which wouldn’t really work given that the release would be out before the PR was merged.

What’s the right approach for achieving this?


I would suggest you to use an existing action like EthanSK/git-branch-name-action, as example:
on: [push, pull_request]
jobs:
main_job:
runs-on: ubuntu-latest
steps:
- name: Git branch name
id: git-branch-name
uses: EthanSK/git-branch-name-action#v1
- name: Echo the branch name
run: echo "Branch name ${GIT_BRANCH_NAME}"
and use also like
if: ${{ contains(env.GIT_BRANCH_NAME, 'patch') }}
Link to the marketplace here

Trigger an action on push or pull request but not both

I have this workflow : (not complet)
# Only one deploy/publish can run
concurrency:
group: lydie-front-deploy
cancel-in-progress: true
on:
pull_request:
branches:
- dev
types:
- closed
push:
branches:
- dev
jobs:
build:
runs-on: ubuntu-latest
if: github.event.pull_request.merged || github.event_name == 'push'
Problem : when I valid a pull_request, and merge it, the push event come just after and I have two actions running.
If there a way to run my workflow if I had a pull_request (merged) or a push but not both? I've checked the concurrency option but it's not what I need : I don't want to cancel a previous jobs.

Prevent GitHub Action to run for each tag on a commit

I have a publish workflow that is supposed to push the dists to PyPi if a commit on main is tagged with either frontend-v* or backend-v* (both are separate packages)
However, if a commit has changes on both the front- and backend and I add two tags to the commit, the workflow is triggered twice.
I understand that I could simply split the workflow into two, but I have another job that should run if either frontend or backend or both were updated which should only be ran once, thus I want to keep this in one workflow.
Can I somehow circumvent this to run this only once?
Thank you very much!
on:
workflow_dispatch:
push:
branches: [ main ]
tags:
- frontend*
- backend*
jobs:
backend:
name: Publish backend
if: ${{contains(github.ref_name, 'backend') }}
runs-on: ubuntu-latest
steps:
...
frontend:
name: Publish frontend
if: ${{contains(github.ref_name, 'frontend') }}
runs-on: ubuntu-latest
steps:
...
housekeeping: # this should only be ran once for the commit
name: House Keeping
runs-on: ubuntu-latest
steps:
...

Trigger GitHub Action Workflow By Pull Request

I'd like to trigger automatically a GitHub Action Workflow for each [assigned, opened, synchronize, reopened] Git Pull Request from side-branch into master.
In addition, Is there a way to checkout the code from the private repository of a PR via branch name?
Below you can find my GitHub Action workflow that I've tried but unfortunately when I open a PR from side-branch into master it didn't trigger at all.
on:
workflow_dispatch:
pull_request:
types: [assigned, opened, synchronize, reopened]
branches:
- master
jobs:
build-image:
name: Build Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
with:
ref: ${{github.event.pull_request.head.sha}}
Is it possible, how can I do that?

Does Github Actions have templates

As I have repetitve steps in my Github Actions, I would like to create a template. Let's make a example
name: ci
on: ["push"]
jobs:
build-and-test:
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: checkout
uses: actions/checkout#v1
- name: do stuff
run: |
bash stuff
Is it possible to save only the steps in a separated file? And import afterwards?
Unfortunately it does not look like github-actions supports reusing workflows. Not even YAML anchors are supported.
It looks like the only way to share steps (not setup) is to create actions.
Update: A storm brewing
I have also caught wind of the possibility of reusing actions. Follow the issue to stay up-to-date.
I mentioned in "Reuse portion of GitHub action across jobs" that reusing GitHub Worfflow is now (Oct. 2021) available.
The documentation "Reusing workflows" includes a section "Reusable workflows and workflow templates", which leads to "Creating workflow templates"
If you need to refer to a repository's default branch, you can use the $default-branch placeholder.
When a workflow is created using your template, the placeholder will be automatically replaced with the name of the repository's default branch.
For example, this file named octo-organization-ci.yml demonstrates a basic workflow.
name: Octo Organization CI
on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Run a one-line script
run: echo Hello from Octo Organization