-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (116 loc) · 4.58 KB
/
build-deploy.yaml
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
name: Docker Build Deploy
on:
push:
branches:
- main
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_IMAGE_NAME: ${{secrets.DOCKER_IMAGE_NAME}}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOST: ${{ secrets.SSH_HOST }}
jobs:
create-docker-image:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker image
run: |
cd server
docker build -f Dockerfile -t ${{secrets.DOCKER_USERNAME}}/${{secrets.DOCKER_IMAGE_NAME}}:latest .
- name: Push Docker image
run: docker push ${{secrets.DOCKER_USERNAME}}/${{secrets.DOCKER_IMAGE_NAME}}:latest
deployment:
needs: create-docker-image
runs-on: ubuntu-latest
steps:
- name: Install SSH client
run: sudo apt-get install -y openssh-client
- name: Create deployment script
run: |
cat << 'EOF' > deploy.sh
#!/bin/bash
set -e
# Ensure the user is added to the docker group
sudo usermod -aG docker $(whoami)
# Log out and back in to apply the group change
newgrp docker <<EONG
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker pull ${{ secrets.DOCKER_USERNAME }}/${{secrets.DOCKER_IMAGE_NAME}}:latest
# Navigate to your project directory
cd /home/ubuntu/projects/servdomain
sed -i 's|image: .*$|image: ${{ secrets.DOCKER_USERNAME }}/${{secrets.DOCKER_IMAGE_NAME}}|' docker-compose.yml
# Restart the Docker containers using Docker Compose
docker-compose down
docker-compose up -d
# Clean up
docker system prune -af
EOF
- name: Make deployment script executable
run: chmod +x deploy.sh
- name: Deploy to EC2
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_IMAGE_NAME: ${{secrets.DOCKER_IMAGE_NAME}}
run: |
echo "${{ secrets.EC2_SSH_KEY }}" > ec2-key.pem
chmod 600 ec2-key.pem
scp -i ec2-key.pem -o StrictHostKeyChecking=no deploy.sh ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:/home/${{ secrets.SSH_USERNAME }}/deploy.sh
ssh -o StrictHostKeyChecking=no -i ec2-key.pem ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }} /home/${{ secrets.SSH_USERNAME }}/deploy.sh
- name: Clean up
run: rm -f ec2-key.pem deploy.sh
- name: Wait for 10 seconds
run: sleep 10
health-check:
needs: deployment
runs-on: ubuntu-latest
steps:
- name: Health check
run: |
echo "${{ secrets.EC2_SSH_KEY }}" > ec2-key.pem
chmod 600 ec2-key.pem
ssh -o StrictHostKeyChecking=no -i ec2-key.pem ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }} "curl -f http://localhost:5000/health || exit 1"
env:
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
- name: Notify on failure
if: failure()
uses: actions/github-script@v5
with:
script: |
const message = "Deployment failed. Health check did not return 200.";
// Check if the event has an associated issue or pull request
if (context.payload.pull_request) {
// Add a comment to the pull request
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: message
});
} else if (context.payload.issue) {
// Add a comment to the issue
github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: message
});
} else {
// Create a new issue if no associated pull request or issue is found
github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Deployment failed",
body: message
});
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}