25 lines
1.1 KiB
Bash
25 lines
1.1 KiB
Bash
#!/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
|
||
mapfile -t opts <<<"${RUN_OPTIONS:-}"
|
||
mapfile -t args <<<"$RUN_ARGS"
|
||
# 空字串清單 → 沒有選項
|
||
[ -n "${opts[0]:-}" ] || opts=()
|
||
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
|