| 1 |
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md |
| 2 |
name: Sync Labels |
| 3 |
|
| 4 |
# See: https://docs.github.com/actions/reference/workflows-and-actions/events-that-trigger-workflows |
| 5 |
on: |
| 6 |
push: |
| 7 |
paths: |
| 8 |
- ".github/workflows/sync-labels.ya?ml" |
| 9 |
- ".github/label-configuration-files/*.ya?ml" |
| 10 |
pull_request: |
| 11 |
paths: |
| 12 |
- ".github/workflows/sync-labels.ya?ml" |
| 13 |
- ".github/label-configuration-files/*.ya?ml" |
| 14 |
schedule: |
| 15 |
# Run daily at 8 AM UTC to sync with changes to shared label configurations. |
| 16 |
- cron: "0 8 * * *" |
| 17 |
workflow_dispatch: |
| 18 |
repository_dispatch: |
| 19 |
|
| 20 |
env: |
| 21 |
CONFIGURATIONS_FOLDER: .github/label-configuration-files |
| 22 |
CONFIGURATIONS_ARTIFACT_PREFIX: label-configuration-file- |
| 23 |
|
| 24 |
jobs: |
| 25 |
check: |
| 26 |
runs-on: ubuntu-latest |
| 27 |
permissions: |
| 28 |
contents: read |
| 29 |
|
| 30 |
steps: |
| 31 |
- name: Checkout repository |
| 32 |
uses: actions/checkout@v7 |
| 33 |
|
| 34 |
- name: Download JSON schema for labels configuration file |
| 35 |
id: download-schema |
| 36 |
uses: carlosperate/download-file-action@v2 |
| 37 |
with: |
| 38 |
file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json |
| 39 |
location: ${{ runner.temp }}/label-configuration-schema |
| 40 |
|
| 41 |
- name: Install JSON schema validator |
| 42 |
run: | |
| 43 |
sudo npm install \ |
| 44 |
--global \ |
| 45 |
ajv-cli \ |
| 46 |
ajv-formats |
| 47 |
|
| 48 |
- name: Validate local labels configuration |
| 49 |
run: | |
| 50 |
# See: https://github.com/ajv-validator/ajv-cli#readme |
| 51 |
ajv validate \ |
| 52 |
--all-errors \ |
| 53 |
-c ajv-formats \ |
| 54 |
-s "${{ steps.download-schema.outputs.file-path }}" \ |
| 55 |
-d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}" |
| 56 |
|
| 57 |
download: |
| 58 |
needs: check |
| 59 |
runs-on: ubuntu-latest |
| 60 |
permissions: {} |
| 61 |
|
| 62 |
strategy: |
| 63 |
matrix: |
| 64 |
filename: |
| 65 |
# Filenames of the shared configurations to apply to the repository in addition to the local configuration. |
| 66 |
# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/sync-labels |
| 67 |
- universal.yml |
| 68 |
- tooling.yml |
| 69 |
|
| 70 |
steps: |
| 71 |
- name: Download |
| 72 |
uses: carlosperate/download-file-action@v2 |
| 73 |
with: |
| 74 |
file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} |
| 75 |
|
| 76 |
- name: Pass configuration files to next job via workflow artifact |
| 77 |
uses: actions/upload-artifact@v7 |
| 78 |
with: |
| 79 |
path: ${{ matrix.filename }} |
| 80 |
if-no-files-found: error |
| 81 |
name: ${{ env.CONFIGURATIONS_ARTIFACT_PREFIX }}${{ matrix.filename }} |
| 82 |
|
| 83 |
sync: |
| 84 |
needs: download |
| 85 |
runs-on: ubuntu-latest |
| 86 |
permissions: |
| 87 |
contents: read |
| 88 |
issues: write |
| 89 |
|
| 90 |
steps: |
| 91 |
- name: Set environment variables |
| 92 |
run: | |
| 93 |
# See: https://docs.github.com/actions/reference/workflows-and-actions/workflow-commands#setting-an-environment-variable |
| 94 |
echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >>"$GITHUB_ENV" |
| 95 |
|
| 96 |
- name: Determine whether to dry run |
| 97 |
id: dry-run |
| 98 |
if: > |
| 99 |
github.event_name == 'pull_request' || |
| 100 |
( |
| 101 |
( |
| 102 |
github.event_name == 'push' || |
| 103 |
github.event_name == 'workflow_dispatch' |
| 104 |
) && |
| 105 |
github.ref != format('refs/heads/{0}', github.event.repository.default_branch) |
| 106 |
) |
| 107 |
run: | |
| 108 |
# Use of this flag in the github-label-sync command will cause it to only check the validity of the |
| 109 |
# configuration. |
| 110 |
echo "flag=--dry-run" >>$GITHUB_OUTPUT |
| 111 |
|
| 112 |
- name: Checkout repository |
| 113 |
uses: actions/checkout@v7 |
| 114 |
|
| 115 |
- name: Download configuration file artifacts |
| 116 |
uses: actions/download-artifact@v8 |
| 117 |
with: |
| 118 |
merge-multiple: true |
| 119 |
pattern: ${{ env.CONFIGURATIONS_ARTIFACT_PREFIX }}* |
| 120 |
path: ${{ env.CONFIGURATIONS_FOLDER }} |
| 121 |
|
| 122 |
- name: Remove unneeded artifacts |
| 123 |
uses: geekyeggo/delete-artifact@v6 |
| 124 |
with: |
| 125 |
name: ${{ env.CONFIGURATIONS_ARTIFACT_PREFIX }}* |
| 126 |
|
| 127 |
- name: Merge label configuration files |
| 128 |
run: | |
| 129 |
# Merge all configuration files |
| 130 |
shopt -s extglob |
| 131 |
cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) >"${{ env.MERGED_CONFIGURATION_PATH }}" |
| 132 |
|
| 133 |
- name: Install github-label-sync |
| 134 |
run: sudo npm install --global github-label-sync |
| 135 |
|
| 136 |
- name: Sync labels |
| 137 |
env: |
| 138 |
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 139 |
run: | |
| 140 |
# See: https://github.com/EndBug/github-label-sync |
| 141 |
github-label-sync \ |
| 142 |
--labels "${{ env.MERGED_CONFIGURATION_PATH }}" \ |
| 143 |
${{ steps.dry-run.outputs.flag }} \ |
| 144 |
${{ github.repository }} |