-
Notifications
You must be signed in to change notification settings - Fork 45
135 lines (126 loc) · 5.25 KB
/
file-go.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
name: 'go构建并下载'
on:
repository_dispatch:
workflow_dispatch:
inputs:
repoPath:
description: 'git仓库路径'
required: true
repoBranch:
description: '指定分支'
required: false
shellUrl:
description: '构建前运行脚本url'
required: false
shell:
description: '构建前运行shell命令:cd,go mod等'
required: false
GO_VERSION:
description: 'go版本,默认最新go稳定版'
required: true
default: 'stable'
UPLOAD_RELEASE:
description: '是否上传到RELEASE页面'
required: false
type: boolean
default: false
UPLOAD_TRANSFER:
description: '上传服务,如wet,见github.com/Mikubill/transfer#support'
required: false
UPLOAD_TAG:
description: '上传文件用的TAG,默认使用构建时间'
required: false
platforms:
description: '需要构建的平台'
required: true
default: linux/amd64,linux/arm64
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check Out
uses: actions/checkout@main
# 产生tag
- name: Generate tag
id: tag
run: |
tag=build-go_$(date +'%m-%d_%H-%M-%S')
[ ! "${{github.event.inputs.UPLOAD_TAG}}" = "" ] && tag=${{github.event.inputs.UPLOAD_TAG}}
echo "tag=$tag" >> $GITHUB_OUTPUT
# 设置go环境
- name: Set up Go
uses: actions/setup-go@main
with:
go-version: ${{ github.event.inputs.GO_VERSION }}
# 克隆目标,并且准备上下文
- name: Git clone
run: |
rm -rf {*,.[^.]*,..?*}
if [[ "${{ github.event.inputs.repoBranch }}" ]] ;then git clone --depth 1 -b "${{ github.event.inputs.repoBranch }}" "${{github.event.inputs.repoPath}}" . ; else git clone --depth 1 "${{github.event.inputs.repoPath}}" . ; fi
if [[ "${{ github.event.inputs.shellUrl }}" ]] ;then wget "${{github.event.inputs.shellUrl}}" -O /tmp/.customize_shell.sh ; chmod +x /tmp/.customize_shell.sh && /tmp/.customize_shell.sh; fi
# 开始构建
- name: Build
id: build
run: |
rm -rf /tmp/dist
mkdir -p /tmp/dist
mkdir -p /tmp/build_log
echo -e "github actions build\nstart time: $(date +'%Y-%m-%d %H:%M:%S')\n" > release.txt
start=$(date +%s)
platforms=${{ github.event.inputs.platforms }}
echo "start build at $start, all platforms: $platforms"
platforms1=(${platforms//,/ })
if [[ "${{ github.event.inputs.shell }}" ]] ;then ""${{github.event.inputs.shell}} ; fi
for platforms2 in ${platforms1[*]}; do
p=(${platforms2//\// })
V_OS="${p[0]}"
V_ARCH="${p[1]}"
V_SUFFIX=""
[ "${V_OS}" == 'windows' ] && V_SUFFIX='.exe'
echo "strart build $platforms2 -> ${V_OS}_${V_ARCH}"
echo "/tmp/dist/${V_OS}_${V_ARCH}" >> /tmp/files
CGO_ENABLED=0 GOOS=${V_OS} GOARCH=${V_ARCH} go build -o "/tmp/dist/${V_OS}_${V_ARCH}${V_SUFFIX}" -ldflags="-w -s" -v 2>&1 | sed "s/^/[${V_OS}_${V_ARCH}]/" | tee "/tmp/build_log/${V_OS}_${V_ARCH}.log" \
&& echo "build over $platforms2 -> ${V_OS}_${V_ARCH} use $(( $(date +%s) - start ))s" \
&& echo "${V_OS}_${V_ARCH} build use $(( $(date +%s) - start ))s" >> release.txt \
&& echo "::notice ::$platforms2 -> ${V_OS}_${V_ARCH} build use $(( $(date +%s) - start ))s" \
|| (echo "::error ::build fail $platforms2 -> ${V_OS}_${V_ARCH} use $(( $(date +%s) - start ))s" && cat /tmp/build_log/${V_OS}_${V_ARCH}.log) &
done
wait
end=$(date +%s)
take=$(( end - start ))
echo "end time: $(date +'%Y-%m-%d %H:%M:%S')" >> release.txt
echo -e "all build use ${take}s\n" >> release.txt
tar -zcvf dist.tar.gz -C /tmp/dist $(ls /tmp/dist) > /dev/null
# 上传构建日志
- name: Upload build logs
uses: actions/upload-artifact@main
with:
name: log_${{ steps.tag.outputs.tag }}
path: /tmp/build_log
retention-days: 1
# 上传构建产物
- name: Upload firmware directory
uses: actions/upload-artifact@main
with:
name: ${{ steps.tag.outputs.tag }}
path: dist.tar.gz
retention-days: 1
# 上传到中转服务
- name: Upload firmware to transfer
if: github.event.inputs.UPLOAD_TRANSFER && !failure() && !cancelled()
run: |
curl -fsSL git.io/file-transfer | sh
./transfer ${{ github.event.inputs.UPLOAD_TRANSFER }} --no-progress dist.tar.gz 2>&1 | tee transfer.log
url=$(cat transfer.log | grep https | cut -f3 -d" ")
echo "🔗 External Download Link: $url" >> release.txt
echo "::warning ::External Download Link: $url"
# 生成release
- name: Upload firmware to release
uses: softprops/action-gh-release@master
if: github.event.inputs.UPLOAD_RELEASE == 'true' && !failure() && !cancelled()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag.outputs.tag }}
body_path: release.txt
files: /tmp/dist/*