参考:
有些项目在发布新版本时,一般都会创建一个
GitHub Release
,并且把对应编译好之后的文件上传到Release
的资源列表中
补充 这两个官方
Action
目前已经归档,停止维护了 可以使用下方代替
项目的构建流程是在项目git push --tags
的时候,触发 workflow,通过Github Actions
编译出来Windows、Linux、macOS
三个操作系统对应的 64 位可执行文件,再根据tag name
和tag message
来创建对应的Github Release
,并将编译好的文件上传
name: .NET Core Release
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Zip the Build
run: zip -r ${{ secrets.ReleaseZipName }} ./AppName/bin/Release/netcoreapp3.1/
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: TODO New Release.
draft: false
prerelease: false
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./${{ secrets.ReleaseZipName }}.zip
asset_name: ${{ secrets.ReleaseZipName }}.zip
asset_content_type: application/zip
softprops/action-gh-release: 📦 GitHub Action for creating GitHub Releases
官方 action 已经归档,下面使用社区的 action,来配置自动 Release
name: .NET Core Release
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Zip the Build
run: zip -r ${{ secrets.ReleaseZipName }} ./AppName/bin/Release/netcoreapp3.1/
- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
tag_name: ${{ github.ref }}
name: Release ${{ github.ref }}
body: TODO New Release.
draft: false
prerelease: false
files: |
${{ secrets.ReleaseZipName }}.zip
LICENSE
Action 运行完成后,会有一个 github-actions
机器人发起的 Release
你需要点击 Edit release
后,保存,才会显示在 全部 Releases
中,
TODO: 不知道是不是我哪里搞错了,这样还需要自己点一下,不能说完全自动化
on:
release:
types: [published]
发现通过这种方法发起的 release,不会触发, 只有自己手动,以自己名义 create release,才会触发上面的条件
PluginCore-release.yml
name: PluginCore Release
on:
push:
tags:
- 'PluginCore-v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.102
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Zip the Build
run: |
zip -r PluginCore-netcoreapp3.1.zip ./src/PluginCore/bin/Release/netcoreapp3.1/
zip -r PluginCore-net5.0.zip ./src/PluginCore/bin/Release/net5.0/
- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
#tag_name: ${{ github.ref }}
#name: ${{ github.ref }}
body: TODO New Release.
#body_path: CHANGELOG.txt
draft: false
prerelease: false
files: |
PluginCore-netcoreapp3.1.zip
PluginCore-net5.0.zip
LICENSE
补充:
不知道为什么,先 dotnet restore
,再以 --no-restore
的方式 dotnet build
就会报错,找不到包,
一定要 在 dotnet build
时,内部 restore
<!-- 方便开发debug,与发布到nuget -->
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="PluginCore.IPlugins" Version="0.2.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="..\PluginCore.IPlugins\PluginCore.IPlugins.csproj" />
</ItemGroup>
解决方案:
删除步骤 Install dependencies
,在 build
时自动 restore
- name: Build
run: dotnet build --configuration Release
不要去指定 tag_name: ${{ github.ref }}
,不然会导致如下,只要不指定,默认就会帮你删除 refs/tags/
refs/tags/PluginCore-v0.3.1
如果需要自定义,则可以截断
- name: Set outputs
id: vars
run: |
echo ::set-output name=TAG_NAME::$(echo ${GITHUB_REF:10})
在步骤中使用
${{ steps.vars.outputs.TAG_NAME }}
name: PluginCore Release
on:
push:
tags:
- 'PluginCore-v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.102
- name: Build
run: dotnet build --configuration Release
- name: Zip the Build
run: |
cd ./src/PluginCore/bin/Release
zip -r PluginCore-netcoreapp3.1.zip ./netcoreapp3.1/
zip -r PluginCore-net5.0.zip ./net5.0/
cd ../../../../
mv ./src/PluginCore/bin/Release/*.zip ./
- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
#tag_name: ${{ github.ref }}
#name: ${{ github.ref }}
body: TODO New Release.
#body_path: CHANGELOG.txt
draft: false
prerelease: false
files: |
PluginCore-netcoreapp3.1.zip
PluginCore-net5.0.zip
LICENSE
感谢帮助!
本文作者: yiyun
本文链接: https://moeci.com/posts/分类-github/github-actions-release/
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!