-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (74 loc) · 2.61 KB
/
backend-ec2-deploy.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
name: Build on DockerHub & Deploy
to Ec2
on:
push:
branches:
- main
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
AWS_PRIVATE_KEY: ${{ secrets.AWS_PRIVATE_KEY }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: ./
push: true
dockerfile: ./Dockerfile
tags: junitalama/my-volunteer-app:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Set permissions for private key
run: |
echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem
chmod 600 key.pem
- name: Pull Docker image
run: |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker pull junitalama/my-volunteer-app:latest'
- name: Stop running container
run: |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker stop stupefied_raman || true'
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker rm stupefied_raman || true'
- name: Run new container
run: |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker run -d --name stupefied_raman -p 5000:5000 -e DB_URL=${{ secrets.DB_URL }} junitalama/my-volunteer-app:latest'
# name: EC2
# on:
# push:
# branches:
# - main
# jobs:
# deploy:
# name: Deploy to EC2 on main branch push
# runs-on: ubuntu-latest
# steps:
# - name: Checkout the files
# uses: actions/checkout@v4
# - name: Deploy to server
# uses: easingthemes/ssh-deploy@main
# env:
# SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
# REMOTE_HOST: ${{ secrets.HOST_DNS }}
# REMOTE_USER: ${{ secrets.USERNAME }}
# TARGET: ${{ secrets.TARGET_DIR }}