Skip to content
Latchkey

PR to master branch from patch/release branch only workflow (theislab/ncem)

The PR to master branch from patch/release branch only workflow from theislab/ncem, explained and optimized by Latchkey.

F

CI health: F - at risk

Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.

Grade your own workflow free or run it on Latchkey →
Source: theislab/ncem.github/workflows/main_master_branch_protection.ymlLicense BSD-3-ClauseView source

What it does

This is the PR to master branch from patch/release branch only workflow from the theislab/ncem repository, a real project running GitHub Actions. It is shown here with attribution under its BSD-3-Clause license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
name: PR to master branch from patch/release branch only

on:
    pull_request:
        branches:
            - master
            - main

jobs:
    check_target:
        runs-on: ubuntu-latest
        name: Check Target branch
        steps:
            # PRs to the repository master branch are only ok if coming from any patch or release branch
            - name: Check PRs
              run: |
                  { [[ $GITHUB_HEAD_REF = *"release"* ]]; } || [[ $GITHUB_HEAD_REF == *"patch"* ]]

            # If the above check failed, post a comment on the PR explaining the failure
            # NOTE - this may not work if the PR is coming from a fork, due to limitations in GitHub actions secrets
            - name: Post PR comment
              if: failure()
              uses: mshick/add-pr-comment@v1
              with:
                  message: |
                      Hi @${{ github.event.pull_request.user.login }},

                      It looks like this pull-request is has been made against the ${{github.event.pull_request.head.repo.full_name}} `master` or `main` branch.
                      The `master`/`main` branch should always contain code from the latest release.
                      Because of this, PRs to `master`/`main` are only allowed if they come from any ${{github.event.pull_request.head.repo.full_name}} `release` or `patch` branch.

                      You do not need to close this PR, you can change the target branch to `development` by clicking the _"Edit"_ button at the top of this page.

                      Thanks again for your contribution!
                  repo-token: ${{ secrets.GITHUB_TOKEN }}
                  allow-repeats: false

    check_version:
        name: No SNAPSHOT version on master branch
        runs-on: ubuntu-latest
        steps:
            - name: Set up Python
              uses: actions/setup-python@v4.3.0
              with:
                  python-version: "3.8"
            # PRs to the repository master branch are only ok if coming from any patch or release branch
            - name: Install mlf-core
              run: pip install mlf-core

            - name: Check project version
              run: |
                  PROJECTVERSION=$(mlf-core bump-version --project-version . | tail -n1)
                  echo $PROJECTVERSION;
                  if [[ $PROJECTVERSION == *"SNAPSHOT"* ]];then
                      exit -1
                  else
                      exit 0
                  fi

            # If the above check failed, post a comment on the PR explaining the failure
            # NOTE - this may not work if the PR is coming from a fork, due to limitations in GitHub actions secrets
            - name: Post PR comment
              if: failure()
              uses: mshick/add-pr-comment@v1
              with:
                  message: |
                      Hi @${{ github.event.pull_request.user.login }},

                      It looks like this pull-request is has been made against the ${{github.event.pull_request.head.repo.full_name}} `master`/`main` branch.
                      A version check determined that you are using a SNAPSHOT version.
                      The `master`/`main` branch should never have any SNAPSHOT versions, since only fully stable code should be on the `master`/`main` branch.
                  repo-token: ${{ secrets.GITHUB_TOKEN }}
                  allow-repeats: false

The same workflow, on Latchkey

Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.

name: PR to master branch from patch/release branch only
 
on:
    pull_request:
        branches:
            - master
            - main
 
concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: true
 
jobs:
    check_target:
        timeout-minutes: 30
        runs-on: latchkey-small
        name: Check Target branch
        steps:
            # PRs to the repository master branch are only ok if coming from any patch or release branch
            - name: Check PRs
              run: |
                  { [[ $GITHUB_HEAD_REF = *"release"* ]]; } || [[ $GITHUB_HEAD_REF == *"patch"* ]]
 
            # If the above check failed, post a comment on the PR explaining the failure
            # NOTE - this may not work if the PR is coming from a fork, due to limitations in GitHub actions secrets
            - name: Post PR comment
              if: failure()
              uses: mshick/add-pr-comment@v1
              with:
                  message: |
                      Hi @${{ github.event.pull_request.user.login }},
 
                      It looks like this pull-request is has been made against the ${{github.event.pull_request.head.repo.full_name}} `master` or `main` branch.
                      The `master`/`main` branch should always contain code from the latest release.
                      Because of this, PRs to `master`/`main` are only allowed if they come from any ${{github.event.pull_request.head.repo.full_name}} `release` or `patch` branch.
 
                      You do not need to close this PR, you can change the target branch to `development` by clicking the _"Edit"_ button at the top of this page.
 
                      Thanks again for your contribution!
                  repo-token: ${{ secrets.GITHUB_TOKEN }}
                  allow-repeats: false
 
    check_version:
        timeout-minutes: 30
        name: No SNAPSHOT version on master branch
        runs-on: latchkey-small
        steps:
            - name: Set up Python
              uses: actions/setup-python@v4.3.0
              with:
                  cache: 'pip'
                  python-version: "3.8"
            # PRs to the repository master branch are only ok if coming from any patch or release branch
            - name: Install mlf-core
              run: pip install mlf-core
 
            - name: Check project version
              run: |
                  PROJECTVERSION=$(mlf-core bump-version --project-version . | tail -n1)
                  echo $PROJECTVERSION;
                  if [[ $PROJECTVERSION == *"SNAPSHOT"* ]];then
                      exit -1
                  else
                      exit 0
                  fi
 
            # If the above check failed, post a comment on the PR explaining the failure
            # NOTE - this may not work if the PR is coming from a fork, due to limitations in GitHub actions secrets
            - name: Post PR comment
              if: failure()
              uses: mshick/add-pr-comment@v1
              with:
                  message: |
                      Hi @${{ github.event.pull_request.user.login }},
 
                      It looks like this pull-request is has been made against the ${{github.event.pull_request.head.repo.full_name}} `master`/`main` branch.
                      A version check determined that you are using a SNAPSHOT version.
                      The `master`/`main` branch should never have any SNAPSHOT versions, since only fully stable code should be on the `master`/`main` branch.
                  repo-token: ${{ secrets.GITHUB_TOKEN }}
                  allow-repeats: false
 

What changed

1 third-party action is referenced by a movable tag. Pin it to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

This workflow runs 2 jobs per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow