Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Ensure labels exist in a GitHub repository#
I wanted to ensure that when this template repository was used to create a new repo that repo would have a specific set of labels.
Here’s the workflow I came up with, saved as .github/workflows/ensure_labels.yml:
1name: Ensure labels2on: [push]3
4jobs:5 ensure_labels:6 runs-on: ubuntu-latest7 steps:8 - name: Create labels9 uses: actions/github-script@v610 with:11 script: |12 try {13 await github.rest.issues.createLabel({14 ...context.repo,15 name: 'captions'16 });17 await github.rest.issues.createLabel({18 ...context.repo,19 name: 'whisper'20 });21 } catch(e) {22 // Ignore if labels exist already23 }This creates captions and whisper labels, if they do not yet exist.
It’s wrapped in a try/catch so that if the labels exist already (as they will on subsequent runs) the error can be ignored.
Note that you need to use await ... inside that try/catch block or exceptions thrown by those methods will still cause the action run to fail.
The ...context.repo trick saves on having to pass owner and repo explicitly.