-
Notifications
You must be signed in to change notification settings - Fork 0
151 lines (131 loc) · 4.93 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
name: Deploy to Pantheon
on:
push:
branches:
- master
pull_request:
branches:
- master
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 }}
# - name: Find canary sites
# id: findSites
# run: |
# # Get list of sites with the canary tag using the upstream in our organization
# SITES=$(terminus org:site:list --upstream="${{ vars.UPSTREAM_GUID }}" --tag="${{ vars.CANARY_TAG_NAME }}" --field="name" ${{ vars.ORG_GUID }})
# SITE_JSON=$(echo $SITES | jq -R -s -c 'split("\n") | map(select(length > 0))')
# # Export the list of sites for the matrix
# echo "sites=$SITE_JSON" >> $GITHUB_OUTPUT
deploy_to_pantheon:
runs-on: ubuntu-latest
needs: [ identify_sites, configure_env ]
# strategy:
# matrix:
# site: ${{ fromJSON(needs.identify_sites.outputs.sites) }}
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
env=${{ github.ref_name }}
if [ "$env" == "master" ]; 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 }}.dev $env
fi
# Ensure environment is in git mode
terminus connection:set ${{ vars.SITE_NAME }}.$env git
- name: Push branch to Pantheon
run: |
# The dev environment is always based on the master branch
branch=$( [ "$env" == "dev" ] && echo "master" || echo "$env" )
# 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 remote add pantheon $(terminus connection:info ${{ vars.SITE_NAME }}.$env --field=git_url)
git fetch --all
git push pantheon ${{ github.ref_name }}:$branch --force
spin_down:
name: Spin down environment
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 }}" )"