Repository navigation
87 lines (74 loc) · 2.65 KB
/
Copy pathdocker-image.yml
File metadata and controls
87 lines (74 loc) · 2.65 KB
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
name: 'Docker 镜像构建'
# 触发时机:
# 1. push 到 main 或开 PR:构建镜像并上传工件,只做自检,不发布 Release;
# 2. 推送 v* 标签,或用户在 GitHub 上「发布」一个版本:额外把镜像 tar 包挂到那个 Release 上。
# 说明:release.yml 的 Release 流水线也会构建同名单平台镜像,两条流水线产出同名产物,重复上传只是覆盖。
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
tags:
- 'v*'
release:
types: [published, released]
permissions:
contents: write
# PR 与 push 可能同时触发,串行执行避免并发争抢同一工件名
concurrency:
group: 'docker-${{ github.workflow }}-${{ github.ref }}'
cancel-in-progress: false
env:
BIN_NAME: simple-web-shell
jobs:
docker-build:
name: '构建镜像 ${{ matrix.arch }}'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- { arch: amd64, platform: 'linux/amd64' }
- { arch: arm64, platform: 'linux/arm64' }
steps:
- name: '检出代码'
uses: actions/checkout@v4
- name: 'Go 单元测试'
uses: actions/setup-go@v5
with:
go-version: '1.25'
- name: '运行测试'
run: go test ./...
- name: '静态检查 go vet'
run: go vet ./...
- name: '启用 QEMU(arm64 镜像构建)'
uses: docker/setup-qemu-action@v3
- name: '启用 Buildx'
uses: docker/setup-buildx-action@v3
- name: '构建镜像并导出 tar 包'
run: |
set -euo pipefail
out="$BIN_NAME-image-${{ matrix.arch }}.tar"
# docker 导出器只接受单平台结果,所以按矩阵逐个架构导出,而不是一次构建多平台
docker buildx build \
--platform ${{ matrix.platform }} \
--tag "$BIN_NAME:${{ matrix.arch }}" \
--output "type=docker,dest=$out" \
.
ls -lh "$out"
gzip -9 -f "$out"
- name: '上传镜像工件'
uses: actions/upload-artifact@v4
with:
name: 'docker-image-${{ matrix.arch }}'
path: '${{ env.BIN_NAME }}-image-${{ matrix.arch }}.tar.gz'
if-no-files-found: 'error'
retention-days: 7
- name: '上传镜像包到 Release'
if: github.event_name == 'release' || startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ github.event.release.tag_name || github.ref_name }}
files: '${{ env.BIN_NAME }}-image-${{ matrix.arch }}.tar.gz'
fail_on_unmatched_files: false