# Github Action 自动构建与发布项目
这两天做了一个流量可视化的项目,因为使用go语言来编写,所以可以很方便的生成windows下和linux下的可执行文件,但是之前纯靠手动发布的方法还是有些麻烦——需要将代码复制到各个系统中,再进行编译,然后压缩,最后再统一上传。
今天了解了一下Github Action,发现这个流程完全可以自动化完成,大大地节约了时间。Action的重点在于项目文件夹中 .github/workflows/
中通过yml文件进行的工作流定义,下面是我用的一个工作流。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| name: Release gocapture
on:
push:
tags:
- v*
jobs:
release:
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: gocapture ${{ github.ref }}
draft: false
prerelease: false
linuxbuild:
name: build gocapture
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install libpcap
run: sudo apt-get install libpcap-dev
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: build linux version
run: go build -o gocapture_linux -v ./...
- name: package
run: tar -zcvf gocapture_linux.tgz gocapture_linux static templates GeoLite2-City.mmdb
- name: upload
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: gocapture_linux.tgz
asset_name: gocapture_linux.tgz
asset_content_type: application/gzip
windowsbuild:
runs-on: windows-latest
needs: release
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: build windows version
run: go build -v -o gocapture_windows.exe ./...
- name: package
run: tar -zcvf gocapture_windows.tgz gocapture_windows.exe static templates GeoLite2-City.mmdb
- name: upload
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: gocapture_windows.tgz
asset_name: gocapture_windows.tgz
asset_content_type: application/gzip
|
该工作流定义了在推送一个tag的时候 (格式形如 v1.0 ),自动的在windows和linux下构建项目并打包,最后上传到github中。
我还折腾了很久的不同分支构建问题,我想的是不同的分支在通过tag触发器触发工作流之后采用不同的工作流程,一开始我以为workflow都定义在main分支的workflows文件夹中,所以得通过if判断action的上下文来处理不同的分支,比如这篇文章,但是实际上这篇文章中指明的方法不可用,因为在tag触发的事件中是找不到关于分支的信息的。后来发现 一条分支中定义的工作流只响应同一分支中的事件,这样就简单了,在各分支中定义自己的工作流即可,就是标签的格式要自己想办法规范一下了, 比如我的项目中的标签分别用用 v1.0和v1.0-cli来区分webui版本与纯命令行版本。
常用的几条命令
1
2
3
4
| git tag v1.0 # 打上标签
git push origin v1.0 # 标签默认只存在本地,需要触发构建的话,需要手动推送
git tag -d v1.0 # 删除标签
git push origin :refs/tags/v1.0 # 接着删除远程的标签
|
最后再贴一段workflow (单纯的编译,看看能否成功,后续还可以加入测试)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| name: Build gocapture
on:
push:
branches: [main, cli]
pull_request:
branches: [main, cli]
jobs:
linuxbuild:
runs-on: ubuntu-latest
env:
PCAPV: 1.10.1
CGO_LDFLAGS: '-L./libpcap-1.10.1'
steps:
- uses: actions/checkout@v2
- name: install libpcap
run: sudo apt-get install libpcap-dev
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Linux Build
run: go build -v ./...
- name: prepare arm64 build
run: sudo apt-get install bison flex gcc-aarch64-linux-gnu
# - name: build libpcap for arm64
# run: |
# cd /tmp
# wget http://www.tcpdump.org/release/libpcap-$PCAPV.tar.gz
# tar -zxvf libpcap-$PCAPV.tar.gz
# cd libpcap-$PCAPV
# export CC=aarch64-linux-gnu-gcc
# ./configure --host=aarch64-linux --with-pcap=linux
# make
# - name: build gocapture for arm64
# run: |
# rm -f nac.manifest nac.syso
# CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 CGO_LDFLAGS="-L/tmp/libpcap-$PCAPV" GOOS=linux GOARCH=arm64 go build -o gocapture_linux_arm64
windowsbuild:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Windows Build
run: go build -v ./...
|
学习参考
https://docs.github.com/cn/actions
http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html
https://www.qikqiak.com/post/use-github-actions-build-go-app/