-
Notifications
You must be signed in to change notification settings - Fork 1
114 lines (101 loc) · 4.59 KB
/
redeploy.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
name: Redeploy to Oracle on PR merge to main
# On PR merge to main branch of repository...
on:
pull_request:
branches:
- main
types:
[closed]
# Only run workflow if changes are detected in specified paths / files
paths:
- src/**
- package.json
- config.ts
- process.yml
- Dockerfile
- .dockerignore
- .github/workflows/**
- tsconfig.json
jobs:
redeploy:
# Run on Ubuntu (Oracle server is Ubuntu / Linux)
runs-on: ubuntu-latest
# Accommodate the retrieval of environmental secrets that are only available in prod environment
environment: prod
# Pull changes from GitHub repo to Oracle server, and set variable to "true" if changes are detected
steps:
# Checkout code from GitHub repo
- name: Checkout code
id: checkout
uses: actions/checkout@v2
# Connect to Oracle server via SSH and execute deploy script there
- name: Connect to Oracle server via SSH and execute commands
uses: appleboy/ssh-action@v0.1.9
with:
host: ${{ secrets.ORACLE_SERVER_HOST }}
username: ${{ secrets.ORACLE_SERVER_USERNAME }}
key: ${{ secrets.ORACLE_SERVER_PRIVATE_KEY }}
port: ${{ secrets.ORACLE_SERVER_PORT }}
command_timeout: 20m
script: |
echo "** Connected to Oracle server via SSH. **"
echo "Running deploy script..."
echo "Pulling changes from GitHub repo..."
cd /home/opc
if [ -d "cycle-mania" ]; then
cd cycle-mania
git pull
else
git clone https://github.com/${{github.repository}}
cd cycle-mania
fi
echo "Changes pulled from GitHub repo."
echo "Copying environmental secrets from GitHub repo to Oracle server..."
echo "" > .env
echo "DISCORD_BOT_ID=${{ secrets.DISCORD_BOT_ID }}" >> .env
echo "DISCORD_BOT_OWNER_ID=${{ secrets.DISCORD_BOT_OWNER_ID }}" >> .env
echo "DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }}" >> .env
echo "DISCORD_CHANNEL_ACTIVITIES_ID=${{ secrets.DISCORD_CHANNEL_ACTIVITIES_ID }}" >> .env
echo "DISCORD_CHANNEL_LEADERBOARD_ID=${{ secrets.DISCORD_CHANNEL_LEADERBOARD_ID }}" >> .env
echo "DISCORD_WEBHOOK_LOG"=${{ secrets.DISCORD_WEBHOOK_LOG }} >> .env
echo "MONGO_DB_NAME=${{ secrets.MONGO_DB_NAME }}" >> .env
echo "MONGO_PASSWORD=${{ secrets.MONGO_PASSWORD }}" >> .env
echo "MONGO_USERNAME=${{ secrets.MONGO_USERNAME }}" >> .env
echo "PROD=${{ secrets.PROD }}" >> .env
echo "STRAVA_ACCESS_TOKEN=${{ secrets.STRAVA_ACCESS_TOKEN }}" >> .env
echo "STRAVA_CLIENT_ID=${{ secrets.STRAVA_CLIENT_ID }}" >> .env
echo "STRAVA_CLIENT_SECRET=${{ secrets.STRAVA_CLIENT_SECRET }}" >> .env
echo "STRAVA_REFRESH_TOKEN=${{ secrets.STRAVA_REFRESH_TOKEN }}" >> .env
echo "STRAVA_USER_COOKIE=${{ secrets.STRAVA_USER_COOKIE }}" >> .env
echo "Environmental secrets copied from GitHub repo to Oracle server"
echo "Building new Docker image..."
sudo docker build --tag cycle-mania:${{ github.sha }} .
echo "New Docker image built."
echo "Stopping and deleting existing Docker container..."
sudo docker stop cycle-mania || true
sudo docker rm cycle-mania || true
echo "Existing Docker container stopped and deleted."
echo "Running new Docker container..."
sudo docker run -d --name cycle-mania --env-file .env cycle-mania:${{ github.sha }}
echo "New Docker container running."
echo "Waiting for new Docker container to start..."
sleep 10
echo "New Docker container started."
echo "Checking if new Docker container is running..."
sudo docker ps | grep cycle-mania
echo "New Docker container is running."
echo "Deployment successful."
exit 0
# Send message to Discord channel to confirm successful deployment (or failure)
- uses: actions/setup-ruby@v1
- name: Send Webhook Notification
if: always()
env:
JOB_STATUS: ${{ job.status }}
WEBHOOK_URL: ${{ vars.DOCKER_WEBHOOK_TOKEN }}
HOOK_OS_NAME: ${{ runner.os }}
WORKFLOW_NAME: ${{ github.workflow }}
run: |
git clone https://github.com/elendil7/github-actions-discord-webhook webhook
bash webhook/send.sh $JOB_STATUS $WEBHOOK_URL
shell: bash