Github Action: How to dynamically run step on each PR label ?
Tags: github, ci
Published: 2020-12-04 22:44:29

---
title: Github Action: How to dynamically run step on each PR label ?
published: true
date: 2020-11-29 00:00:00 UTC
tags: github,ci
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/2922xuzvoockt1ckanob.jpg
canonical_url: https://slashgear.github.io/how-to-dynamically-run-step-on-each-github-pr-label/
---

For some time now I’ve been playing with github actions to see if I can easily reproduce behaviors I apply in my current CI tool. I wanted to parallelize tasks on each label of a pull request. In this article, I will show you a possible way to do it!

In my work at [Bedrock](https://www.bedrockstreaming.com/), we currently use Jenkins to manage our ongoing application integration and deployment. Beyond the various concerns of using groovy with pipeline syntax 😅, we have to admit that we manage to do some pretty complex workflows.

![example of complex Continous Integration workflow](https://dev-to-uploads.s3.amazonaws.com/i/wuab225zvtojfqe6vpin.jpg)

Deciding to see if it would be easy for me to reproduce the steps of these pipelines with github action, I decided to try its features. Among other things by publishing [a Github Action to check the title of a PR according to a regexp.](https://github.com/Slashgear/action-check-pr-title)

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#solution)Solution

First you need to create a workflow based on `pull_request` events and you can even specify the types of events.

```yaml
name: Example workflow
on:
  pull_request:
    types: [opened, labeled, unlabeled, synchronize]
```

Now add a job that will set up a parallelization strategy based on the labels of the github event.

```yaml
jobs:
  build:
    strategy:
      matrix:
        label: ${{github.event.pull_request.labels.*.name}}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo Hello, world from "${{matrix.label}}"!
```

This will automatically run the steps in parallel on all the labels of each Pull Request. 🎉 However, `strategy.matrix.label` does not accept an empty array as a value.😢

The job should therefore be conditional so that it is only run if `${{github.event.pull_request.labels.*.name}}` is not empty. There is an `if` parameter in the jobs that allows to do this, but unfortunately it is interpreted after checking the value of `strategy.matrix.label`.

The solution I found is to do this check in a previous job. This is an example of a workflow that dynamically launches actions on all the labels of each PR.

```yaml
name: Example workflow

on:
  pull_request:
    types: [opened, labeled, unlabeled, synchronize]

jobs:
  checkLabels:
    runs-on: ubuntu-latest
    if: ${{github.event.pull_request.labels.*.name[0]}}
    steps:
      - run: echo Check labels is not empty
  build:
    needs: [checkLabels]
    strategy:
      matrix:
        label: ${{github.event.pull_request.labels.*.name}}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo Hello, world from "${{matrix.label}}"!
```

It’s up to you to adapt it to your needs!