-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (100 loc) · 4.14 KB
/
dev-server-deployer.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
name: Develop Server Deployer (CD)
on: workflow_dispatch
jobs:
deploy:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Send discord notification (develop server deploy start)
uses: appleboy/discord-action@master
with:
webhook_id: ${{ secrets.SERVER_DEPLOY_DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.SERVER_DEPLOY_DISCORD_WEBHOOK_TOKEN }}
message: |
> **🌈 Server Deployment Start (Develop)**
>
> 🛢️ Repository : ${{ github.repository }}
> 🎋 Branch : ${{ github.ref }}
> 🔁 Run Attempt : ${{ github.run_attempt }}
> 🤗 Actor : ${{ github.triggering_actor }}
- name: Get Github Actions IP Addresses
id: publicip
run: |
response=$(curl -s canhazip.com)
echo "ip=$response" >> "$GITHUB_OUTPUT"
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: 'ap-northeast-2'
- name: Add GitHub Actions IP
run: |
aws ec2 authorize-security-group-ingress \
--group-id ${{ secrets.SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.publicip.outputs.ip }}/32
- name: Copy Docker Compose file to server
uses: appleboy/scp-action@master
with:
host: ${{ vars.INSTANCE_HOST }}
username: ${{ vars.INSTANCE_USERNAME }}
key: ${{ secrets.INSTANCE_PEM_KEY }}
source: "./idle-presentation/compose-dev.yaml"
target: "~/app/docker"
overwrite: true
- name: Install Docker if not present
uses: appleboy/[email protected]
with:
host: ${{ vars.INSTANCE_HOST }}
username: ${{ vars.INSTANCE_USERNAME }}
key: ${{ secrets.INSTANCE_PEM_KEY }}
script: |
if ! command -v docker >/dev/null 2>&1; then
echo "Installing Docker..."
sudo apt-get update
sudo apt-get install -y docker.io
else
echo "Docker already installed."
fi
if ! command -v docker-compose >/dev/null 2>&1; then
echo "Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose already installed."
fi
- name: Configuration Env file
uses: appleboy/ssh-action@master
env:
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
with:
host: ${{ vars.INSTANCE_HOST }}
username: ${{ vars.INSTANCE_USERNAME }}
key: ${{ secrets.INSTANCE_PEM_KEY }}
envs: VARS_CONTEXT,SECRETS_CONTEXT
script: |
cd ~/app/docker
jq -s '.[0] * .[1]' <(echo "$VARS_CONTEXT") <(echo "$SECRETS_CONTEXT") \
| jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' > .env
- name: Run Docker Compose up
uses: appleboy/ssh-action@master
with:
host: ${{ vars.INSTANCE_HOST }}
username: ${{ vars.INSTANCE_USERNAME }}
key: ${{ secrets.INSTANCE_PEM_KEY }}
script: |
echo "${{ secrets.DOCKER_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
sudo docker-compose -f ~/app/docker/compose-dev.yaml pull
sudo docker-compose -f ~/app/docker/compose-dev.yaml up -d --force-recreate
- name: Remove GitHub Actions IP
run: |
aws ec2 revoke-security-group-ingress \
--group-id ${{ secrets.SECURITY_GROUP_ID }} \
--protocol tcp \
--port 22 \
--cidr ${{ steps.publicip.outputs.ip }}/32