Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0289933c72 | ||
|
|
a39fa3332f | ||
|
|
5f48d105bb | ||
|
|
14e631cca6 | ||
|
|
6207e24fb0 | ||
|
|
031de65d55 | ||
|
|
2176296991 | ||
|
|
c97f8b16aa | ||
|
|
6a1819e2a1 |
@@ -0,0 +1,22 @@
|
|||||||
|
name: cd
|
||||||
|
# 只在「任何分支 → main」的 PR 合併時發版:action-cd 用 next-version 算下一版,沒有跳過就在 PR 來源分支的最新提交(pull_request.head.sha)上打標籤並推送。
|
||||||
|
# branches 寫死 main,來源分支不限;PR 沒合併就關閉時,tag 工作在工作層級跳過。直接推送 main 不發版。
|
||||||
|
# 版本只遞增 patch、每位最大 9,沒有標籤時從 v0.0.1 開始。只動文件、圖示、測試、.gitea/、go.mod 時不發版(這些不會進映像檔,也不影響使用者)。
|
||||||
|
# 呼叫端用標籤引用這個 action(版本取自 Gitea 變數),所以發版後要到呼叫端把 ACTION_<名稱>_VERSION 改成新標籤才會生效。
|
||||||
|
# 沒有手動觸發:發版失敗時重跑這個 workflow 的 run。
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [closed]
|
||||||
|
branches: [main]
|
||||||
|
# 排隊、不取消:連續推送依序發版,版本不重複
|
||||||
|
concurrency:
|
||||||
|
group: cd
|
||||||
|
cancel-in-progress: false
|
||||||
|
jobs:
|
||||||
|
tag:
|
||||||
|
if: ${{ github.event.pull_request.merged == true }}
|
||||||
|
runs-on: docker-ubuntu
|
||||||
|
steps:
|
||||||
|
- uses: https://gitea.jsc.idv.tw/actions/action-cd@${{ vars.ACTION_ACTION_CD_VERSION }}
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.TOKEN || github.token }}
|
||||||
@@ -11,5 +11,6 @@ jobs:
|
|||||||
runs-on: docker-ubuntu
|
runs-on: docker-ubuntu
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: 預設輸入(沒有腳本、不跑測試)
|
- uses: ./
|
||||||
uses: ./
|
with:
|
||||||
|
checkout: false
|
||||||
|
|||||||
@@ -1,25 +1,35 @@
|
|||||||
# action-ci
|
# action-ci
|
||||||
|
|
||||||
action repo 的共用 CI:先 `shellcheck`,再(可選)在 `ci-env` 容器裡跑 Go 腳本測試(`GO_VERSION=go.mod`,用 go.mod 的版本),最後(可選)`docker build` 檢查映像檔。
|
action repo 的共用 CI,自動偵測、不必設開關:
|
||||||
|
|
||||||
|
1. checkout(可關)
|
||||||
|
2. `shellcheck`(沒有符合的檔案就略過)
|
||||||
|
3. Go 腳本測試:有 `go.mod` 與 `test/` 才跑,在 `ci-env` 容器裡執行,`GO_VERSION=go.mod`(用 go.mod 的版本)
|
||||||
|
4. 建置映像檔:有 `Dockerfile` 才跑,映像檔標籤是 `action-test:<sha>`(唯一,不會撞到 runner 上的舊映像檔);有設 `docker_run_args` 時再實際執行一次並檢查輸出
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: https://gitea.jsc.idv.tw/actions/action-ci@${{ vars.ACTION_ACTION_CI_VERSION }}
|
||||||
- uses: https://gitea.jsc.idv.tw/actions/action-ci@v0.0.1
|
|
||||||
with:
|
|
||||||
go_test: true
|
|
||||||
docker_build: true
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 輸入
|
## 輸入
|
||||||
|
|
||||||
|名稱|預設|說明|
|
|名稱|預設|說明|
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
|`shellcheck_files`|`*.sh`|shellcheck 要檢查的檔案(空白分隔、可用萬用字元),沒有符合的就略過|
|
|`checkout`|`true`|先 checkout;`false`=呼叫端已經 checkout(例如要用 `uses: ./` 測自己)|
|
||||||
|`go_test`|`false`|`true`=跑 Go 測試|
|
|`shellcheck_files`|`*.sh`|shellcheck 要檢查的檔案(空白分隔、可用萬用字元)|
|
||||||
|`go_test_command`|`go vet ./... && go test ./test/`|Go 測試的指令|
|
|`go_test_command`|`go vet ./... && go test ./test/`|有 `go.mod` 與 `test/` 時執行的指令|
|
||||||
|`docker_build`|`false`|`true`=`docker build` 檢查 Dockerfile|
|
|`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 會依名稱重用舊映像檔,可能跑到別的專案的映像檔。
|
||||||
|
|
||||||
圖示:`icon.svg`(頭像用 `icon.png`)。
|
內部用的 `shellcheck`、`ci-env` 版本取自呼叫端 repo 的 Gitea 變數 `ACTION_SHELLCHECK_VERSION`、`ACTION_CI_ENV_VERSION`(不寫死在 `action.yml`),所以呼叫端要先設好這兩個變數。
|
||||||
|
|
||||||
|
## 開發與發版
|
||||||
|
|
||||||
|
- CI(`ci.yaml`):用自己(`uses: ./`)檢查 shellcheck。
|
||||||
|
- CD(`cd.yaml`):「任何分支 → `main`」的 PR 合併時,用 [action-cd](https://gitea.jsc.idv.tw/actions/action-cd) 算下一個版本([next-version](https://gitea.jsc.idv.tw/actions/next-version),只遞增 patch、每位最大 9)並在 PR 來源最新提交上打標籤;只動文件、圖示、測試、`.gitea/`、`go.mod` 時不發版。直接推送 `main` 不發版。
|
||||||
|
- 呼叫端要把 Gitea 變數 `ACTION_ACTION_CI_VERSION` 改成新標籤才會採用。
|
||||||
|
- 圖示:`icon.svg`(儲存庫頭像用 `icon.png`)。
|
||||||
|
|||||||
+32
-13
@@ -1,33 +1,52 @@
|
|||||||
name: action-ci
|
name: action-ci
|
||||||
description: "action repo 的共用 CI:shellcheck → Go 腳本測試(在 ci-env 容器裡,Go 版本取 go.mod)→ 建置映像檔。要在 checkout 之後使用,各步驟用輸入開關"
|
description: "action repo 的共用 CI:checkout → shellcheck → Go 腳本測試(有 go.mod 與 test/ 才跑,在 ci-env 容器裡,Go 版本取 go.mod)→ 建置映像檔(有 Dockerfile 才跑,可選擇實際執行並檢查輸出)。自動偵測,各 repo 的 ci.yaml 不必設開關。內部用的 shellcheck、ci-env 版本取自呼叫端的 Gitea 變數 ACTION_SHELLCHECK_VERSION、ACTION_CI_ENV_VERSION"
|
||||||
inputs:
|
inputs:
|
||||||
|
checkout:
|
||||||
|
description: "true=先 checkout;false=呼叫端已經 checkout(例如要用 uses: ./ 測自己)"
|
||||||
|
default: "true"
|
||||||
shellcheck_files:
|
shellcheck_files:
|
||||||
description: "shellcheck 要檢查的檔案(空白分隔,可用萬用字元);沒有符合的檔案就略過"
|
description: "shellcheck 要檢查的檔案(空白分隔,可用萬用字元);沒有符合的檔案就略過"
|
||||||
default: "*.sh"
|
default: "*.sh"
|
||||||
go_test:
|
|
||||||
description: "true=跑 Go 測試"
|
|
||||||
default: "false"
|
|
||||||
go_test_command:
|
go_test_command:
|
||||||
description: "go_test 為 true 時執行的指令"
|
description: "有 go.mod 與 test/ 時執行的指令"
|
||||||
default: "go vet ./... && go test ./test/"
|
default: "go vet ./... && go test ./test/"
|
||||||
docker_build:
|
docker_run_args:
|
||||||
description: "true=用 docker build 檢查 Dockerfile 建得起來"
|
description: "docker build 之後實際執行容器的參數(entrypoint 的參數,每行一個);空=只建置不執行"
|
||||||
default: "false"
|
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: checkout
|
||||||
|
if: ${{ inputs.checkout == 'true' }}
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: 偵測
|
||||||
|
id: detect
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
[ -f go.mod ] && [ -d test ] && echo "go=true" >> "$GITHUB_OUTPUT" || echo "go=false" >> "$GITHUB_OUTPUT"
|
||||||
|
[ -f Dockerfile ] && echo "docker=true" >> "$GITHUB_OUTPUT" || echo "docker=false" >> "$GITHUB_OUTPUT"
|
||||||
- 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: ${{ steps.detect.outputs.go == '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:
|
||||||
command: ${{ inputs.go_test_command }}
|
command: ${{ inputs.go_test_command }}
|
||||||
- name: 建置映像檔
|
- name: 建置映像檔
|
||||||
if: ${{ inputs.docker_build == 'true' }}
|
if: ${{ steps.detect.outputs.docker == '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