Github Action workflow_run only on push to main branch - github-actions

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

Related

GitHub triggers pull_request but it is not expected

I have a workflow, main.yml:
name: Build Main
on:
push:
branches: main
pull_request:
branches:
- main
types: [opened, synchronize, reopened, labeled]
I also have this other one, feature.yml:
name: Build Feature
on:
push:
branches: feature/**
When I'm on a feature branch (feature/NN-my-feature), and I push a commit from Visual Studio Code, both the workflows run.
Looking at the Actions in GitHub, the main.yml is triggered by a "pull_request".
I don't understand why/how the pull_request on "main" is triggered.

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

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 action rebase running twice

I want github action to rebase my branch onto main as soon as a PR is created or updated from feature branch onto main branch.
But this rebase workflow ends up running twice.
Following is the code:
name: Rebase on main branch
on:
pull_request:
branches: [ main ]
jobs:
rebase:
name: Rebase on main branch
runs-on: ubuntu-latest
steps:
- name: git checkout
uses: actions/checkout#v2
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
fetch-depth: 0
- name: automatic rebase
uses: cirrus-actions/rebase#1.5
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
I have another test workflow that runs on successful rebase. Since rebase runs twice, test also runs twice which is not the intended behavior.
How do I make rebase run only once?
Or is there some trigger that can tell me if PR(create or update) + rebase happened?

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?