Skip to content
Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

GitHub Action for label filtering

If a labels exists on a pull_request, do something.

Github Release NumberTwitter Follow

After fiddling through workflow expression syntax for the past few hours, I found it easier just to make an action that specifically checked if a pull_request had a certain label or not. I plan to expand this to issues as well.

Using the actionInstallConfigurationEventsReal world

Using the action

In this barebones example, we look to see if a pull_request pointing towards the main branch has a label 'preview'.

name: Has labelon:
pull_request:
types:
- opened
- synchronize
- labeled
- unlabeledbranches:
- mainjobs:
haslabel:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
- name: Labeled to previewid: haslabeluses: DanielTamkin/HasLabel@v1.0.4with:
contains: 'preview'
- name: Test actionif: steps.haslabel.outputs.labeled-previewrun: echo 'This pull_request has this label.'
- name: Test action "no match"if: steps.haslabel.outputs.labeled-preview != 'true'run: echo 'This pull_request does not have this label.'
- name: Test action "unlabeled"if: steps.haslabel.outputs.unlabeledrun: echo 'This pull_request had this label removed.'

Install

Click the button 'Use this workflow', or if you're on the marketplace 'Use latest version.'

Configuration

Failure to supply either a contains or exact label will result in job failure.

You can either loosely check for a label, or strictly check, but not both. If you need to check for more than one label try and narrow down your usecases for your labels first. If you still need more than one label for a job to trigger, stack HasLabel! The Action outputs unique variables through the step.output context.

with:
contains: 'label'# a label contains this stringexact: 'label'# a label contains exactly this string.

contains: $labelname

Type: String

If a label contains the string provided, HasLabel will proceed by outputting labeled-$labelname. Useful for statically worded labels such as docs or feat where you have flexibility in the naming scheme.

exact: $labelname

Type: String

If a label contains the string provided, HasLabel will proceed by outputting labeled-$labelname. Useful for statically worded labels such as docs or feat where you have flexability in the naming scheme.

Events

HasLabel handles labeled, synchronize & unlabeled events by outputting unique variables accessable through steps.<stepid>.outputs.<event>-<label>. I'd suggest learning more about the steps context so to best utilize this Action.

labeled, synchronize

Outputs: labeled-$labelname
Usage: steps.<stepid>.outputs.labeled-$labelname

Replace $labelname with the label name you specified. If the supplied label is found, HasLabel will signify this by outputting a unique variable in the form of labeled-$labelname, perfect for custom logic. This also goes for the unique synchronize event, allowing pull_request's dependent on push events on pull_requests

Example: A Step triggered when a pull_request has label preview.

 - name: Test action "no match"if: steps.haslabel.outputs.labeled-previewrun: echo 'Handle a unlabeled event'

unlabeled

Outputs: unlabeled-$labelname
Usage: steps.<stepid>.outputs.unlabeled-$labelname

Replace $labelname with the label name you specified. If the supplied label is either not found or was just removed, HasLabel will signify this by outputting a unique variable in the form of unlabeled-$labelname. Especially useful if you want to trigger steps based on a loss of a label. Such as review checks, stagging builds or approvals.

Example: A Step triggered when a label is no-longer present.

 - name: Test action "no match"if: steps.haslabel.outputs.unlabeled-previewrun: echo 'Handle a unlabeled event'

Real World

This is a good real-world example where you trigger @nwtgck's Netlify deploy action once the label preview is added.

You could just as easily have this action work in a single job too! Accessing the outputs of a step are easy to access through: steps.<jobid>.outputs.<labeled/unlabeled>-<label>

name: Previewon:
pull_request:
types:
- opened
- synchronize
- labeled
- unlabeledbranches:
- masterjobs:
haslabel:
name: analyse labelsruns-on: ubuntu-latestoutputs:
preview: ${{ steps.haslabel.outputs.labeled-preview }}steps:
- name: Cancel Previous Runsuses: styfle/cancel-workflow-action@0.4.1with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- name: Labeled to previewid: haslabeluses: DanielTamkin/HasLabel@v1.0.4with:
contains: 'preview'deploy:
name: deployneeds: haslabelif: needs.haslabel.outputs.previewruns-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
- name: Deploy to Netlifyuses: nwtgck/actions-netlify@v1.1with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}timeout-minutes: 1

Contact

This is my first GitHub action, i'd love feedback! Create an issue or Tweet me.

License

This project uses the MIT license.

About

GitHub Action that filters pull_request labels. If a labels exists on a pull_request, do something.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages