til: github-actions_ensure-labels.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| github-actions_ensure-labels.md | github-actions | Ensure labels exist in a GitHub repository | https://github.com/simonw/til/blob/main/github-actions/ensure-labels.md | I wanted to ensure that when [this template repository](https://github.com/simonw/action-transcription) 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`: ```yaml name: Ensure labels on: [push] jobs: ensure_labels: runs-on: ubuntu-latest steps: - name: Create labels uses: actions/github-script@v6 with: script: | try { await github.rest.issues.createLabel({ ...context.repo, name: 'captions' }); await github.rest.issues.createLabel({ ...context.repo, name: 'whisper' }); } catch(e) { // Ignore if labels exist already } ``` 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. | <p>I wanted to ensure that when <a href="https://github.com/simonw/action-transcription">this template repository</a> was used to create a new repo that repo would have a specific set of labels.</p> <p>Here's the workflow I came up with, saved as <code>.github/workflows/ensure_labels.yml</code>:</p> <div class="highlight highlight-source-yaml"><pre><span class="pl-ent">name</span>: <span class="pl-s">Ensure labels</span> <span class="pl-ent">on</span>: <span class="pl-s">[push]</span> <span class="pl-ent">jobs</span>: <span class="pl-ent">ensure_labels</span>: <span class="pl-ent">runs-on</span>: <span class="pl-s">ubuntu-latest</span> <span class="pl-ent">steps</span>: - <span class="pl-ent">name</span>: <span class="pl-s">Create labels</span> <span class="pl-ent">uses</span>: <span class="pl-s">actions/github-script@v6</span> <span class="pl-ent">with</span>: <span class="pl-ent">script</span>: <span class="pl-s">|</span> <span class="pl-s"> try {</span> <span class="pl-s"> await github.rest.issues.createLabel({</span> <span class="pl-s"> ...context.repo,</span> <span class="pl-s"> name: 'captions'</span> <span class="pl-s"> });</span> <span class="pl-s"> await github.rest.issues.createLabel({</span> <span class="pl-s"> ...context.repo,</span> <span class="pl-s"> name: 'whisper'</span> <span class="pl-s"> });</span> <span class="pl-s"> } catch(e) {</span> <span class="pl-s"> // Ignore if labels exist already</span> <span class="pl-s"> }</span></pre></div> <p>This creates <code>captions</code> and <code>whisper</code> labels, if they do not yet exist.</p> <p>It's wrapped in a <code>try/catch</code> so that if the labels exist already (as they will on subsequent runs) the error can be ignored.</p> <p>Note that you need to use <code>await ...</code> inside that <code>try/catch</code> block or exceptions thrown by those methods will still cause the action run to fail.</p> <p>The <code>...context.repo</code> trick saves on having to pass <code>owner</code> and <code>repo</code> explicitly.</p> | <Binary: 45,815 bytes> | 2022-09-25T11:28:21-07:00 | 2022-09-25T18:28:21+00:00 | 2022-09-25T11:28:53-07:00 | 2022-09-25T18:28:53+00:00 | 147e94f19f28e7c13888d03c583014ee | ensure-labels |