55 lines
2.4 KiB
YAML
55 lines
2.4 KiB
YAML
name: Verify mini program and deploy preview
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
concurrency:
|
|
group: pk-lab-xhs-production
|
|
cancel-in-progress: true
|
|
jobs:
|
|
verify-deploy:
|
|
runs-on: ubuntu-latest
|
|
container: { image: "docker:27-cli" }
|
|
timeout-minutes: 20
|
|
env:
|
|
SOURCE_SERVER: ${{ gitea.server_url }}
|
|
SOURCE_REPOSITORY: ${{ gitea.repository }}
|
|
SOURCE_REVISION: ${{ gitea.sha }}
|
|
DEPLOY_PORT: ${{ vars.DEPLOY_PORT || '3000' }}
|
|
API_BASE_URL: ${{ vars.API_BASE_URL }}
|
|
steps:
|
|
- name: Fetch exact revision
|
|
env: { SOURCE_TOKEN: "${{ secrets.GITEA_TOKEN || gitea.token }}" }
|
|
run: |
|
|
set -eu
|
|
apk add --no-cache python3 curl
|
|
python3 - <<'PY'
|
|
import io,os,shutil,tarfile,tempfile,urllib.request
|
|
from pathlib import Path
|
|
url=f"{os.environ['SOURCE_SERVER'].rstrip('/')}/api/v1/repos/{os.environ['SOURCE_REPOSITORY']}/archive/{os.environ['SOURCE_REVISION']}.tar.gz"
|
|
req=urllib.request.Request(url,headers={'Authorization':'token '+os.environ['SOURCE_TOKEN']})
|
|
with urllib.request.urlopen(req,timeout=60) as r:data=r.read()
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with tarfile.open(fileobj=io.BytesIO(data),mode='r:gz') as a:a.extractall(d,filter='data')
|
|
roots=list(Path(d).iterdir());shutil.copytree(roots[0],Path.cwd(),dirs_exist_ok=True)
|
|
PY
|
|
- name: Check source and preview
|
|
run: python3 scripts/check.py
|
|
- name: Build preview image
|
|
run: docker build -t "pk-lab-xhs:$SOURCE_REVISION" .
|
|
- name: Update permanent preview container
|
|
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
|
run: |
|
|
set -eu
|
|
docker network inspect pk-lab-public >/dev/null 2>&1 || docker network create pk-lab-public
|
|
docker rm -f pk-lab-xhs-preview 2>/dev/null || true
|
|
docker run -d --name pk-lab-xhs-preview --restart unless-stopped --network pk-lab-public --read-only --tmpfs /tmp --tmpfs /var/cache/nginx --tmpfs /var/run -e API_BASE_URL="$API_BASE_URL" -p "$DEPLOY_PORT:8080" "pk-lab-xhs:$SOURCE_REVISION"
|
|
for i in 1 2 3 4 5 6 7 8 9 10; do
|
|
status="$(docker inspect --format '{{.State.Health.Status}}' pk-lab-xhs-preview)"
|
|
[ "$status" = healthy ] && exit 0
|
|
[ "$status" = unhealthy ] && break
|
|
sleep 2
|
|
done
|
|
docker logs pk-lab-xhs-preview
|
|
exit 1
|