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