Skip to content

Commit b002af0

Browse files
committed
feat: add build deploy workflow
1 parent 7912025 commit b002af0

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
name: build_and_deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- production
8+
9+
workflow_dispatch:
10+
inputs:
11+
env:
12+
description: 'integration | production'
13+
required: true
14+
default: integration
15+
16+
jobs:
17+
set_env:
18+
runs-on: ubuntu-latest
19+
outputs:
20+
deploy_env: ${{ steps.set.outputs.deploy_env }}
21+
steps:
22+
- id: set
23+
run: |
24+
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
25+
echo "deploy_env=${{ inputs.env }}" >> "$GITHUB_OUTPUT"
26+
else
27+
case "${{ github.ref_name }}" in
28+
main)
29+
echo "deploy_env=staging" >> "$GITHUB_OUTPUT"
30+
;;
31+
production)
32+
echo "deploy_env=production" >> "$GITHUB_OUTPUT"
33+
;;
34+
*)
35+
echo "Unknown branch: ${{ github.ref_name }}"
36+
exit 1
37+
;;
38+
esac
39+
fi
40+
41+
build_api:
42+
needs: set_env
43+
runs-on: ubuntu-latest
44+
defaults:
45+
run:
46+
working-directory: ./api
47+
permissions:
48+
id-token: write
49+
contents: read
50+
env:
51+
DEPLOY_ENV: ${{ needs.set_env.outputs.deploy_env }}
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- name: Set up Docker Buildx
56+
uses: docker/setup-buildx-action@v3
57+
58+
- name: Configure AWS credentials
59+
uses: aws-actions/configure-aws-credentials@v4.2.1
60+
with:
61+
role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
62+
aws-region: ${{ vars.AWS_REGION }}
63+
64+
- name: Login to ECR
65+
id: login-ecr
66+
uses: aws-actions/amazon-ecr-login@v2
67+
68+
- name: Docker Login to ECR
69+
uses: docker/login-action@v3
70+
with:
71+
registry: ${{ steps.login-ecr.outputs.registry }}
72+
73+
- name: Build, tag, and push image to Amazon ECR
74+
uses: docker/build-push-action@v6
75+
with:
76+
context: .
77+
platforms: linux/amd64
78+
push: true
79+
cache-from: type=gha
80+
cache-to: type=gha,mode=max
81+
file: Dockerfile
82+
tags: |
83+
${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}/api:${{ github.sha }}
84+
${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}/api:${{ env.DEPLOY_ENV }}
85+
${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}/api:latest
86+
87+
build_ui:
88+
needs: set_env
89+
runs-on: ubuntu-latest
90+
defaults:
91+
run:
92+
working-directory: ./ui
93+
permissions:
94+
id-token: write
95+
contents: read
96+
env:
97+
DEPLOY_ENV: ${{ needs.set_env.outputs.deploy_env }}
98+
VITE_API_HOST: 'api.${{ needs.set_env.outputs.deploy_env }}.parakeet.vigetx.com'
99+
steps:
100+
- uses: actions/checkout@v4
101+
102+
- uses: pnpm/action-setup@v4
103+
name: Install pnpm
104+
with:
105+
version: 10
106+
run_install: false
107+
108+
- name: Install Node.js
109+
uses: actions/setup-node@v4
110+
with:
111+
node-version: 22
112+
cache: 'pnpm'
113+
114+
- name: Install dependencies
115+
run: pnpm install
116+
117+
- name: Build UI
118+
run: pnpm run build
119+
120+
- name: Upload dist folder
121+
uses: actions/upload-artifact@v4
122+
with:
123+
name: ui
124+
path: dist/
125+
126+
deploy:
127+
needs:
128+
- build_api
129+
- build_ui
130+
- set_env
131+
runs-on: ubuntu-latest
132+
env:
133+
DEPLOY_ENV: ${{ needs.set_env.outputs.deploy_env }}
134+
environment: ${{ needs.set_env.outputs.deploy_env }}
135+
permissions:
136+
id-token: write
137+
steps:
138+
- name: Setup SSH Keys and known_hosts
139+
env:
140+
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
141+
run: |
142+
mkdir -p -m 700 ~/.ssh
143+
ssh-agent -a $SSH_AUTH_SOCK > /dev/null
144+
ssh-add - <<< "${{ secrets.DEPLOY_KEY }}"
145+
ssh-keyscan ${{ vars.HOST }} >> ~/.ssh/known_hosts
146+
147+
- name: Deploy API
148+
env:
149+
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
150+
run: |
151+
ssh deploy@${{ vars.HOST }} 'cd /var/www/parakeet/${{ env.DEPLOY_ENV }}; echo "APP_IMAGE_TAG=${{ github.sha }}" > .env; docker compose pull && docker compose up -d --remove-orphans'
152+
153+
- name: Download UI dist folder
154+
uses: actions/download-artifact@v4
155+
with:
156+
name: ui
157+
path: .
158+
159+
- name: Deploy UI
160+
env:
161+
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
162+
run: |
163+
rsync -rzve ssh --del ./dist/ deploy@${{ vars.HOST }}:/var/www/parakeet/${{ env.DEPLOY_ENV }}/ui/

0 commit comments

Comments
 (0)