通过 GitHub Actions 自动创建 Github Release

通过 GitHub Actions 自动创建 Github Release

Intro

在 GitHub 上维护了几个小的开源项目,每次在发布新版本的时候会创建一个 release,这样可以比较方便的找到对应的版本的代码,不需要再人肉的从 git log 中找到指定的 commit,而且在 GitHub 上创建 Release 的话别人可以方便的关注项目新版本的发布,之前都是手动创建 release,最近看到 docfx 的项目配置了自动创建 release,于是想给自己的项目里加上自动创建 release。

Sample

可以看一个实际的示例效果:



release 中的简介是后来编辑加上去的,因为选的这个 Action 暂时不支持设置 release 的简介部分

Github  Actions 配置

name: Release
on:
  push:
    branches: [ master ]
jobs:
  build:
    name: Release
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Build
      shell: pwsh
      run: .\build.ps1 --stable=true
    - name: Get Release Version
      shell: pwsh
      run: .\build\getReleaseVersion.ps1
    - name: Create GitHub release
      uses: marvinpinto/action-automatic-releases@latest
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        automatic_release_tag: ${{ env.ReleaseVersion }}
        title: ${{ env.ReleaseVersion }}
        prerelease: false
        files: |
          artifacts/packages/*

整个 Action 大体上可以分为三步,第一步是安装 dotnet 环境并且 build package,第二步是获取当前 package 的版本,用作 release 的 tag,第三步就是要创建 release 了,创建 release 使用的是一个开源的别人封装好的创建 release 的 action 模板(marvinpinto/action-automatic-releases),具体使用可以参考文档介绍:

ParameterDescriptionDefault
repo_token**GitHub Action token, e.g. "${{ secrets.GITHUB_TOKEN }}".null
draftMark this release as a draft?false
prereleaseMark this release as a pre-release?true
automatic_release_tagTag name to use for automatic releases, e.g latest.null
titleRelease title; defaults to the tag name if none specified.Tag Name
filesFiles to upload as part of the release assets.null

  • Parameters denoted with ** are required.

  • The files parameter supports multi-line glob patterns, see repository examples.

上面的 ${{ env.ReleaseVersion }} 表示从 GitHub Actions 环境中获取变量 ReleaseVersion 的值,而 ReleaseVersion 是在上一步中执行的 powershell 脚本中设置的,GitHub Actions 中的环境变量并不直接等于系统的环境变量,出于安全考虑,GitHub Actions 使用了一个自定义的变量 GITHUB_ENV 来支持用户自定义环境变量,我们需要把自定义的变量放在这里面才能跨 step 共享

powershell 脚本比较简单就是获取 package version 并将其设置到 GitHub Actions 环境变量中,脚本内容如下:

$versionPath=$PSScriptRoot+"/version.props"
$versionXml=([xml](Get-Content $versionPath))
$versionProperty=$versionXml.Project.PropertyGroup
$ReleaseVersion=$versionProperty.VersionMajor+"."+$versionProperty.VersionMinor+"."+$versionProperty.VersionPatch
$ReleaseVersion
Add-Content -Path $env:GITHUB_ENV -Value "ReleaseVersion=${ReleaseVersion}"

最后设置脚本的这里折腾了好久,因为官方给的示例是基于 Bash 的,基于 Linux  的示例,直接使用并没有效果,最后参考了这里的https://github.community/t/empty-github-env-variables-on-powershell/147626 的答案,使用了上面这种方式终于可以了,在 Linux 中需要使用 echo "ReleaseVersion=${ReleaseVersion}" >> $GITHUB_ENV,详细信息可以参考 https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable

More

以上的示例是在 Windows 上,有需要的可以调整成 Linux 进行使用,只需要把获取和设置 ReleaseVersion 的逻辑换一下就可以了

这不仅仅适用于 Nuget 包的发布,同样可以适用于其他任何需要发布的项目

目前唯一的不够完美的地方就是不支持设置介绍部分,已经有一个 issue,有需要的可以关注一下 https://github.com/marvinpinto/actions/issues/19

现在 docfx 项目在使用这个 GitHub Action 来做自动发布 release,所以也使用了这个 Action,但是没看明白 docfx 项目是在哪里设置版本的,有兴趣的可以看一下 docfx 项目,目前 3.0 版本正在使用自动发布 release 的功能 https://github.com/dotnet/docfx/blob/v3-release/.github/workflows/release.yml

References

  • https://github.com/WeihanLi/WeihanLi.Common

  • https://github.com/WeihanLi/WeihanLi.Common/blob/dev/.github/workflows/release.yml

  • https://github.com/WeihanLi/WeihanLi.Common/actions/runs/671183725

  • https://github.community/t/empty-github-env-variables-on-powershell/147626

  • https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable

  • https://github.com/marvinpinto/action-automatic-releases

  • https://github.com/marvinpinto/actions/issues/19

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值