1+ name : Build, Test and Deploy to Prod
2+
3+ # Trigger the workflow when changes are pushed to the main branch
4+ on :
5+ push :
6+ branches :
7+ - main
8+ workflow_dispatch :
9+
10+ jobs :
11+ build :
12+ runs-on : ubuntu-latest
13+
14+ steps :
15+ # Checkout code from the repository
16+ - name : Checkout code
17+ uses : actions/checkout@v2
18+
19+ # Cache dependencies to speed up build times
20+ - name : Cache dependencies
21+ uses : actions/cache@v3
22+ with :
23+ path : |
24+ app-service/.cargo
25+ app-service/target/
26+ auth-service/.cargo
27+ auth-service/target/
28+ key : ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
29+ restore-keys : ${{ runner.os }}-cargo-
30+
31+ - name : Install Rust
32+ run : rustup update stable && rustup default stable
33+
34+ - name : Build and test app-service code
35+ working-directory : ./app-service
36+ run : |
37+ cargo build --verbose
38+ cargo test --verbose
39+
40+ - name : Build and test auth-service code
41+ working-directory : ./auth-service
42+ run : |
43+ cargo build --verbose
44+ cargo test --verbose
45+
46+ # Set up Docker Buildx for multi-platform builds
47+ - name : Set up Docker Buildx
48+ uses : docker/setup-buildx-action@v2
49+
50+ - name : Log in to Docker Hub
51+ uses : docker/login-action@v2
52+ with :
53+ username : ${{ secrets.DOCKER_USERNAME }}
54+ password : ${{ secrets.DOCKER_PASSWORD }}
55+
56+ - name : Build and push Docker images
57+ uses : docker/bake-action@v2.3.0
58+ with :
59+ push : true
60+ files : |
61+ compose.yml
62+ compose.override.yml
63+ set : |
64+ *.cache-from=type=gha
65+ *.cache-to=type=gha,mode=max
66+
67+ deploy :
68+ needs : build
69+ runs-on : ubuntu-latest
70+
71+ steps :
72+ - name : Checkout code
73+ uses : actions/checkout@v2
74+
75+ - name : Log in to Docker Hub
76+ uses : docker/login-action@v1
77+ with :
78+ username : ${{ secrets.DOCKER_USERNAME }}
79+ password : ${{ secrets.DOCKER_PASSWORD }}
80+
81+ - name : Install sshpass
82+ run : sudo apt-get install sshpass
83+
84+ - name : Copy compose.yml to droplet
85+ run : sshpass -v -p '${{ secrets.DROPLET_PASSWORD }}' scp -o StrictHostKeyChecking=no compose.yml root@${{ vars.DROPLET_IP }}:~
86+
87+ - name : Deploy
88+ uses : appleboy/ssh-action@master
89+ with :
90+ host : ${{ vars.DROPLET_IP }}
91+ username : root
92+ password : ${{ secrets.DROPLET_PASSWORD }}
93+ script : |
94+ cd ~
95+ export AUTH_SERVICE_IP=${{ vars.DROPLET_IP }}
96+ docker compose down
97+ docker compose pull
98+ docker compose up -d
0 commit comments