Skip to content

feat: auto-fix license files on PRs and improve CI reliability #4899

feat: auto-fix license files on PRs and improve CI reliability

feat: auto-fix license files on PRs and improve CI reliability #4899

Workflow file for this run

# Automatically fix license files on PRs that need updates
# Instead of just failing, this workflow pushes the fix and comments on the PR
name: License Check
on:
pull_request:
paths:
- "**.go"
- go.mod
- go.sum
- ".github/licenses.tmpl"
- "script/licenses*"
- "third-party-licenses.*.md"
- "third-party/**"
permissions:
contents: write
pull-requests: write
jobs:
license-check:
runs-on: ubuntu-latest
# Don't run on forks (they can't push back) or dependabot
if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]'
steps:
- name: Check out code
uses: actions/checkout@v6
with:
ref: ${{ github.head_ref }}
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: "go.mod"
# actions/setup-go does not setup the installed toolchain to be preferred over the system install,
# which causes go-licenses to raise "Package ... does not have module info" errors.
# For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633
- name: Regenerate licenses
env:
CI: "true"
run: |
export GOROOT=$(go env GOROOT)
export PATH=${GOROOT}/bin:$PATH
./script/licenses
- name: Check for changes
id: changes
run: |
if git diff --exit-code; then
echo "changed=false" >> $GITHUB_OUTPUT
echo "✅ License files are up to date"
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "📝 License files need updating"
git diff --stat
fi
- name: Commit and push fixes
if: steps.changes.outputs.changed == 'true'
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add third-party third-party-licenses.*.md
git commit -m "chore: regenerate third-party licenses"
git push
- name: Comment on PR
if: steps.changes.outputs.changed == 'true'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## 📜 License files updated
I noticed the third-party license files were out of date and pushed a fix to this PR.

Check failure on line 79 in .github/workflows/license-check.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/license-check.yml

Invalid workflow file

You have an error in your yaml syntax on line 79
**What changed:** Dependencies were added, removed, or updated, which requires regenerating the license documentation.
**What I did:** Ran \`./script/licenses\` and committed the result.
Please pull the latest changes before pushing again.`
})