-
Notifications
You must be signed in to change notification settings - Fork 0
79 lines (63 loc) · 2.59 KB
/
ProdCICD.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
name: PROD CI/CD
on:
push:
branches: main
env:
PROFILE_PROD: prod
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew :Api:build
- name: Docker build
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_PROD_REPO }} .
docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_PROD_REPO }}:latest
- name: Get Public IP
id: publicip
run: |
response=$(curl -s canhazip.com)
echo "ip='$response'" >> "$GITHUB_OUTPUT"
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_PROD_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_PROD_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Add GitHub IP to AWS
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_PROD_SG_ID }} --protocol tcp --port ${{ secrets.EC2_SSH_PORT }} --cidr ${{ steps.publicip.outputs.ip }}/32
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_PROD_HOST }}
username: ${{ secrets.EC2_PROD_USERNAME }}
key: ${{ secrets.EC2_PROD_KEY }}
port: ${{ secrets.EC2_SSH_PORT }}
timeout: 60s
script: |
cd allchive-prod
sudo touch .env
echo "${{ secrets.ENV_PROD_VARS }}" | sudo tee .env > /dev/null
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker rmi $(sudo docker images -q)
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_PROD_REPO }}:latest
sudo docker-compose -f ~/allchive-prod/docker-compose.yml --env-file ~/allchive-prod/.env up --build -d
sudo docker system prune --all -f
- name: Remove IP FROM security group
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_PROD_SG_ID }} --protocol tcp --port ${{ secrets.EC2_SSH_PORT }} --cidr ${{ steps.publicip.outputs.ip }}/32