registry/.gitea/workflows/deploy.yml
cheney 41c717d319
Some checks failed
build-and-deploy / build-and-deploy (push) Failing after 11s
重试
2026-07-16 13:37:46 +08:00

54 lines
1.8 KiB
YAML
Raw 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.

# Gitea Actions 工作流:构建 Docker 镜像并通过 SSH 触发部署
# 前置:
# 1. Gitea Runner 已开放 Docker 访问(挂载 /var/run/docker.sock 到容器内)
# 2. 仓库 Secret 中已添加 SSH_PRIVATE_KEY内容为部署机私钥全文
name: build-and-deploy
on:
push:
branches: [main]
workflow_dispatch:
env:
DEPLOY_HOST: bh.vps.honor3.com
DEPLOY_USER: root
DEPLOY_DIR: /app/registry
IMAGE_NAME: registry-app
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 生成镜像 tag短 commit
id: meta
run: echo "tag=$(echo ${GITHUB_SHA} | cut -c1-8)" >> "$GITHUB_OUTPUT"
- name: 构建 Docker 镜像
run: |
docker build \
-t "${IMAGE_NAME}:${{ steps.meta.outputs.tag }}" \
-t "${IMAGE_NAME}:latest" \
.
- name: 准备 SSH 私钥
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh
printf "%s\n" "${SSH_PRIVATE_KEY}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
- name: 上传 start.sh 到部署目录
run: |
ssh -i ~/.ssh/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p ${DEPLOY_DIR}"
scp -i ~/.ssh/deploy_key deploy/start.sh "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_DIR}/start.sh"
ssh -i ~/.ssh/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" "chmod +x ${DEPLOY_DIR}/start.sh"
- name: 远程执行部署
run: |
ssh -i ~/.ssh/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" \
"IMAGE_TAG=${{ steps.meta.outputs.tag }} ${DEPLOY_DIR}/start.sh"