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.
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.
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
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
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Cache dependency installs on the setup step so they are served from cache.
- Add a job timeout so a hung step cannot burn hours of runner time.
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:
- Dependency installs
This workflow runs 2 jobs per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.