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

Viewed 28

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?



1 Answers

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

Related