-
Notifications
You must be signed in to change notification settings - Fork 61
130 lines (117 loc) · 4.87 KB
/
pullrequest-close.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
name: PR close
on:
pull_request:
types:
- closed
defaults:
run:
shell: bash
jobs:
check-unicorn:
name: Is this a unicorn PR
if: github.event.pull_request.merged == true
runs-on: arc-runners
timeout-minutes: 10
outputs:
IS_UNICORN: ${{ steps.unicorn_affected.outputs.IS_UNICORN }}
LATEST_RELEASE: ${{ steps.get_latest_release.outputs.LATEST_RELEASE }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version-file: 'package.json'
- name: Setup yarn
run: corepack enable
- name: Get cache
id: get-cache
uses: ./.github/actions/get-cache
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
enable-cache: 'node_modules'
- name: Derive appropriate SHAs
uses: nrwl/nx-set-shas@v4
- run: |
echo "BASE: ${{ env.NX_BASE }}"
echo "HEAD: ${{ env.NX_HEAD }}"
- name: Check unicorn affected
id: unicorn_affected
run: |
echo "Comparing nx affected for ${{ env.NX_HEAD }} using ${{ env.NX_BASE }} as base branch"
echo IS_UNICORN=$(node scripts/ci/unicorn-utils.mjs "{\"head\": \"${{ env.NX_HEAD }}\", \"base\": \"${{ env.NX_BASE }}\" }") >> "$GITHUB_OUTPUT"
- name: Results
run: |
echo "Unicorn = ${{ steps.unicorn_affected.outputs.IS_UNICORN }}"
- name: Find Latest Release Branch
if: ${{ steps.unicorn_affected.outputs.IS_UNICORN == 'true' }}
id: get_latest_release
run: |
LATEST_RELEASE="$(node scripts/ci/get-last-release.mjs $(git branch -r))"
echo LATEST_RELEASE=$LATEST_RELEASE >> "$GITHUB_OUTPUT"
- run: "echo 'latest release: ${{ steps.get_latest_release.outputs.LATEST_RELEASE }}'"
- name: Create a PR
if: ${{ steps.unicorn_affected.outputs.IS_UNICORN == 'true' }}
id: create_pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_BRANCH: ${{ github.head_ref}}/cherry-pick
TARGET_BRANCH: release/${{ steps.get_latest_release.outputs.LATEST_RELEASE }}
run: |
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global user.name "${{ github.actor }} (automated)"
echo ${{ github.event.pull_request.merge_commit_sha }}
echo ${{ github.head_ref}}
git checkout $TARGET_BRANCH
git checkout -b $PR_BRANCH
git cherry-pick -x ${{ github.event.pull_request.merge_commit_sha }}
git commit --allow-empty -am "Automated cherry-pick of ${{ github.event.pull_request.merge_commit_sha }}"
git push --set-upstream origin $PR_BRANCH
echo "new branch created"
PR=$(gh pr create -B $TARGET_BRANCH -H $PR_BRANCH --title "Automated cherry-pick of ${{ github.event.pull_request.merge_commit_sha }}" --body "Automated cherry-pick of ${{ github.event.pull_request.merge_commit_sha }}")
echo "$PR created"
echo PR=$PR >> "$GITHUB_OUTPUT"
- name: Send slack message
if: ${{ steps.unicorn_affected.outputs.IS_UNICORN == 'true' }}
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "A new PR has been created: ${{ steps.create_pr.outputs.PR }}"
cleanup:
name: Clean up feature deployment
runs-on: arc-runners
steps:
- name: Get git branch
run: |
set -euo pipefail
GIT_BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF/refs\/heads\//}}"
echo "GIT_BRANCH=$GIT_BRANCH" >> "$GITHUB_ENV"
- name: Generate deployment branch name
id: git-branch-deploy
run: |
set -euo pipefail
GIT_BRANCH_DEPLOY="$GIT_BRANCH"
if [[ ! ("$GIT_BRANCH_DEPLOY" =~ "feature/") ]]; then
# If event is pull request but branch is not prefixed with feature/
GIT_BRANCH_DEPLOY=feature/$GIT_BRANCH_DEPLOY
fi
# Avoid too long resource names
GIT_BRANCH_DEPLOY="${GIT_BRANCH_DEPLOY:0:50}"
echo "GIT_BRANCH_DEPLOY=$GIT_BRANCH_DEPLOY" >> "$GITHUB_ENV"
- name: Clean up feature
env:
SPINNAKER_WEBHOOK_TOKEN: ${{ secrets.SPINNAKER_WEBHOOK_TOKEN }}
SPINNAKER_URL: https://spinnaker-gate.shared.devland.is
run: |
set -euo pipefail
curl "$SPINNAKER_URL/webhooks/webhook/feature-cleanup" -H "content-type: application/json" --data-binary @- <<BODY
{
"token": "$SPINNAKER_WEBHOOK_TOKEN",
"parameters": {
"feature_name": "$(echo "$GIT_BRANCH_DEPLOY" | cut -d"/" -f2- | tr -cd '[:alnum:]-' | tr '[:upper:]' '[:lower:]' | cut -c1-50)"
}
}
BODY