Running GitHub Actions on Pull Requests with Specific Labels
GitHub Actions has revolutionized the way we automate workflows directly from GitHub repositories. But did you know that it can be further customized to react to more specific events, such as labeling a pull request? In this blog post, we’ll guide you through the process of configuring GitHub Actions to trigger when pull requests have a specific label.
GitHub Actions and Event-Based Triggers
GitHub Actions are all about automation, which is driven by certain events in your repository. These events can be anything from pushing code, making a release, or even something as specific as labeling a pull request.
Events triggering workflows is the core concept of GitHub Actions. These are predefined in the .github/workflows
directory of your repository in YAML format.
Running Actions on Pull Request Labeling
Here’s how you can set up a GitHub Actions workflow to trigger whenever a pull request gets a certain label:
Step 1: Define the Workflow Trigger
The first thing you need to define in your YAML file is what will trigger this workflow. In our case, it’s a labeled event on a pull request.
1 2 3 4 |
name: Label-based workflow on: pull_request: types: [labeled] |
This YAML code creates a workflow that gets triggered whenever a label is added to a pull request.
Step 2: Define the Job and Steps
Now that we’ve set the trigger, let’s define the job and the steps this workflow will execute.
1 2 3 4 5 6 |
jobs: check-label: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 |
In the above YAML code, we’re creating a job named check-label
that runs on an Ubuntu environment. The first step is to checkout the code using the actions/checkout
action.
Step 3: Set a Condition for Specific Label
Since we want this workflow to run when a specific label is applied, we need to add a conditional statement:
1 2 3 |
- name: Run when label is applied if: contains(github.event.pull_request.labels.*.name, 'your-label') run: echo "This will run when 'your-label' is applied" |
In this part of the YAML file, we’re setting an if condition to check if the label that triggered the workflow matches ‘your-label’. If it does, it echoes a statement.
Step 4: Putting It All Together
After setting up the trigger, defining the job and steps, and setting the condition for a specific label, the final YAML file for the GitHub Action workflow should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
name: Label-based workflow on: pull_request: types: [labeled] jobs: check-label: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Run when label is applied if: contains(github.event.pull_request.labels.*.name, 'your-label') run: echo "This will run when 'your-label' is applied" |
In this final code, the workflow triggers when a label is added to a pull request. The workflow checks out the code and, if the label ‘your-label’ is applied, echoes a message.
Just replace ‘your-label’ with the actual label you want to trigger this workflow, and you’re all set! This workflow will now react to your labeled pull requests.
Conclusion
Configuring GitHub Actions to run on pull requests with specific labels allows for precise control over your workflows. This flexibility demonstrates how powerful and customizable GitHub Actions can be, proving itself an invaluable tool in efficient and productive CI/CD setup.
It’s worth noting that GitHub Actions offers a plethora of events that can trigger workflows, enabling a wide array of automation possibilities. Make sure to explore further to leverage GitHub Actions to its full potential in your projects.