FantasticFiasco/action-update-license-year

GitHub: FantasticFiasco/action-update-license-year

这是一个GitHub Action,用于自动更新许可证文件中的版权年份,通过创建Pull Request实现安全、自动化的维护。

Stars: 69 | Forks: 3

# GitHub Action - 更新许可证文件中的版权年份 ![构建状态](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/fbc9e7708a050308.svg) [![代码覆盖率状态](https://coveralls.io/repos/github/FantasticFiasco/action-update-license-year/badge.svg?branch=main)](https://coveralls.io/github/FantasticFiasco/action-update-license-year?branch=main) [![SemVer 兼容](https://img.shields.io/badge/%E2%9C%85-SemVer%20compatible-blue)](https://semver.org/) 所以,看起来这件事发生了。为了不再手动更新我 GitHub 仓库中许可证文件的版权年份,我创建了这个 GitHub Action。 **创建过程有趣吗?** - 当然,它激活了我为数不多的脑细胞。 **我能使用它吗?** - 是的,你可以。它自动支持下列许可证,同时也支持自定义的正则表达式转换,让你指定自己的许可证格式。 - Apache 2.0 (Apache-2.0) - BSD 2-clause "Simplified" (BSD-2-Clause) - BSD 3-clause "New" or "Revised" (BSD-3-Clause) - GNU Affero General Public License v3.0 only (AGPL-3.0-only) - MIT (MIT) **这个 Action 会在默认分支上提交任何内容吗?** - 不会。该 Action 将创建一个新的 Pull Request,合并它将是你的责任。 ## 使用非常简单 对于 GitHub 上的大多数仓库,下面的工作流文件就能完成任务。如果你发现结果不符合预期,请参阅[场景示例](#scenarios)或[打开一个新 Issue](https://github.com/FantasticFiasco/action-update-license-year/issues/new/choose)。 ## API 该 Action 支持以下输入: ``` - uses: FantasticFiasco/action-update-license-year@v3 with: # Personal access token (PAT) used when interacting with Git and GitHub. # # We recommend using a service account with the least permissions necessary. Also # when generating a new PAT, select the least scopes necessary. # # [Learn more about creating and using encrypted secrets](https://help.github.com/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets) # # Required: true token: '' # A path or wildcard pattern specifying files to transform. Multiple paths can be # specified using literal styled YAML. # # Required: false # Default: LICENSE path: '' # A regular expression (JavaScript flavor) describing the license transform. The # expression must have the following properties: # # - A capturing group named "from", encapsulating the first year of license # validity # - Written to support the RegExp flags "gmi" ("global", "multiline" and "ignore # case") # # The expression will be used by String.prototype.replace() to apply the # transformation. # # Required: false # Default: null transform: '' # The branch name. Supports substituting variable {{currentYear}}. # # Required: false # Default: license/copyright-to-{{currentYear}} branchName: '' # The git commit title. Supports substituting variable {{currentYear}}. # # Required: false # Default: docs(license): update copyright year(s) commitTitle: '' # The git commit body that will be appended to commit title, separated by two line # returns. Supports substituting variable {{currentYear}}. # # Required: false # Default: commitBody: '' # The git author name, used when committing changes to the repository. # # Required: false # Default: github-actions commitAuthorName: '' # The git author e-mail, used when committing changes to the repository. # # Required: false # Default: github-actions@github.com commitAuthorEmail: '' # The GPG private key, used in combination with gpgPassphrase when signing # commits. Private keys protected by a passphrase are supported while private keys # without a passphrase are unsupported. # # Required: false # Default: gpgPrivateKey: '' # The GPG passphrase, used in combination with gpgPrivateKey when signing commits. # # Required: false # Default: gpgPassphrase: '' # The title of the new pull request. Supports substituting variable # {{currentYear}}. # # Required: false # Default: Update license copyright year(s) prTitle: '' # The contents of the pull request. Supports substituting variable # {{currentYear}}. # # Required: false # Default: prBody: '' # Comma-separated list with usernames of people to assign when pull request is # created. # # Required: false # Default: assignees: '' # Comma-separated list of labels to add when pull request is created. # # Required: false # Default: labels: '' ``` 该 Action 设置以下输出: - `currentYear`: 当前年份。如果 Action 成功运行且许可证被更新,此输出将存在。 - `branchName`: 为更新许可证而创建的 Git 分支名称。如果 Action 成功运行且许可证被更新,此输出将存在。 - `pullRequestNumber`: 为更新许可证而创建的 GitHub Pull Request 编号。如果 Action 成功运行且许可证被更新,此输出将存在。 - `pullRequestUrl`: 为更新许可证而创建的 GitHub Pull Request 的 URL。如果 Action 成功运行且许可证被更新,此输出将存在。 有关输出及其用法的更多信息,请参阅 [GitHub Actions 示例:输出](https://www.actionsbyexample.com/outputs.html)或名为 [我想在每年第一次提交时更新我的许可证,并希望它被合并](#i-want-to-update-my-license-on-first-commit-each-year-and-i-want-it-merged) 的场景示例。 ## 场景示例 以下章节将展示一些常见场景及其 GitHub Action 配置。 - [我刚开始接触 GitHub Actions,不知道从何开始](#Im-new-to-github-actions-and-dont-know-where-to-start) - [我想在每年 1 月 1 日凌晨 3:00 更新我的许可证](#i-want-to-update-my-license-annually-at-0300-am-on-january-1) - [我想使用手动触发来更新我的许可证](#i-want-to-update-my-license-using-a-manual-trigger) - [我想在每年第一次提交时更新我的许可证,并希望它被合并](#i-want-to-update-my-license-on-first-commit-each-year-and-i-want-it-merged) - [我想更新我的许可证,但它不叫 `LICENSE`](#i-want-to-update-my-license-but-it-isnt-called-license) - [我想更新我的许可证,但此 Action 不支持它](#i-want-to-update-my-license-but-it-isnt-supported-by-this-action) - [我想更新我所有的许可证,我有不止一个](#i-want-to-update-all-my-licenses-i-have-more-than-one) - [我想更新我 monorepo 中所有的许可证](#i-want-to-update-all-my-licenses-in-my-monorepo) - [我想更新我源文件中的许可证](#i-want-to-update-the-license-in-my-source-files) - [我想在同一个 PR 中更新我的许可证和一个自定义源](#i-want-to-update-my-license-and-a-custom-source-in-the-same-pr) - [我想为我的提交添加 GPG 签名](#i-want-to-gpg-sign-my-commits) - [我希望我的 Pull Request 遵循某个规范](#i-want-my-pull-requests-to-follow-a-convention) - [我希望我的 Pull Request 被自动合并](#i-want-my-pull-requests-to-be-automatically-merged) - [我希望我的 Pull Request 能触发新的 GitHub Actions 工作流](#i-want-my-pull-requests-to-trigger-new-github-actions-workflows) ### 我刚开始接触 GitHub Actions,不知道从何开始 GitHub Actions 在 [GitHub Actions 文档](https://docs.github.com/actions)中有详细描述,但基本上归结为在 `./.github/workflows/` 目录下创建一个文件,例如 `./.github/workflows/update-copyright-years-in-license-file.yml`,然后决定[何时触发该 Action](https://docs.github.com/actions/reference/events-that-trigger-workflows),最后根据需要进行配置。 以下场景将为你提供一些示例。 ### 我想在每年 1 月 1 日凌晨 3:00 更新我的许可证 这可能是该 Action 最常见的用法,前提是你可以忍受在新年庆祝期间收到一个 Pull Request。 ``` name: Update copyright year(s) in license file on: schedule: - cron: '0 3 1 1 *' jobs: run: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} ``` ### 我想使用手动触发来更新我的许可证 一年时间很长,1 月 1 日可能还很遥远。既然 [GitHub Actions 支持手动触发](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/),我们可以使用 `workflow_dispatch` 来手动触发我们的工作流。 ``` name: Update copyright year(s) in license file on: workflow_dispatch jobs: run: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} ``` ### 我想在每年第一次提交时更新我的许可证,并希望它被合并 也许你不想在新年庆祝期间被打扰,而且你知道自己肯定会忘记手动触发工作流。另一种选择是在每年第一次提交时更新你的许可证?再加上自动合并?对于这些情况,我们可以使用以下工作流。 ``` name: Update copyright year(s) in license file on: push: branches: - 'main' # Or 'master' depending on your default branch jobs: run: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} id: license - name: Merge PR if: steps.license.outputs.pullRequestNumber != '' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Replace '--merge' with '--rebase' to rebase the commits onto the base # branch, or with '--squash' to squash the commits into one commit and # merge it into the base branch. # For more information regarding the merge command, please see # https://cli.github.com/manual/gh_pr_merge. run: gh pr merge --merge --delete-branch ${{ steps.license.outputs.pullRequestNumber }} ``` ### 我想更新我的许可证,但它不叫 `LICENSE` 你的仓库中有一个许可证,但它可能不叫 `LICENSE`。也许它叫 `LICENSE.md`?那么你就需要相应地配置该 Action。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} path: LICENSE.md ``` ### 我想更新我的许可证,但此 Action 不支持它 此 Action 内置支持几个常见的许可证。但是,你可能使用了自己特别的许可证,并且发现此 Action 不支持它。在这种情况下,你可以定义自己的转换。 该转换被声明为一个正则表达式(JavaScript 风格),并且必须具有以下属性: - 一个名为 `from` 的捕获组,封装许可证有效期的起始年份。 - 编写时要支持 RegExp 标志 `gmi`(`global`、`multiline` 和 `ignore case`)。 该表达式将由 `String.prototype.replace()` 使用以应用转换。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} transform: (?<=my own copyright )(?\d{4})?-?(\d{4})? ``` ### 我想更新我所有的许可证,我有不止一个 你的仓库可能包含不止一个许可证。也许你有一个用于开源,另一个用于商业用途?无论如何,此 Action 支持使用字面量样式的 YAML 指定多个路径。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} path: | LICENSE-OPEN-SOURCE LICENSE-COMMERCIAL ``` ### 我想更新我 monorepo 中所有的许可证 你的仓库可能是一个 monorepo,并且你有很多许可证。你想一次性更新它们,最好不必逐个指定。好吧,我们为你解决了这个问题。`path` 输入参数支持 glob 模式。耶! ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} path: packages/*/LICENSE ``` ### 我想更新我源文件中的许可证 你的每个源文件中都有一个声明许可证的头部。我想那有很多文件需要更新。好吧,我们为你解决了这个问题。glob 模式来救场。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} path: src/**/*.js ``` ### 我想在同一个 PR 中更新我的许可证和一个自定义源 除了你的许可证文件外,你的项目还包括其他需要使用 `transform` 进行自定义年份更新的文件。你可以在一个 Pull Request 中使用 `needs` 指令链接多个作业来完成全部更新。 ``` jobs: license: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} source: needs: license runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} path: '*.js' transform: (?<=my own copyright )(?\d{4})?-?(\d{4})? ``` ### 我想为我的提交添加 GPG 签名 你可能依赖于 [GitHub 提交签名验证](https://docs.github.com/authentication/managing-commit-signature-verification/about-commit-signature-verification),并且就是喜欢那些绿色的闪光徽章。别担心,你可以配置此 Action 来使用带有对应密码短语的私钥对提交进行签名。 请记住,GPG 密钥必须注册到一个有效的 GitHub 用户,并且该用户的电子邮件地址也必须配置好。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} commitAuthorEmail: gpgPrivateKey: ${{ secrets.gpgPrivateKey }} gpgPassphrase: ${{ secrets.gpgPassphrase }} ``` ### 我希望我的 Pull Request 遵循某个规范 你的 Pull Request 可能需要遵循某个规范。它可能需要特定的标题,或者你希望将 Pull Request 分配给特定的维护者?无论原因如何,我们都为你解决了。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} branchName: license/{{currentYear}} commitTitle: update my license commitBody: Let's keep legal happy. prTitle: Update my license prBody: It's that time of the year, let's update the license. assignees: MyUser, SomeMaintainer labels: documentation, legal ``` ### 我希望我的 Pull Request 被自动合并 你可以利用 [GitHub CLI](https://github.com/cli/cli) 来合并你的 Pull Request 并删除分支。 ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.GITHUB_TOKEN }} - name: Merge pull request env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | # Replace '--merge' with '--rebase' to rebase the commits onto the base # branch, or with '--squash' to squash the commits into one commit and merge # it into the base branch. # For more information regarding the merge command, please see # https://cli.github.com/manual/gh_pr_merge. gh pr merge --merge --delete-branch ``` ### 我希望我的 Pull Request 能触发新的 GitHub Actions 工作流 如果你使用个人访问令牌(PAT)[而不是 `GITHUB_TOKEN`](https://docs.github.com/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow),你的 Pull Request 可以触发其他 GitHub Actions 工作流,例如 CI 检查。 为此,你需要[创建一个个人访问令牌](https://docs.github.com/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token),最小所需作用域为 `public_repo`,并[基于此令牌创建一个 Secret](https://docs.github.com/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository)(例如 `LICENSE_SECRET`)。 将你的 Secret 名称放入 `token` 属性中: ``` steps: - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: FantasticFiasco/action-update-license-year@v3 with: token: ${{ secrets.LICENSE_SECRET }} ``` ## 贡献者 以下用户对本项目做出了重要贡献。非常感谢!

Álvaro Mondéjar

💻 📖 💡

Gérôme Grignon

🐛 💻

Utkarsh Sethi

📖

C0D3 M4513R

🤔

Aleksei Borodin

💡

Jeroen van Warmerdam

🤔 🐛

Angel Aviel Domaoan

🐛
标签:数据可视化, 自定义脚本