-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (133 loc) · 4.87 KB
/
Copy pathdeploy.yml
File metadata and controls
152 lines (133 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: CI and Deploy
on:
pull_request:
push:
branches:
- main
workflow_dispatch:
inputs:
rebuild_migrate:
description: Rebuild and publish the migrate image before deploy
required: false
default: false
type: boolean
env:
REGISTRY: ghcr.io
APP_IMAGE_NAME: ${{ github.repository }}
MIGRATE_IMAGE_NAME: ${{ github.repository }}-migrate
jobs:
verify:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Set up Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Typecheck
run: bun typecheck
- name: Test
run: bun test
- name: Build app bundle
run: bun run build
publish-and-deploy:
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
needs: verify
runs-on: ubuntu-24.04-arm
environment: production
concurrency:
group: production-deploy
cancel-in-progress: true
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Resolve migrate image
id: migrate-image
env:
FORCE_REBUILD_MIGRATE: ${{ github.event_name == 'workflow_dispatch' && inputs.rebuild_migrate || 'false' }}
MIGRATE_IMAGE_TAG: inputs-${{ hashFiles('Dockerfile', 'package.json', 'bun.lock', 'drizzle.config.ts', 'drizzle/**', 'src/db/**') }}
run: |
set -euo pipefail
cache_image="$REGISTRY/$MIGRATE_IMAGE_NAME:$MIGRATE_IMAGE_TAG"
build_migrate=false
if [ "$FORCE_REBUILD_MIGRATE" = "true" ]; then
build_migrate=true
elif ! docker buildx imagetools inspect "$cache_image" >/dev/null 2>&1; then
build_migrate=true
fi
echo "build=$build_migrate" >> "$GITHUB_OUTPUT"
echo "cache_image=$cache_image" >> "$GITHUB_OUTPUT"
- name: Build and push app image
uses: docker/build-push-action@v7
with:
context: .
pull: true
platforms: linux/arm64
push: true
target: release
tags: |
${{ env.REGISTRY }}/${{ env.APP_IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.APP_IMAGE_NAME }}:sha-${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push migrate image
if: steps.migrate-image.outputs.build == 'true'
uses: docker/build-push-action@v7
with:
context: .
pull: true
platforms: linux/arm64
push: true
target: migrate
tags: |
${{ steps.migrate-image.outputs.cache_image }}
${{ env.REGISTRY }}/${{ env.MIGRATE_IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.MIGRATE_IMAGE_NAME }}:sha-${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Configure SSH
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
run: |
install -m 700 -d ~/.ssh
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "$DEPLOY_HOST" > ~/.ssh/known_hosts
- name: Deploy release
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
APP_IMAGE: ${{ env.REGISTRY }}/${{ env.APP_IMAGE_NAME }}:sha-${{ github.sha }}
GHCR_USERNAME: ${{ github.actor }}
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MIGRATE_IMAGE: ${{ steps.migrate-image.outputs.cache_image }}
run: |
printf '%s\n' "$GHCR_TOKEN" | ssh "$DEPLOY_USER@$DEPLOY_HOST" "docker login ghcr.io -u '$GHCR_USERNAME' --password-stdin"
ssh "$DEPLOY_USER@$DEPLOY_HOST" "mkdir -p '$DEPLOY_PATH'"
cat docker-compose.deploy.yml | ssh "$DEPLOY_USER@$DEPLOY_HOST" "cat > '$DEPLOY_PATH/docker-compose.deploy.yml'"
ssh "$DEPLOY_USER@$DEPLOY_HOST" <<EOF
set -e
cd "$DEPLOY_PATH"
export KLEIS_APP_IMAGE="$APP_IMAGE"
export KLEIS_MIGRATE_IMAGE="$MIGRATE_IMAGE"
docker compose -f docker-compose.deploy.yml pull
docker compose -f docker-compose.deploy.yml run --rm --interactive=false migrate
docker compose -f docker-compose.deploy.yml up -d app
docker image prune -af
EOF