From 01cff316edeb5d153534cb919dac8c3285cc4fdf Mon Sep 17 00:00:00 2001 From: m1saka Date: Sat, 4 Jul 2026 21:54:16 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=94=B9=E7=94=A8=E7=BA=AF=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=96=B9=E5=BC=8F=E6=9E=84=E5=BB=BA,=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=97=A0=20Node=20=E7=9A=84=20runner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - runner 镜像缺少 node,JS 类 action(checkout/setup-go/upload-artifact)无法运行 - 改为 shell 步骤:apt 装 git/curl/tar,手动下载 Go 工具链 - 用 git 直接 clone 当前 commit,交叉编译 amd64/arm64 - 打 v* 标签时通过 Gitea API(curl)创建 Release 并上传二进制 --- .gitea/workflows/build.yml | 98 ++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index e8570d7..05ea1ac 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -10,47 +10,75 @@ on: jobs: build: runs-on: debian - strategy: - matrix: - include: - - goarch: amd64 - output: docker-compose-backup-linux-amd64 - - goarch: arm64 - output: docker-compose-backup-linux-arm64 steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: "1.26" - - - name: Build ${{ matrix.output }} - env: - CGO_ENABLED: "0" - GOOS: linux - GOARCH: ${{ matrix.goarch }} + - name: Install toolchain (git, curl, tar, go) run: | + set -e + export DEBIAN_FRONTEND=noninteractive + if ! command -v git >/dev/null || ! command -v curl >/dev/null || ! command -v tar >/dev/null; then + apt-get update + apt-get install -y --no-install-recommends git curl ca-certificates tar + fi + GO_VERSION=1.26.0 + curl -fsSL "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -o /tmp/go.tar.gz + rm -rf /usr/local/go + tar -C /usr/local -xzf /tmp/go.tar.gz + /usr/local/go/bin/go version + + - name: Clone repository + run: | + set -e + git init -q . + git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" 2>/dev/null || \ + git remote set-url origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + git -c http.extraHeader="Authorization: basic $(printf 'x-access-token:%s' "${GITHUB_TOKEN}" | base64 -w0)" \ + fetch --depth 1 origin "${GITHUB_SHA}" + git checkout -q FETCH_HEAD + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Build binaries + run: | + set -e + export PATH="/usr/local/go/bin:${PATH}" if [ "${GITHUB_REF_TYPE}" = "tag" ]; then VERSION="${GITHUB_REF_NAME}" else - VERSION="${GITHUB_REF_NAME}-${GITHUB_SHA::7}" + VERSION="${GITHUB_REF_NAME}-$(echo "${GITHUB_SHA}" | cut -c1-7)" fi - go build -trimpath \ - -ldflags "-s -w -X git.misaka.ren/M1saka/docker_backup/cmd/backup.Version=${VERSION}" \ - -o "${{ matrix.output }}" . + LDFLAGS="-s -w -X git.misaka.ren/M1saka/docker_backup/cmd/backup.Version=${VERSION}" + for arch in amd64 arm64; do + echo "building linux/${arch}" + CGO_ENABLED=0 GOOS=linux GOARCH="${arch}" \ + go build -trimpath -ldflags "${LDFLAGS}" \ + -o "docker-compose-backup-linux-${arch}" . + done + ls -lh docker-compose-backup-linux-* - - name: Upload artifact - uses: actions/upload-artifact@v3 - with: - name: ${{ matrix.output }} - path: ${{ matrix.output }} - - - name: Attach to release + - name: Publish release assets if: startsWith(github.ref, 'refs/tags/v') - uses: akkuman/gitea-release-action@v1 - with: - files: ${{ matrix.output }} + run: | + set -e + API="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" + TAG="${GITHUB_REF_NAME}" + AUTH="Authorization: token ${GITHUB_TOKEN}" + + # Find the release for this tag, or create it. + RELEASE_ID=$(curl -fsSL -H "${AUTH}" "${API}/releases/tags/${TAG}" | \ + grep -o '"id":[0-9]*' | head -n1 | cut -d: -f2 || true) + if [ -z "${RELEASE_ID}" ]; then + RELEASE_ID=$(curl -fsSL -X POST -H "${AUTH}" -H "Content-Type: application/json" \ + "${API}/releases" \ + -d "{\"tag_name\":\"${TAG}\",\"name\":\"${TAG}\"}" | \ + grep -o '"id":[0-9]*' | head -n1 | cut -d: -f2) + fi + echo "release id: ${RELEASE_ID}" + + for arch in amd64 arm64; do + f="docker-compose-backup-linux-${arch}" + curl -fsSL -X POST -H "${AUTH}" \ + -F "attachment=@${f};filename=${f}" \ + "${API}/releases/${RELEASE_ID}/assets" + done env: - NODE_OPTIONS: "--experimental-fetch" + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}