-
Notifications
You must be signed in to change notification settings - Fork 36
210 lines (183 loc) · 9.06 KB
/
deployment.workflow.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
name: "Code Deployment (Acquia)"
on:
push:
branches: [ develop, main, ACMS-*, feature/* ]
workflow_dispatch:
inputs:
environment:
description: 'Choose environment'
type: choice
options:
- dev
- stage
- prod
- next
- ode1
- ode2
- ode3
- ode4
- ode5
- ode6
- ode7
- ode8
required: true
default: 'dev'
php-version:
description: 'Choose PHP Version'
type: choice
options:
- "8.1"
- "8.2"
- "8.3"
- "8.4"
required: true
default: '8.3'
drupal-core:
description: 'Choose Drupal Core Version.'
required: false
default: '^11'
download-latest-dependencies:
description: 'Download latest dependencies'
type: boolean
default: false
concurrency:
group: "ci-${{ github.ref }}"
cancel-in-progress: true
env:
GIT_AUTHOR_NAME: "CD"
GIT_COMMITTER_NAME: "CD"
GIT_COMMITTER_EMAIL: "[email protected]"
GIT_AUTHOR_EMAIL: "[email protected]"
PHP_VERSION: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.php-version || '8.3' }}
CORE_VERSION: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.drupal-core || '' }}
ACQUIA_CLOUD_ENVIRONMENT: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || 'dev' }}
DOWNLOAD_LATEST_DEPENDENCIES: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.download-latest-dependencies || 'false' }}
jobs:
acquia_code_deployment:
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
name: "Code Deployment"
runs-on: ubuntu-latest
environment: acquia_code_deployment
env:
ACQUIA_CLOUD_API_KEY: ${{ secrets.ACQUIA_CLOUD_API_KEY }}
ACQUIA_CLOUD_API_SECRET: ${{ secrets.ACQUIA_CLOUD_API_SECRET }}
ACQUIA_CLOUD_APPLICATION: ${{ secrets.ACQUIA_CLOUD_APPLICATION }}
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
- name: Setup Drupal Project
run: |
# Download specific version of Drupal Core, if requested.
[ -n "${CORE_VERSION}" ] && echo composer require drupal/core:${CORE_VERSION} drupal/core-composer-scaffold:${CORE_VERSION} drupal/core-recommended:${CORE_VERSION} --no-install --no-update -n || true
# Move some of development dependencies to production dependencies or else acli push:artifact will fail.
composer require oomphinc/composer-installers-extender --no-install --no-update -n
if [ "${DOWNLOAD_LATEST_DEPENDENCIES}" == "true" ]; then
rm composer.lock
composer install
else
# Update specific version of Drupal Core, if requested or else simply install all dependencies.
if [ -n "${CORE_VERSION}" ]; then
composer update "drupal/core-*" drush/drush "drupal/*" -W --with=drupal/core:${CORE_VERSION} --minimal-changes
else
# In CI, we don't need to run composer install again, as it's already will be done by below command.
composer update oomphinc/composer-installers-extender
fi
fi
# Include MySQL 5.7 connection settings, before DRS require line is added.
echo "require DRUPAL_ROOT . '/modules/contrib/mysql57/settings.inc';" >> docroot/sites/default/settings.php
# Download acquia/drupal-recommended-settings plugin & drupal/mysql57 library.
composer require acquia/drupal-recommended-settings:^1.1 drupal/mysql57
# Update .gitignore file, otherwise acli won't push any settings file.
sed -i 's/docroot\//docroot\/core/' .gitignore
cat .github/cd.gitignore >> .gitignore 2>/dev/null
# Create symlink directory for multisite headless and community.
cd docroot/sites
ln -s default headless
ln -s default community
cd -
git add . && git commit -m "Update drupal core and it's dependencies." 2>/dev/null
- name: Setup Acquia CLI
run: |
curl -OL https://github.com/acquia/cli/releases/latest/download/acli.phar
chmod +x acli.phar
mv acli.phar /usr/local/bin/acli
acli --version
acli auth:login --key=${ACQUIA_CLOUD_API_KEY} --secret=${ACQUIA_CLOUD_API_SECRET} -n
ACQUIA_APPLICATION_UUID=$(acli api:applications:find ${ACQUIA_CLOUD_APPLICATION} -n | jq -r '.uuid')
acli link ${ACQUIA_APPLICATION_UUID} -n
acli remote:aliases:download -n
git add . && git commit -m "Added acli & drush alias configurations." 2>/dev/null
- name: Find & Switch PHP Version
run: |
# Find the PHP version.
ENV_PHP_VERSION=$(acli api:environments:find ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} -n | jq -r '.configuration.php.version')
echo "The CI php-version: ${PHP_VERSION}"
echo "The Acquia Cloud environment php-version: $ENV_PHP_VERSION"
# If PHP_VERSION is NOT the same as the one we want to switch to. Then switch PHP version.
if [ "${ENV_PHP_VERSION}" != "${PHP_VERSION}" ]; then
echo "Switching PHP version to ${PHP_VERSION}"
acli api:environments:update ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} --lang_version=${PHP_VERSION} -n
sleep 10
fi
- name: Configure SSH keys
run: |
mkdir -p ~/.ssh
echo "${ACQUIA_CLOUD_SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
SSH_URL=$(acli api:environments:find ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} -n | jq -r '.ssh_url' | awk -F'[@:]' '{print $2}')
GIT_URL=$(acli api:environments:find ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} -n | jq -r '.vcs.url' | awk -F'[@:]' '{print $2}')
ssh-keyscan "${SSH_URL}" >> ~/.ssh/known_hosts && ssh-keyscan "${GIT_URL}" >> ~/.ssh/known_hosts
shell: bash
env:
ACQUIA_CLOUD_SSH_PRIVATE_KEY: ${{ secrets.ACQUIA_CLOUD_SSH_PRIVATE_KEY }}
- name: Deploy & Switch Code
shell: bash
run: |
BRANCH_TO_DEPLOY=acli-${GITHUB_REF_NAME}
# Push changes on Acuia Cloud.
acli push:artifact ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} --destination-git-branch=${BRANCH_TO_DEPLOY} -n
# If the event is push and branch is develop or if CI is triggered manually, then only switch code.
if [[ "${GITHUB_EVENT_NAME}" == "push" && "${GITHUB_REF_NAME}" == "develop" ]] || [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
DEPLOYED_BRANCH=$(acli api:environments:find ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} -n | jq -r '.vcs.path')
# If BRANCH_TO_DEPLOY is NOT the same as DEPLOYED_BRANCH. Then switch code.
if [ "${BRANCH_TO_DEPLOY}" != "${DEPLOYED_BRANCH}" ]; then
# Step 1: Run the code:switch command and capture its output.
response=$(acli api:environments:code-switch ${ACQUIA_CLOUD_APPLICATION}.${ACQUIA_CLOUD_ENVIRONMENT} ${BRANCH_TO_DEPLOY} -n)
# Step 2: Extract the notification ID from the JSON response
notification_id=$(echo "$response" | jq -r '.notification')
# Step 3: Check if the notification ID was successfully extracted.
if [[ -z "$notification_id" ]]; then
echo "Error: Notification ID not found in the response."
exit 1
fi
progress=0
timeout=1800 # Timeout in seconds (30 minutes).
start_time=$(date +%s) # Get the current time in seconds
echo "Please wait while code is being switched..."
# Step 4: Poll the notification status until progress reaches 100.
while [[ "$progress" -lt 100 ]]; do
# Run the notification find command.
notification_response=$(acli api:notifications:find "$notification_id" -n)
# Extract the progress value.
progress=$(echo "$notification_response" | jq -r '.progress')
# Display current progress.
#echo "Current progress: $progress%"
# Check if we've exceeded the timeout (5 minutes).
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [[ "$elapsed_time" -ge "$timeout" ]]; then
# Calculate minutes and seconds.
minutes=$((elapsed_time / 60))
seconds=$((elapsed_time % 60))
echo "Error: Timeout reached after $minutes minute(s) and $seconds second(s). Progress did not reach 100%."
exit 1
fi
# Wait for 5 seconds before polling again.
sleep 5
done
fi
fi