This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (133 loc) · 5.56 KB
/
production_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
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
name: Production Server Deployer (CD)
on:
workflow_dispatch:
inputs:
version:
description: 'Image version tag (e.g., 0.2411.135 or latest)'
required: true
default: 'latest'
jobs:
deploy:
needs: validate_version
runs-on: ubuntu-latest
environment: PROD
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Send discord notification (production server deploy start)
uses: appleboy/discord-action@master
with:
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }}
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }}
message: |
> **🛰️ Server Deployment Start (Prod)**
>
> 🛢️ Repository : ${{ github.repository }}
> 🎋 Branch : ${{ github.ref }}
> 📐 Version : ${{ github.event.inputs.version }}
> 🔁 Run Attempt : ${{ github.run_attempt }}
> 🤗 Actor : ${{ github.triggering_actor }}
- 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
- 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 ~
jq -s '.[0] * .[1] | del(.INSTANCE_PEM_KEY)' <(echo "$VARS_CONTEXT") <(echo "$SECRETS_CONTEXT") \
| jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' > .env
- name: Set deployed image version
id: set_version
run: echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
- name: Run Docker Image
id: deploy
uses: appleboy/ssh-action@master
with:
host: ${{ vars.INSTANCE_HOST }}
username: ${{ vars.INSTANCE_USERNAME }}
key: ${{ secrets.INSTANCE_PEM_KEY }}
script: |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull public.ecr.aws/u1i8r6z9/weave_server_dev_cr:${{ github.event.inputs.version }}
sudo docker stop weave_server_prod || true
sudo docker rm weave_server_prod || true
sudo docker run --name weave_server_prod --env-file ./.env -d -p 8080:8080 public.ecr.aws/u1i8r6z9/weave_server_dev_cr:${{ github.event.inputs.version }}
- name: Send discord notification (production server deploy failed)
if: failure() && steps.deploy.outcome == 'failure'
uses: appleboy/discord-action@master
with:
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }}
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }}
message: |
> **🚨 Server Deployment Failed (Prod)**
>
> 🛢️ Repository : ${{ github.repository }}
> 🎋 Branch : ${{ github.ref }}
> 📐 Version : ${{ github.event.inputs.version }}
> 🔁 Run Attempt : ${{ github.run_attempt }}
> 🤗 Actor : ${{ github.triggering_actor }}
- name: Create tag
uses: actions/github-script@v6
with:
github-token: ${{ secrets.RELEASE_TOKEN }}
script: |
const version = '${{ steps.set_version.outputs.version }}';
const tag = `prod-${version}`;
try {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tag}`,
sha: context.sha
});
console.log(`Tag ${tag} created successfully.`);
} catch (error) {
if (error.status === 422) {
console.log(`Tag ${tag} already exists. Skipping tag creation.`);
} else {
throw error;
}
}
- name: Send discord notification (production server deploy complete)
uses: appleboy/discord-action@master
with:
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }}
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }}
message: |
> **🛰️ Server Deployment Complete (Prod)**
>
> 🛢️ Repository : ${{ github.repository }}
> 🎋 Branch : ${{ github.ref }}
> 📐 Version : ${{ github.event.inputs.version }}
> 🔁 Run Attempt : ${{ github.run_attempt }}
> 🤗 Actor : ${{ github.triggering_actor }}
validate_version:
runs-on: ubuntu-latest
steps:
- name: Validate version format
run: |
if [[ "${{ github.event.inputs.version }}" =~ ^(latest|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
echo "Version format is valid"
else
echo "Invalid version format. Use 'latest' or semver format (e.g., 0.2411.135)"
exit 1
fi