Trigger an action on push or pull request but not both - github-actions

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.

Related

github action check if first commit to a branch before doing the action

Currently using the Submit Pull Request where it will automatically create a pull request for the branch but I wanted to it to run only for the first time I committed a branch, how do I stop it from running in other times?
name: create-pull-request workflow
on:
push:
branches:
- '*'
- '!master'
jobs:
SubmitPullRequest:
runs-on: ubuntu-latest
steps:
- name: Submit Pul Request
uses: shimewtr/submit_pull_request#master
env:
ASSIGN: false
GITHUB_ACCESS_TOKEN: ${{secrets.github_token}}

Github actions How to only run a step when merging into master

I have been trying to get this right for hours, and nothing I have managed to find has helped. I am trying to setup a github action that will run tests on every pull request into master and any changes to the master branch, but only run the deploy step when there are changes to the master branch.
Here is a simple reproduction of what I am trying to do.
name: Main
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Test
run: echo "running tests"
- name: Deploy
run: echo "Deploying"
if: github.head_ref == 'main'
I have tried multiple conditionals I have found here / on other forums, I have tried moving the if statement above and below run I am completely out of ideas. Everything I have tried either runs the deploy step on both pull request and merge or skips the deploy step on both pull request and merge.
Just use:
github.ref == 'refs/heads/master'
or:
github.ref == 'refs/heads/main'
Depending on which branch you want to check
This is what I do:
build:
runs-on: [...]
steps:
- name: Echo on merging to master
if: ${{ github.ref == 'refs/heads/master' }}
run: echo "I'm merging!"

Github Action workflow_run only on push to main branch

I have a CI workflow that runs on PR and PUSH to main branch.
---
name: CI
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
I have another workflow I'd like to only run after CI is complete and conclusion is success but only when it's pushed to main branch.
---
name: Build
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
build:
name: Build
runs-on: self-hosted
if: ${{ github.event.workflow_run.conclusion == 'success' }}
It runs on both PR and push to main. How do I get the Build workflow to only run on push to main?
It looks like you can just filter on the branch in the Build workflow (see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#limiting-your-workflow-to-run-based-on-branches):
on:
workflow_run:
workflows: ["CI"]
types:
- completed
branches:
- main

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:
...

Github action jobs triggered by different events

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:
...