-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (135 loc) · 4.99 KB
/
standard-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
name: Deploy to Pantheon
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
configure_env:
name: Configure environment and Terminus
runs-on: ubuntu-latest
steps:
- name: Restore ssh config cache
id: restore-ssh-config
uses: actions/cache/restore@v4
with:
path: ~/.ssh
key: ${{ runner.os }}-config-${{ github.run_id }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
- name: Install Terminus
uses: pantheon-systems/terminus-github-actions@main
with:
pantheon-machine-token: ${{ secrets.TERMINUS_TOKEN }}
- name: Create SSH key & add to Pantheon
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -m PEM -t rsa -b 4096 -N '' -C 'CI+deployment+${{ github.run_id }}' -f ~/.ssh/pantheon
terminus ssh-key:add ~/.ssh/pantheon.pub
- name: Save ssh config to cache
id: save-ssh-config
uses: actions/cache/save@v4
with:
path: ~/.ssh
key: ${{ runner.os }}-config-${{ github.run_id }}
identify_sites:
runs-on: ubuntu-latest
needs: configure_env
outputs:
sites: ${{ steps.findSites.outputs.sites }}
steps:
- name: Restore ssh config cache
id: restore-ssh-config
uses: actions/cache/restore@v4
with:
path: ~/.ssh
key: ${{ runner.os }}-config-${{ github.run_id }}
- name: Install Terminus
uses: pantheon-systems/terminus-github-actions@main
with:
pantheon-machine-token: ${{ secrets.TERMINUS_TOKEN }}
deploy_to_pantheon:
runs-on: ubuntu-latest
needs: [ identify_sites, configure_env ]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Restore ssh config cache
id: restore-ssh-config
uses: actions/cache/restore@v4
with:
path: ~/.ssh
key: ${{ runner.os }}-config-${{ github.run_id }}
- name: Install Terminus
uses: pantheon-systems/terminus-github-actions@v1
with:
pantheon-machine-token: ${{ secrets.TERMINUS_TOKEN }}
- name: Determine target environment for deploy
run: |
# Identify environment based on branch name
if [ "${{ github.event_name }}" = "pull_request" ]; then
# Use the PR number as the environment name
env=$(echo ${{ github.ref_name }} | sed 's|^\([0-9]*\)/.*|pr-\1|')
else
# Use the branch name as the environment name
env=${{ github.ref_name }}
fi
if [ "$env" == "main" ]; then
env="dev"
fi
# Ensure environment name is 11 characters or less and has no special characters
env="${env:0:11}"
env=$(echo "$env" | sed 's/[^a-zA-Z0-9]/-/g')
# Export environment name
echo "env=$env" >> $GITHUB_ENV
- name: Ensure environment exists and is in git mode
run: |
# Create multidev environment if it doesn't exist
if ! terminus env:list ${{ vars.SITE_NAME }} --field=ID | grep $env; then
terminus env:create ${{ vars.SITE_NAME }}.live $env
fi
# Ensure environment is in git mode
terminus connection:set ${{ vars.SITE_NAME }}.$env git
- name: Push branch to Pantheon
run: |
curr_branch=$( git branch --show-current )
# The dev environment is always based on the master branch
dest_branch=$( [ "$env" == "dev" ] && echo "master" || echo "$env" )
if [[ ${{ github.event_name }} == 'pull_request' ]]; then
git checkout ${{ github.head_ref }}
fi
# Configure git to use the SSH key and avoid host key checking
git config --local core.sshCommand 'ssh -i ~/.ssh/pantheon -o StrictHostKeyChecking=no'
git config --local user.email "[email protected]"
git config --local user.name "GitHub Actions"
git remote add pantheon $(terminus connection:info ${{ vars.SITE_NAME }}.$env --field=git_url)
git fetch pantheon
git checkout -b $dest_branch
git pull pantheon $dest_branch --rebase
git status
git push pantheon $dest_branch
spin_down:
name: Spin down
needs: deploy_to_pantheon
if: always()
runs-on: ubuntu-latest
steps:
- name: Restore ssh config cache
id: restore-ssh-config
uses: actions/cache/restore@v4
with:
path: ~/.ssh
key: ${{ runner.os }}-config-${{ github.run_id }}
- name: Install Terminus
uses: pantheon-systems/terminus-github-actions@main
with:
pantheon-machine-token: ${{ secrets.TERMINUS_TOKEN }}
- name: Remove SSH key from Pantheon
run: terminus ssh-key:remove "$( terminus ssh-key:list --field=id --filter="comment=CI+deployment+${{ github.run_id }}" )"