Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14e631cca6 | ||
|
|
6207e24fb0 | ||
|
|
031de65d55 | ||
|
|
2176296991 | ||
|
|
c97f8b16aa | ||
|
|
6a1819e2a1 |
@@ -0,0 +1,45 @@
|
|||||||
|
name: cd
|
||||||
|
# main 的 ci 通過後:用 release-go 的 next-version 算下一版,沒有跳過就在剛通過 ci 的提交上打標籤並推送。
|
||||||
|
# 版本只遞增 patch、每位最大 9,沒有標籤時從 v0.0.1 開始。只動文件、圖示、測試、.gitea/、go.mod 時不發版(這些不會進映像檔,也不影響使用者)。
|
||||||
|
# 呼叫端用標籤引用這個 action(版本取自 Gitea 變數),所以發版後要到呼叫端把 ACTION_<名稱>_VERSION 改成新標籤才會生效。
|
||||||
|
# workflow_run 只看 main:ci 在 push main 時跑,成功才觸發;失敗或取消不發版。
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: [ci]
|
||||||
|
types: [completed]
|
||||||
|
branches: [main]
|
||||||
|
# 排隊、不取消:連續推送依序發版,版本不重複
|
||||||
|
concurrency:
|
||||||
|
group: cd
|
||||||
|
cancel-in-progress: false
|
||||||
|
jobs:
|
||||||
|
tag:
|
||||||
|
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main' }}
|
||||||
|
runs-on: docker-ubuntu
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
ref: ${{ github.event.workflow_run.head_sha }}
|
||||||
|
token: ${{ secrets.TOKEN || github.token }}
|
||||||
|
- id: ver
|
||||||
|
uses: https://gitea.jsc.idv.tw/actions/release-go@${{ vars.ACTION_RELEASE_GO_VERSION }}
|
||||||
|
with:
|
||||||
|
task: next-version
|
||||||
|
channel: release
|
||||||
|
go_registry: false
|
||||||
|
docs_pattern: '^\.gitea/|^icon\.|^test/|^go\.mod$|\.md$'
|
||||||
|
token: ${{ secrets.TOKEN || github.token }}
|
||||||
|
- name: 只動文件等檔案,不發版
|
||||||
|
if: ${{ steps.ver.outputs.skip == 'true' }}
|
||||||
|
run: echo "不發版"
|
||||||
|
- name: 打標籤並推送 ${{ steps.ver.outputs.version }}
|
||||||
|
if: ${{ steps.ver.outputs.skip != 'true' }}
|
||||||
|
env:
|
||||||
|
VERSION: ${{ steps.ver.outputs.version }}
|
||||||
|
TOKEN: ${{ secrets.TOKEN || github.token }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
git tag "$VERSION" HEAD
|
||||||
|
git push "${GITHUB_SERVER_URL%%://*}://oauth2:${TOKEN}@${GITHUB_SERVER_URL#*://}/${GITHUB_REPOSITORY}.git" "refs/tags/$VERSION"
|
||||||
|
echo "已推送標籤 $VERSION"
|
||||||
@@ -11,5 +11,5 @@ jobs:
|
|||||||
runs-on: docker-ubuntu
|
runs-on: docker-ubuntu
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: 預設輸入(沒有腳本、不跑測試)
|
- name: shellcheck(含 docker-check.sh)
|
||||||
uses: ./
|
uses: ./
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ action repo 的共用 CI:先 `shellcheck`,再(可選)在 `ci-env` 容器
|
|||||||
```yaml
|
```yaml
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: https://gitea.jsc.idv.tw/actions/action-ci@v0.0.1
|
- uses: https://gitea.jsc.idv.tw/actions/action-ci@${{ vars.ACTION_ACTION_CI_VERSION }}
|
||||||
with:
|
with:
|
||||||
go_test: true
|
go_test: true
|
||||||
docker_build: true
|
docker_build: true
|
||||||
@@ -18,8 +18,15 @@ steps:
|
|||||||
|`shellcheck_files`|`*.sh`|shellcheck 要檢查的檔案(空白分隔、可用萬用字元),沒有符合的就略過|
|
|`shellcheck_files`|`*.sh`|shellcheck 要檢查的檔案(空白分隔、可用萬用字元),沒有符合的就略過|
|
||||||
|`go_test`|`false`|`true`=跑 Go 測試|
|
|`go_test`|`false`|`true`=跑 Go 測試|
|
||||||
|`go_test_command`|`go vet ./... && go test ./test/`|Go 測試的指令|
|
|`go_test_command`|`go vet ./... && go test ./test/`|Go 測試的指令|
|
||||||
|`docker_build`|`false`|`true`=`docker build` 檢查 Dockerfile|
|
|`docker_build`|`false`|`true`=`docker build` 檢查 Dockerfile,映像檔標籤是 `action-test:<sha>`(唯一,不會撞到 runner 上的舊映像檔)|
|
||||||
|
|`docker_run_args`||建置後實際執行容器的 entrypoint 參數(每行一個),空=不執行|
|
||||||
|
|`docker_run_options`||執行容器的 `docker create` 選項(每行一個,例如 `--env=INPUT_TASK=x`)|
|
||||||
|
|`docker_run_expect`||容器輸出必須包含的字串,空=只檢查結束碼|
|
||||||
|
|
||||||
內部引用的 `shellcheck`、`ci-env` 版本寫死在 `action.yml`,升級要先發它們再發 `action-ci`。
|
不要對 Docker action 用 `uses: ./` 自測:這台 runner 會依名稱重用舊映像檔,可能跑到別的專案的映像檔。內部用的 `shellcheck`、`ci-env` 版本取自呼叫端 repo 的 Gitea 變數 `ACTION_SHELLCHECK_VERSION`、`ACTION_CI_ENV_VERSION`(不寫死在 `action.yml`),所以呼叫端要先設好這兩個變數。
|
||||||
|
|
||||||
圖示:`icon.svg`(頭像用 `icon.png`)。
|
圖示:`icon.svg`(頭像用 `icon.png`)。
|
||||||
|
|
||||||
|
## 發版
|
||||||
|
|
||||||
|
`main` 的 `ci` 通過後,`cd.yaml` 用 [release-go](https://gitea.jsc.idv.tw/actions/release-go) 算下一個版本並打標籤(只遞增 patch,每位最大 9);只動文件、圖示、測試、`.gitea/`、`go.mod` 時不發版。呼叫端要把 Gitea 變數 `ACTION_ACTION_CI_VERSION` 改成新標籤才會採用。
|
||||||
|
|||||||
+18
-5
@@ -1,5 +1,5 @@
|
|||||||
name: action-ci
|
name: action-ci
|
||||||
description: "action repo 的共用 CI:shellcheck → Go 腳本測試(在 ci-env 容器裡,Go 版本取 go.mod)→ 建置映像檔。要在 checkout 之後使用,各步驟用輸入開關"
|
description: "action repo 的共用 CI:shellcheck → Go 腳本測試(在 ci-env 容器裡,Go 版本取 go.mod)→ 建置映像檔。內部用的 shellcheck、ci-env 版本取自呼叫端的 Gitea 變數 ACTION_SHELLCHECK_VERSION、ACTION_CI_ENV_VERSION。要在 checkout 之後使用,各步驟用輸入開關"
|
||||||
inputs:
|
inputs:
|
||||||
shellcheck_files:
|
shellcheck_files:
|
||||||
description: "shellcheck 要檢查的檔案(空白分隔,可用萬用字元);沒有符合的檔案就略過"
|
description: "shellcheck 要檢查的檔案(空白分隔,可用萬用字元);沒有符合的檔案就略過"
|
||||||
@@ -11,18 +11,27 @@ inputs:
|
|||||||
description: "go_test 為 true 時執行的指令"
|
description: "go_test 為 true 時執行的指令"
|
||||||
default: "go vet ./... && go test ./test/"
|
default: "go vet ./... && go test ./test/"
|
||||||
docker_build:
|
docker_build:
|
||||||
description: "true=用 docker build 檢查 Dockerfile 建得起來"
|
description: "true=用 docker build 檢查 Dockerfile 建得起來(映像檔標籤唯一,不會撞到 runner 上的舊映像檔)"
|
||||||
default: "false"
|
default: "false"
|
||||||
|
docker_run_args:
|
||||||
|
description: "docker_build 之後實際執行容器的參數(entrypoint 的參數,每行一個);空=不執行"
|
||||||
|
default: ""
|
||||||
|
docker_run_options:
|
||||||
|
description: "執行容器的 docker create 選項(每行一個,例如 --env=INPUT_TASK=resolve-channel)"
|
||||||
|
default: ""
|
||||||
|
docker_run_expect:
|
||||||
|
description: "容器輸出必須包含的字串;空=只檢查結束碼"
|
||||||
|
default: ""
|
||||||
runs:
|
runs:
|
||||||
using: composite
|
using: composite
|
||||||
steps:
|
steps:
|
||||||
- name: shellcheck
|
- name: shellcheck
|
||||||
uses: https://gitea.jsc.idv.tw/actions/shellcheck@v0.0.1
|
uses: https://gitea.jsc.idv.tw/actions/shellcheck@${{ vars.ACTION_SHELLCHECK_VERSION }}
|
||||||
with:
|
with:
|
||||||
files: ${{ inputs.shellcheck_files }}
|
files: ${{ inputs.shellcheck_files }}
|
||||||
- name: Go 測試
|
- name: Go 測試
|
||||||
if: ${{ inputs.go_test == 'true' }}
|
if: ${{ inputs.go_test == 'true' }}
|
||||||
uses: https://gitea.jsc.idv.tw/actions/ci-env@v0.0.2
|
uses: https://gitea.jsc.idv.tw/actions/ci-env@${{ vars.ACTION_CI_ENV_VERSION }}
|
||||||
env:
|
env:
|
||||||
GO_VERSION: go.mod
|
GO_VERSION: go.mod
|
||||||
with:
|
with:
|
||||||
@@ -30,4 +39,8 @@ runs:
|
|||||||
- name: 建置映像檔
|
- name: 建置映像檔
|
||||||
if: ${{ inputs.docker_build == 'true' }}
|
if: ${{ inputs.docker_build == 'true' }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: docker build -t "action-test:$GITHUB_SHA" .
|
env:
|
||||||
|
RUN_ARGS: ${{ inputs.docker_run_args }}
|
||||||
|
RUN_OPTIONS: ${{ inputs.docker_run_options }}
|
||||||
|
RUN_EXPECT: ${{ inputs.docker_run_expect }}
|
||||||
|
run: bash "${{ github.action_path }}/docker-check.sh"
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# 建置映像檔(唯一標籤 action-test:<sha>,避免 runner 重用同名舊映像檔),可選擇實際執行一次並檢查輸出。
|
||||||
|
# 環境變數:RUN_OPTIONS、RUN_ARGS(各是「每行一個參數」的清單,空=不執行)、RUN_EXPECT(輸出必須包含的字串)。
|
||||||
|
# 用 docker create + docker cp 把 workspace 放進容器:job 本身可能也在容器裡,用 -v 掛載的路徑會對不上主機。
|
||||||
|
set -euo pipefail
|
||||||
|
image="action-test:${GITHUB_SHA:-local}"
|
||||||
|
docker build -t "$image" .
|
||||||
|
[ -n "${RUN_ARGS:-}" ] || exit 0
|
||||||
|
# 清單是「每行一個」,濾掉空行(多行 YAML 值結尾的換行會產生空字串,docker 會把它當成映像檔名稱)
|
||||||
|
mapfile -t opts < <(printf '%s\n' "${RUN_OPTIONS:-}" | sed '/^[[:space:]]*$/d')
|
||||||
|
mapfile -t args < <(printf '%s\n' "$RUN_ARGS" | sed '/^[[:space:]]*$/d')
|
||||||
|
id=$(docker create -w /w -e GITHUB_WORKSPACE=/w "${opts[@]}" "$image" "${args[@]}")
|
||||||
|
trap 'docker rm -f "$id" >/dev/null 2>&1 || true' EXIT
|
||||||
|
docker cp "$PWD/." "$id:/w"
|
||||||
|
out=$(mktemp)
|
||||||
|
rc=0
|
||||||
|
docker start -a "$id" >"$out" 2>&1 || rc=$?
|
||||||
|
cat "$out"
|
||||||
|
[ "$rc" -eq 0 ] || { echo "容器以 $rc 結束" >&2; exit 1; }
|
||||||
|
if [ -n "${RUN_EXPECT:-}" ] && ! grep -qF -- "$RUN_EXPECT" "$out"; then
|
||||||
|
echo "輸出沒有包含「$RUN_EXPECT」" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 685 B After Width: | Height: | Size: 685 B |
Reference in New Issue
Block a user