This commit is contained in:
parent
1346abf733
commit
dbd9dc28af
1 changed files with 56 additions and 37 deletions
|
|
@ -1,40 +1,59 @@
|
||||||
name: Deploy
|
name: Deploy
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- 'v*'
|
- 'v*'
|
||||||
jobs:
|
jobs:
|
||||||
build-and-deploy:
|
build-and-deploy:
|
||||||
runs-on: ubuntu-full-24.04
|
runs-on: ubuntu-full-24.04
|
||||||
steps:
|
services:
|
||||||
- name: Checkout code
|
postgres:
|
||||||
uses: actions/checkout@v4
|
image: postgres:16-alpine
|
||||||
- name: Build
|
env:
|
||||||
run: task build
|
POSTGRES_USER: budgit_test
|
||||||
- name: Setup SSH
|
POSTGRES_PASSWORD: testpass
|
||||||
run: |
|
POSTGRES_DB: budgit_test
|
||||||
mkdir -p ~/.ssh
|
options: >-
|
||||||
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/deploy_key
|
--health-cmd "pg_isready -U budgit_test -d budgit_test"
|
||||||
chmod 600 ~/.ssh/deploy_key
|
--health-interval 5s
|
||||||
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
|
--health-timeout 3s
|
||||||
- name: Deploy binary
|
--health-retries 10
|
||||||
run: |
|
--health-start-period 10s
|
||||||
SSH_CMD="ssh -i ~/.ssh/deploy_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
|
steps:
|
||||||
${SSH_CMD} "cp ${{ secrets.DEPLOY_PATH }}/budgit ${{ secrets.DEPLOY_PATH }}/budgit.prev 2>/dev/null || true"
|
- name: Checkout code
|
||||||
scp -i ~/.ssh/deploy_key ./dist/budgit ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.DEPLOY_PATH }}/budgit.new
|
uses: actions/checkout@v4
|
||||||
${SSH_CMD} "mv ${{ secrets.DEPLOY_PATH }}/budgit.new ${{ secrets.DEPLOY_PATH }}/budgit && sudo systemctl restart budgit"
|
- name: Run tests
|
||||||
- name: Verify deployment
|
env:
|
||||||
run: |
|
BUDGIT_TEST_POSTGRES_URL: 'postgres://budgit_test:testpass@postgres:5432/budgit_test?sslmode=disable'
|
||||||
sleep 5
|
run: |
|
||||||
for i in 1 2 3 4 5; do
|
go tool templ generate
|
||||||
status=$(curl -s -o /dev/null -w "%{http_code}" "${{ secrets.APP_URL }}/healthz" || true)
|
task test:integration
|
||||||
[ "$status" = "200" ] && echo "Health check passed" && exit 0
|
- name: Build
|
||||||
echo "Attempt $i: got $status, retrying in 3s..."
|
run: task build
|
||||||
sleep 3
|
- name: Setup SSH
|
||||||
done
|
run: |
|
||||||
echo "Health check failed" && exit 1
|
mkdir -p ~/.ssh
|
||||||
- name: Rollback on failure
|
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/deploy_key
|
||||||
if: failure()
|
chmod 600 ~/.ssh/deploy_key
|
||||||
run: |
|
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
|
||||||
SSH_CMD="ssh -i ~/.ssh/deploy_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
|
- name: Deploy binary
|
||||||
${SSH_CMD} "[ -f ${{ secrets.DEPLOY_PATH }}/budgit.prev ] && mv ${{ secrets.DEPLOY_PATH }}/budgit.prev ${{ secrets.DEPLOY_PATH }}/budgit && sudo systemctl restart budgit"
|
run: |
|
||||||
|
SSH_CMD="ssh -i ~/.ssh/deploy_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
|
||||||
|
${SSH_CMD} "cp ${{ secrets.DEPLOY_PATH }}/budgit ${{ secrets.DEPLOY_PATH }}/budgit.prev 2>/dev/null || true"
|
||||||
|
scp -i ~/.ssh/deploy_key ./dist/budgit ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.DEPLOY_PATH }}/budgit.new
|
||||||
|
${SSH_CMD} "mv ${{ secrets.DEPLOY_PATH }}/budgit.new ${{ secrets.DEPLOY_PATH }}/budgit && sudo systemctl restart budgit"
|
||||||
|
- name: Verify deployment
|
||||||
|
run: |
|
||||||
|
sleep 5
|
||||||
|
for i in 1 2 3 4 5; do
|
||||||
|
status=$(curl -s -o /dev/null -w "%{http_code}" "${{ secrets.APP_URL }}/healthz" || true)
|
||||||
|
[ "$status" = "200" ] && echo "Health check passed" && exit 0
|
||||||
|
echo "Attempt $i: got $status, retrying in 3s..."
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
echo "Health check failed" && exit 1
|
||||||
|
- name: Rollback on failure
|
||||||
|
if: failure()
|
||||||
|
run: |
|
||||||
|
SSH_CMD="ssh -i ~/.ssh/deploy_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}"
|
||||||
|
${SSH_CMD} "[ -f ${{ secrets.DEPLOY_PATH }}/budgit.prev ] && mv ${{ secrets.DEPLOY_PATH }}/budgit.prev ${{ secrets.DEPLOY_PATH }}/budgit && sudo systemctl restart budgit"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue