-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (122 loc) · 5.23 KB
/
build-and-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
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
# ./.github/workflows/build-and-deploy.yml
# FORGE_PROJECT_ROOT — a path to the root directory of the project on the server (/home/forge/example.com)
# FORGE_SSH_HOST url or IP address
# FORGE_SSH_KEY
# FORGE_SSH_USER forge
name: CraftCMS Build & Atomic Deployment
on:
# push:
# branches: [main]
workflow_dispatch:
env:
SHA: ${{ github.sha }}
PROJECT_ROOT: ${{ secrets.FORGE_STAGING_ROOT }}
jobs:
build-and-deploy:
# https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idruns-on
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idsteps
steps:
# https://github.com/actions/checkout
- name: Checkout
# 2.4.0 was the latest at the time of writing
uses: actions/[email protected]
# a standard step for GitHub actions on PHP
# https://github.com/shivammathur/setup-php
- name: Setup PHP with composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none
tools: composer:v2
- name: Get composer cache directory
id: composer-cache
working-directory: ./cms
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Composer dependencies
working-directory: ./cms
run: composer install --no-dev --no-progress --no-scripts --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs
# a standard step for GitHub actions on Node
# https://github.com/actions/setup-node
- name: Set up node env
# 2.5.1 was the latest at the time of writing
uses: actions/setup-node@v3
with:
# specify the version appropriate to your project
# setup-node can also read the version from a Node version file. see the setup-node docs for details
node-version-file: '.nvmrc'
# cache installed dependencies for best performance. yarn and pnpm are also supported
cache: yarn
- name: Install dependencies
# For Node 16: https://docs.npmjs.com/cli/v8/commands/npm-ci
# for other Node versions, look up the npm version at https://nodejs.org/en/download/releases/
run: yarn install --immutable
# Build frontend. For critical, use `yarn build:critical`
- name: Build frontend
run: yarn build
# Setup container with private SSH Key (used by rsync)
- name: Load private SSH key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.FORGE_SSH_KEY }}
- name: Deploy with rsync
run: |
rsync -azh --delete-after -e "ssh -o StrictHostKeyChecking=no" ./cms/ ${{ secrets.FORGE_SSH_USER }}@${{ secrets.FORGE_SSH_HOST }}:${{ secrets.FORGE_PROJECT_ROOT }}/deploy-cache/
# Execute remote commands via SSH
- name: Atomic Deploy
run: |
ssh ${{ secrets.FORGE_SSH_USER }}@${{ secrets.FORGE_SSH_HOST }} << EOF
echo "PROJECT_ROOT: $PROJECT_ROOT"
echo "SHA: $SHA"
if [ ! -d "$PROJECT_ROOT/releases" ];
then
mkdir $PROJECT_ROOT/releases
fi
if [ -d "$PROJECT_ROOT/releases/$SHA" ];
then
echo "Removing: $PROJECT_ROOT/releases/$SHA"
rm -rf $PROJECT_ROOT/releases/$SHA;
fi
if [ ! -d "$PROJECT_ROOT/releases/$SHA" ];
then
echo "Creating: $PROJECT_ROOT/releases/$SHA"
cp -dR $PROJECT_ROOT/deploy-cache $PROJECT_ROOT/releases/$SHA;
fi
echo "Creating: persistent directories"
mkdir -p $PROJECT_ROOT/storage
mkdir -p $PROJECT_ROOT/uploaded
echo "Symlinking: persistent files & directories"
ln -nfs $PROJECT_ROOT/.env $PROJECT_ROOT/releases/$SHA
ln -nfs $PROJECT_ROOT/storage $PROJECT_ROOT/releases/$SHA
ln -nfs $PROJECT_ROOT/uploaded $PROJECT_ROOT/releases/$SHA/web
echo "Linking current to revision: $SHA"
rm -f $PROJECT_ROOT/current
ln -s $PROJECT_ROOT/releases/$SHA $PROJECT_ROOT/current
echo "Removing old releases"
cd $PROJECT_ROOT/releases && ls -t | tail -n +6 | xargs rm -rf
EOF
# Execute Craft commands on remote server via ssh
- name: Prep Craft CMS
run: |
ssh ${{ secrets.FORGE_SSH_USER }}@${{ secrets.FORGE_SSH_HOST }} << EOF
# Move into current
cd $PROJECT_ROOT/current
# Ensure the craft script is executable
chmod a+x craft
# Turn Craft off
php craft off --retry=60
# Perform update
php craft db/backup
php craft clear-caches/all
php craft migrate/all
php craft project-config/apply
# Turn Craft on
php craft on
echo "" | sudo -S service php8.1-fpm reload
EOF