48 lines
1.6 KiB
YAML
48 lines
1.6 KiB
YAML
# Gitea Actions 工作流:构建 Docker 镜像并通过 SSH 触发部署
|
||
# 前置:Gitea Runner 已开放 Docker 访问(挂载 /var/run/docker.sock)
|
||
name: build-and-deploy
|
||
|
||
on:
|
||
push:
|
||
branches: [main]
|
||
workflow_dispatch:
|
||
|
||
env:
|
||
# 部署目标(CI/CD 与部署机同机,仅用 SSH 触发 start.sh)
|
||
DEPLOY_HOST: bh.vps.honor3.com
|
||
DEPLOY_USER: root
|
||
DEPLOY_DIR: /app/registry
|
||
SSH_KEY: /keypair/id_rsa
|
||
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: 上传 start.sh 到部署目录
|
||
run: |
|
||
install -m 600 "${SSH_KEY}" /tmp/deploy_key
|
||
mkdir -p ~/.ssh
|
||
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
|
||
ssh -i /tmp/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p ${DEPLOY_DIR}"
|
||
scp -i /tmp/deploy_key deploy/start.sh "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_DIR}/start.sh"
|
||
ssh -i /tmp/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" "chmod +x ${DEPLOY_DIR}/start.sh"
|
||
|
||
- name: 远程执行部署
|
||
run: |
|
||
ssh -i /tmp/deploy_key "${DEPLOY_USER}@${DEPLOY_HOST}" \
|
||
"IMAGE_TAG=${{ steps.meta.outputs.tag }} ${DEPLOY_DIR}/start.sh" |