okaryo.log

Preventing GitHub Actions from Running Jobs on PRs from Specific Branches | okaryo.log

Preventing GitHub Actions from Running Jobs on PRs from Specific Branches

    #GitHubActions#GitHub#CI/CD

Introduction

Recently, I started using renovate for automatic updates of dependent libraries and applied it to the repository of this blog.

This blog utilizes Firebase Hosting, and I’ve set up a GitHub Actions workflow such that when a PR is made to the repository, a preview channel is uploaded to Firebase Hosting. However, this workflow was also triggered for PRs created by renovate. As a result, the storage on Firebase Hosting was becoming congested due to these preview channels, at times exceeding the free tier limit.

Exceeding the free storage limit temporarily prevents subsequent production deployments, necessitating the deletion of the preview channels. To avoid this hassle, I wanted to ensure that PRs from renovate did not trigger uploads to the preview channels.

How to Set Up

To achieve this, one can either opt not to run jobs for PRs from certain branches or only run jobs for PRs from specific branches. To make this possible, we can use the if condition and expressions.

In the example below, the source branch name of the PR is obtained using github.head_ref. If it starts with `renovate/“, the job is not executed.

name: Deploy to Firebase Hosting on PR

on:
  pull_request:

jobs:
  build_and_preview:
    if: ${{ !startsWith(github.head_ref, 'renovate/') }}
    runs-on: ubuntu-latest
    steps:
      ...(omitted)...

今回は式の中でstartsWithを使ったが、これ以外にもendsWithcontainsなどいろんな関数がGitHubActionsでは用意されている。ユースケースに応じて使い分けてみてほしい。 In this instance, I’ve employed the startsWith function, but GitHub Actions offers various functions like endsWith, contains, and more. Choose the one that best fits your use case.

Conclusion

With this configuration, even if renovate creates a PR, the job to upload to the preview channel won’t be triggered.

That said, it’s crucial to check whether a library update functions correctly. It’s advisable to verify locally, write tests, and take other necessary measures to ensure everything works as expected.


Related Posts
Related Posts
Promotion

This site uses Google Analytics.