Files
action-ci/docker-check.sh
T

24 lines
1.3 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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