Home > Mobile >  Running Github Action When PR is raised from specific branch name to specific branch name?
Running Github Action When PR is raised from specific branch name to specific branch name?

Time:03-16

We have a public SDK repo for which I am writing multiple github workflows. One such workflow includes Generating a Release SDK(minified, encoded with private keys) when a PR is raised from develop to master. another such action is to run static code checks when a PR is raised from task/** branch to develop branch. I tried to use the following workflow:

name: validate PR raised from task/** branched to develop branch
on:
  pull_request:
    branches: [ task/** ]

  pull_request_target:
    branches: [ develop ]
  ...

this code is pushed to develop branch. but this triggers the workflow twice everytime a PR is raised or modified, indicating that the action is running with 'OR' configuration. I would like to run this workflow only once in 'AND' condition (i.e when the source branch is task/** AND target branch is develop)

For my first usecase, it is very important that only internal devs can trigger the generate build. so is that possible to run the actions for specific target and source branch?

CodePudding user response:

You can trigger the workflow for a pull request to the base branch and further refine the jobs to be run based on the head branch. Therefore, you can use the information available in github.head_ref and evaluate it in the if conditional. As a result, the job gets skipped whenever the expression does not match. Here are the two workflows that match your description:

build-sdk.yml

name: Build-SDK

on:
  pull_request:
    branches: [ master ]

jobs:
  build-sdk:
    if: github.head_ref == 'develop'
    runs-on: ubuntu-latest  
    steps:
      - run: echo "Build SDK..."

validate.yml:

name: Validate

on:
  pull_request:
    branches: [ develop ]

jobs:
  validate:
    if: startsWith(github.head_ref, 'task/')
    runs-on: ubuntu-latest  
    steps:
      - run: echo "Validate..."
  • Related