-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (86 loc) · 3.32 KB
/
blue-green-cd.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
name: Blue/Green CD for Backend
on:
push:
#paths: ["backend/**"]
#branches: ["release", "*hotfix*"]
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
GITHUB_SHA: ${{ github.sha }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Generate .env file and SSL files
run: |
cd backend
echo "${{ secrets.ENV_FILE }}" > .env
echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" >> .env
echo "GITHUB_SHA=${{ github.sha }}" >> .env
- name: Add Remote Host Key to known_hosts
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PUBLIC_KEY }}" >> ~/.ssh/known_hosts
- name: Copy some files needed for Deployment to Remote Server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
source: "backend/.env,backend/*.sh,backend/compose*deploy.yml,backend/*nginx*"
target: "~/app/"
overwrite: true
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Setup docker-compose
uses: KengoTODA/actions-setup-docker-compose@v1.2.1
with:
version: "2.24.5"
- name: Create a Builder Instance
run: |
docker buildx create --name buildx
docker buildx use buildx
- name: Build & Push Docker Images (Blue & Green)
run: |
cp package.json backend/package.json
cp package-lock.json backend/package-lock.json
cp -r packages backend/packages
cd backend
docker-compose -f compose.blue-build.yml build --builder buildx --build-arg BUILDKIT_INLINE_CACHE=1
docker-compose -f compose.green-build.yml build --builder buildx --build-arg BUILDKIT_INLINE_CACHE=1
docker-compose -f compose.blue-build.yml push
docker-compose -f compose.green-build.yml push
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Run a New Version of the application on Remote Server
uses: appleboy/ssh-action@master
env:
SLACK_WEBHOOK_URI: ${{ secrets.SLACK_WEBHOOK_URI_FOR_GITHUB_ACTIONS }}
GITHUB_WORKFLOW: ${{ github.workflow }}
GITHUB_REF: ${{ github.ref }}
GITHUB_ACTOR: ${{ github.actor }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: SLACK_WEBHOOK_URI,GITHUB_WORKFLOW,GITHUB_REF,GITHUB_ACTOR
script: |
cd ~/app/backend
chmod +x deploy.sh
DEPLOYMENT_RESULT='Success'
./deploy.sh || DEPLOYMENT_RESULT='Fail'
chmod +x send-slack-message.sh
./send-slack-message.sh $SLACK_WEBHOOK_URI $DEPLOYMENT_RESULT $GITHUB_REF $GITHUB_ACTOR $GITHUB_WORKFLOW