-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (149 loc) · 6.04 KB
/
cicd-admin.yml
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Java CI/CD with Gradle
on:
push:
branches:
- dev
- admin
env:
AWS_REGION: ap-northeast-2
# Dev branch
jobs:
build-and-push-docker-image-dev:
if: github.ref == 'refs/heads/dev'
runs-on: ubuntu-latest
env:
DOCKER_IMAGE_NAME: dev-uket
DOCKER_CONTAINER_NAME: dev-uket
DOCKER_REGISTRY_URL: ${{ secrets.DEV_DOCKER_REGISTRY_URL }}
steps:
- uses: actions/checkout@v3
# Caching Gradle dependencies to speed up the build process
- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# Setting up JDK 21 for building the Java application
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
# Ensuring the Gradle wrapper script is executable
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Building the application with Gradle
- name: Build with Gradle
run: ./gradlew build -x test
# Configuring AWS credentials for accessing AWS services
- name: Configure AWS credentials for dev
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }}
aws-region: ${{ env.AWS_REGION }}
# Logging in to Amazon ECR
- name: Login to Amazon ECR for dev
uses: aws-actions/amazon-ecr-login@v1
# Building and pushing Docker image to ECR
- name: Docker build and push for dev
run: |
cd ./application/ticket-app-api
docker build --platform linux/amd64 -t ${{ env.DOCKER_IMAGE_NAME }} .
docker tag ${{ env.DOCKER_IMAGE_NAME }}:latest ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest
docker push ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest
echo "::set-output name=image::${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest"
deploy-dev:
if: github.ref == 'refs/heads/dev'
needs: build-and-push-docker-image-dev
runs-on: ubuntu-latest
steps:
- name: Deploy to Dev EC2 via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST_DEV }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY_DEV }}
script: |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ env.DOCKER_REGISTRY_URL }}
docker-compose down
docker pull ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}
docker-compose up -d
# Cleaning up unused Docker images
docker image prune -f
# Admin branch
build-and-push-docker-image-admin:
if: github.ref == 'refs/heads/admin'
runs-on: ubuntu-latest
env:
DOCKER_IMAGE_NAME: admin-uket
DOCKER_CONTAINER_NAME: admin-uket
DOCKER_REGISTRY_URL: ${{ secrets.ADMIN_DOCKER_REGISTRY_URL }}
steps:
- uses: actions/checkout@v3
# Caching Gradle dependencies to speed up the build process
- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# Setting up JDK 21 for building the Java application
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
# Ensuring the Gradle wrapper script is executable
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Building the application with Gradle
- name: Build with Gradle
run: ./gradlew clean build -x test
# Configuring AWS credentials for accessing AWS services
- name: Configure AWS credentials for admin
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_ADMIN }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_ADMIN }}
aws-region: ${{ env.AWS_REGION }}
# Logging in to Amazon ECR
- name: Login to Amazon ECR for admin
uses: aws-actions/amazon-ecr-login@v1
# Building and pushing Docker Image to ECR
- name: Docker build and push for admin
run: |
cd ./application/admin-app-api
docker build --platform linux/amd64 -t ${{ env.DOCKER_IMAGE_NAME }} .
docker tag ${{ env.DOCKER_IMAGE_NAME }}:latest ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest
docker push ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest
echo "::set-output name=image::${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}:latest"
deploy-admin:
if: github.ref == 'refs/heads/admin'
env:
DOCKER_IMAGE_NAME: admin-uket
DOCKER_CONTAINER_NAME: admin-uket
DOCKER_REGISTRY_URL: ${{ secrets.ADMIN_DOCKER_REGISTRY_URL }}
needs: build-and-push-docker-image-admin
runs-on: ubuntu-latest
steps:
- name: Deploy to Admin EC2 via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST_ADMIN }}
username: ubuntu
key: ${{ secrets.SSH_PRIVATE_KEY_ADMIN }}
script: |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${{ env.DOCKER_REGISTRY_URL }}
docker-compose down
docker pull ${{ env.DOCKER_REGISTRY_URL }}/${{ env.DOCKER_IMAGE_NAME }}
docker-compose up -d
# Cleaning up unused Docker images
docker image prune -f