-
Notifications
You must be signed in to change notification settings - Fork 340
124 lines (109 loc) · 4.9 KB
/
manage-environment.yaml
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
name: Manage environment
on:
pull_request_target:
branches: [main]
types:
- opened
- reopened
- closed
env:
PLATFORMSH_CLI_NO_INTERACTION: 1
PLATFORM_PROJECT: ${{ vars.PROJECT_ID }}
PLATFORMSH_CLI_DEFAULT_TIMEOUT: 60 # Increase timeout for CLI commands
PLATFORMSH_CLI_TOKEN: ${{ secrets.PLATFORMSH_CLI_TOKEN }}
BRANCH_TITLE: ${{ vars.BFF_PREFIX }}-${{ github.event.number }}
jobs:
activate_environment:
runs-on: ubuntu-latest
# Activate when a PR is opened, but not for forks (handled through `build-from-fork` workflow)
if: >-
(github.event.action == 'opened' ||
github.event.action== 'reopened') &&
github.event.pull_request.head.repo.id == 21975579
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Set up workflow environment to use the Platform.sh CLI
- name: Set up Platform.sh CLI
uses: ./.github/actions/set-up-cli
- name: Set environment title
env:
# Github does not escape PR titles before swapping the token with the value. This means that in a shell context
# if a PR title includes a single or double quote, it will break the shell command. If you try to enclose
# the value with quotes before assigning it to a variable, it will also break. The only way around it is to
# assign it as an environmental variable for the step which will force GitHub to escape it properly for the
# shell, and *then* you can use it
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
# @todo - this is the same as the last step in build-from-fork:jobs:build. See if we can combine the two
newTitle="(pr-${{ github.event.number }}) ${PR_TITLE}"
currentTitle=$(platform environment:info -e ${{ github.event.pull_request.head.ref }} title)
if [[ "${currentTitle}" != "${newTitle}" ]]; then
echo "::notice::Setting environment's title"
platform environment:info -e ${{ github.event.pull_request.head.ref }} title "${newTitle}"
fi
- name: Activate environment
run: platform environment:activate --no-wait ${{ github.event.pull_request.head.ref }}
deactivate_environment:
runs-on: ubuntu-latest
# For non-forked PRs
# Deactivate when a PR is closed
# Don't need to run on merges since the integration takes care of it
if: >-
github.event.action == 'closed' &&
github.event.pull_request.merged == false &&
github.event.pull_request.head.repo.id == 21975579
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Set up workflow environment to use the Platform.sh CLI
- name: Set up Platform.sh CLI
uses: ./.github/actions/set-up-cli
- name: Close environment
run: platform environment:deactivate --no-delete-branch ${{ github.event.pull_request.head.ref }}
deactivate_forked_environment:
runs-on: ubuntu-latest
# For forked PRs labelled as build-fork
# Delete branch when a PR is closed
if: >-
github.event.action == 'closed' &&
github.event.pull_request.head.repo.id != 21975579 &&
contains(github.event.pull_request.labels.*.name, 'build-fork')
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Check if Git branch exists
id: branch_exists
# Check if the Git branch exists
run: |
BRANCH=$(git ls-remote --heads origin ${{ env.BRANCH_TITLE }})
echo $BRANCH
if [[ -n $BRANCH ]]; then
echo "branch_exists=true" >> $GITHUB_OUTPUT
else
echo "branch_exists=false" >> $GITHUB_OUTPUT
fi
# If the branch or environment exists, delete it from Platform.sh
- name: Delete branch
if: steps.branch_exists.outputs.branch_exists == 'true'
run: |
git push origin -d ${{ env.BRANCH_TITLE }}
# Set up workflow environment to use the Platform.sh CLI
- name: Set up Platform.sh CLI
# If the Git branch was deleted, skip checks for the environment
if: steps.branch_exists.outputs.branch_exists != 'true'
uses: ./.github/actions/set-up-cli
- name: Check if environment exists
id: environment_exists
if: steps.branch_exists.outputs.branch_exists != 'true'
# Check if the built environment exists
run: |
BRANCH=$(platform environments --pipe --columns title | grep ${{ env.BRANCH_TITLE }})
if [[ -n $BRANCH ]]; then
echo "environment_exists=true" >> $GITHUB_OUTPUT
fi
# If the branch or environment exists, delete it from Platform.sh
- name: Delete environment
if: steps.branch_exists.outputs.branch_exists != 'true' && steps.environment_exists.outputs.environment_exists == 'true'
run: |
platform environment:delete --delete-branch --no-wait ${{ env.BRANCH_TITLE }}