-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (98 loc) · 3.68 KB
/
deploy.yml
File metadata and controls
111 lines (98 loc) · 3.68 KB
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
name: Docker Deployment
on:
workflow_dispatch:
inputs:
environment:
type: environment
required: true
env:
DEPLOYMENT_NAME: nwdl
DEPLOY_PATH: /var/www/nwdl
# gotchas:
# ssh host should have custom user just for this purpose, which needs
# to have his own home directory
# sudo useradd -m user_name
# sudo passwd user_name
# sudo usermod -aG docker user_name
# sudo usermod -aG www-data user_name
# the DEPLOY_PATH needs to have specific permissions
# https://serverfault.com/questions/124800/how-to-setup-linux-permissions-for-the-www-folder
# sudo mkdir $DEPLOY_PATH
# sudo chgrp -R www-data $DEPLOY_PATH
# sudo chmod -R 2774 $DEPLOY_PATH
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker
uses: docker/setup-buildx-action@v3
- name: Build and Export
uses: docker/build-push-action@v5
with:
context: .
tags: ${{ env.DEPLOYMENT_NAME }}:prod
build-args: RAILS_MASTER_KEY=${{ secrets.RAILS_MASTER_KEY }}
target: production
outputs: type=docker,dest=/tmp/compressed.tar
- name: Save Image
uses: actions/upload-artifact@v4
with:
name: compressed_image_artifact
path: /tmp/compressed.tar
if-no-files-found: error
deploy:
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment }}
needs: build
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker
uses: docker/setup-buildx-action@v3
- name: Load Image
uses: actions/download-artifact@v4
with:
name: compressed_image_artifact
path: .
- name: List files for debugging
run: ls -la
- name: Preparing SSH & Clear Previos Deployment
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
password: ${{ secrets.DEPLOY_PASSWORD }}
script: |
docker-compose -f ${{ env.DEPLOY_PATH }}/docker-compose.yml -f ${{ env.DEPLOY_PATH }}/docker-compose.production.yml down || true
docker-compose -f ${{ env.DEPLOY_PATH }}/docker-compose.yml -f ${{ env.DEPLOY_PATH }}/docker-compose.production.yml rm || true
rm -rf ${{ env.DEPLOY_PATH }}/* || true
mkdir -p ${{ env.DEPLOY_PATH }}
- name: SCP Compressed Container & Dependencies
uses: appleboy/scp-action@master
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
password: ${{ secrets.DEPLOY_PASSWORD }}
source: "compressed.tar,docker-compose.yml,docker-compose.production.yml"
target: ${{ env.DEPLOY_PATH }}
overwrite: true
- name: Start Deployed Container
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USERNAME }}
password: ${{ secrets.DEPLOY_PASSWORD }}
script: |
cd ${{ env.DEPLOY_PATH }}
rm -f .env
echo "DOCKER_IMAGE_NAME=${{ env.DEPLOYMENT_NAME }}" > .env
echo "RAILS_MASTER_KEY=${{ secrets.RAILS_MASTER_KEY }}" >> .env
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env
echo "Generated .env file:"
cat .env
mv docker-compose.production.yml docker-compose.override.yml
docker image load -q -i compressed.tar
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d -V