149 lines
4.3 KiB
YAML
149 lines
4.3 KiB
YAML
name: Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [ main, develop ]
|
|
tags:
|
|
- 'v*'
|
|
|
|
jobs:
|
|
deploy-staging:
|
|
name: Deploy Staging
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: docker:27-cli
|
|
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
archive_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/archive/${GITHUB_SHA}.tar.gz"
|
|
wget --header="Authorization: token ${GITHUB_TOKEN}" -O /tmp/repo.tar.gz "$archive_url"
|
|
tar -xzf /tmp/repo.tar.gz --strip-components=1 -C .
|
|
rm -f /tmp/repo.tar.gz
|
|
|
|
- name: Sync code to staging workspace
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
tar --exclude=.git -cf - . | docker run --rm -i \
|
|
-v /:/host \
|
|
docker:27-cli \
|
|
sh -lc '
|
|
set -eu
|
|
mkdir -p /host/var/lib/xiaoxia-saas-staging
|
|
rm -rf /host/var/lib/xiaoxia-saas-staging/repo
|
|
mkdir -p /host/var/lib/xiaoxia-saas-staging/repo
|
|
tar -xf - -C /host/var/lib/xiaoxia-saas-staging/repo
|
|
'
|
|
|
|
- name: Verify staging env file
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm -v /:/host docker:27-cli sh -lc 'test -f /host/var/lib/xiaoxia-saas-staging/.env'
|
|
|
|
- name: Prepare staging env
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm -v /:/host docker:27-cli sh -lc 'cp /host/var/lib/xiaoxia-saas-staging/.env /host/var/lib/xiaoxia-saas-staging/repo/.env'
|
|
|
|
- name: Deploy staging stack
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
-v /:/host \
|
|
docker:27-cli sh -lc '
|
|
chmod +x /host/var/lib/xiaoxia-saas-staging/repo/infra/docker/deploy-staging.sh && \
|
|
WEB_PORT=3001 /host/var/lib/xiaoxia-saas-staging/repo/infra/docker/deploy-staging.sh
|
|
'
|
|
|
|
- name: Verify staging health
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
i=0
|
|
while [ "$i" -lt 30 ]; do
|
|
if wget -qO- http://127.0.0.1:8000/health; then
|
|
exit 0
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 2
|
|
done
|
|
exit 1
|
|
|
|
deploy-production:
|
|
name: Deploy Production
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: docker:27-cli
|
|
if: startsWith(github.ref, 'refs/tags/v')
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
archive_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/archive/${GITHUB_SHA}.tar.gz"
|
|
wget --header="Authorization: token ${GITHUB_TOKEN}" -O /tmp/repo.tar.gz "$archive_url"
|
|
tar -xzf /tmp/repo.tar.gz --strip-components=1 -C .
|
|
rm -f /tmp/repo.tar.gz
|
|
|
|
- name: Sync code to production workspace
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
tar --exclude=.git -cf - . | docker run --rm -i \
|
|
-v /:/host \
|
|
docker:27-cli \
|
|
sh -lc '
|
|
set -eu
|
|
mkdir -p /host/var/lib/xiaoxia-saas-production
|
|
rm -rf /host/var/lib/xiaoxia-saas-production/repo
|
|
mkdir -p /host/var/lib/xiaoxia-saas-production/repo
|
|
tar -xf - -C /host/var/lib/xiaoxia-saas-production/repo
|
|
'
|
|
|
|
- name: Verify production env file
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm -v /:/host docker:27-cli sh -lc 'test -f /host/var/lib/xiaoxia-saas-production/.env'
|
|
|
|
- name: Prepare production env
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm -v /:/host docker:27-cli sh -lc 'cp /host/var/lib/xiaoxia-saas-production/.env /host/var/lib/xiaoxia-saas-production/repo/.env'
|
|
|
|
- name: Deploy production stack
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
-v /:/host \
|
|
docker:27-cli sh -lc '
|
|
chmod +x /host/var/lib/xiaoxia-saas-production/repo/infra/docker/deploy-production.sh && \
|
|
WEB_PORT=3001 /host/var/lib/xiaoxia-saas-production/repo/infra/docker/deploy-production.sh
|
|
'
|
|
|
|
- name: Verify production health
|
|
shell: sh
|
|
run: |
|
|
set -eu
|
|
i=0
|
|
while [ "$i" -lt 30 ]; do
|
|
if wget -qO- http://127.0.0.1:8000/health; then
|
|
exit 0
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 2
|
|
done
|
|
exit 1
|