-
Notifications
You must be signed in to change notification settings - Fork 0
102 lines (90 loc) · 3.23 KB
/
publish_prod_project.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
name: Build Test Deploy and Publish Production Project In AWS
on:
push:
branches:
- release/*
pull_request:
types: [opened, synchronize, reopened]
branches:
- release/*
jobs:
build_and_test:
name: Build_And_Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Install dependencies
run: npm install --force
continue-on-error: false
- name: Build
run: npm run build
continue-on-error: false
- name: Test
run: npm run test
continue-on-error: false
sonarcloud:
needs: build_and_test
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Install dependencies
run: npm install --force
continue-on-error: false
- name: Test and Generate Report Coverage
run: npm run test:coverage
continue-on-error: false
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
deploy_prod:
needs: sonarcloud
name: Production Build Deploy and Publish Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build Docker image
env:
DOCKER_CLI_AGGREGATE: 1
run: |
docker-compose -f docker-compose.prod.yml build
continue-on-error: false
- name: Push Docker image to Docker Hub
env:
DOCKER_LOGIN: ${{ secrets.DOCKER_LOGIN }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_CLI_AGGREGATE: 1
run: |
docker login -u ${{ secrets.DOCKER_LOGIN }} -p ${{ secrets.DOCKER_PASSWORD }}
docker push ${{ secrets.DOCKER_IMAGE_PROD }}
continue-on-error: false
- name: Execute SSH Commands into EC2
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USERNAME: ${{ secrets.EC2_USERNAME }}
AWS_SSH_KEY: ${{ secrets.AWS_SSH_KEY }}
DOCKER_LOGIN: ${{ secrets.DOCKER_LOGIN }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
port: 22
script: |
IMAGEM="${{ secrets.DOCKER_IMAGE_PROD }}"
container_id=$(docker ps -a -q --filter "ancestor=$IMAGEM")
image_id=$(docker images --format "{{.ID}}" "$IMAGEM")
docker login -u ${{ secrets.DOCKER_LOGIN }} -p ${{ secrets.DOCKER_PASSWORD }}
docker rm --force $container_id
docker rmi --force $image_id
docker pull $IMAGEM
image_id=$(docker images --format "{{.ID}}" "$IMAGEM")
docker run -d -p 80:80 $IMAGEM