-
Notifications
You must be signed in to change notification settings - Fork 1.5k
150 lines (132 loc) · 5.85 KB
/
Copy pathbackport-pr.yml
File metadata and controls
150 lines (132 loc) · 5.85 KB
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
name: Backport PR
on:
pull_request:
types:
- closed
- labeled
jobs:
get-backport-targets:
name: Get backport targets
runs-on: ubuntu-latest
if: github.event.pull_request.merged
outputs:
targets: ${{ steps.get-targets.outputs.targets }}
steps:
- name: Get backport targets
id: get-targets
env:
EVENT_ACTION: ${{ github.event.action }}
EVENT_LABEL_NAME: ${{ github.event.label.name }}
PR_LABEL_NAMES_JSON: ${{ toJson(github.event.pull_request.labels.*.name) }}
run: |
# For 'labeled' event, use just the added label
# For 'closed' event, use all backport labels
if [[ "$EVENT_ACTION" == "labeled" ]]; then
if [[ "$EVENT_LABEL_NAME" =~ ^backport/([0-9]+\.[0-9]+\.x|main)$ ]]; then
targets=$(jq -cn --arg label "$EVENT_LABEL_NAME" '[$label]')
echo "targets=$targets" >> "$GITHUB_OUTPUT"
else
echo "targets=[]" >> "$GITHUB_OUTPUT"
fi
else
# Extract all well-formed backport/* labels as JSON array
targets=$(jq -c '[.[] | select(test("^backport/([0-9]+\\.[0-9]+\\.x|main)$"))]' <<< "$PR_LABEL_NAMES_JSON")
echo "targets=$targets" >> "$GITHUB_OUTPUT"
fi
backport:
name: Backport PR to ${{ matrix.target }}
needs: get-backport-targets
if: needs.get-backport-targets.outputs.targets != '[]' && needs.get-backport-targets.outputs.targets != ''
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target: ${{ fromJson(needs.get-backport-targets.outputs.targets) }}
permissions:
id-token: write # This is required for getting the required OIDC token from GitHub
contents: write # This is required for pushing the backport branch
pull-requests: write # This is required for creating the backport PR
steps:
- name: Compute target branch
id: target
env:
MATRIX_TARGET: ${{ matrix.target }}
run: |
target="$MATRIX_TARGET"
target_branch="${target/backport\//}"
echo "target_branch=$target_branch" >> "$GITHUB_OUTPUT"
- uses: DataDog/dd-octo-sts-action@96a25462dbcb10ebf0bfd6e2ccc917d2ab235b9a # v1.0.4
id: octo-sts
with:
scope: DataDog/datadog-agent
policy: self.backport-pr.create-pr
- name: Checkout
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
fetch-depth: 0 # needed to get the full history of the PR
- name: Install dda
uses: ./.github/actions/install-dda
with:
features: github
- name: Run cherry-pick script
id: cherry-pick
continue-on-error: true
env:
GITHUB_TOKEN: ${{ steps.octo-sts.outputs.token }}
GITHUB_EVENT_PATH: ${{ github.event_path }}
run: dda gh cherry-pick-pr --target-branch "${{ steps.target.outputs.target_branch }}"
- name: Comment on backport failure
if: steps.cherry-pick.outcome == 'failure'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
TARGET_BRANCH: ${{ steps.target.outputs.target_branch }}
MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
github-token: ${{ steps.octo-sts.outputs.token }}
script: |
const targetBranch = process.env.TARGET_BRANCH;
const mergeCommitSHA = process.env.MERGE_COMMIT_SHA;
const runUrl = process.env.RUN_URL;
const commentBody = `⚠️ Automatic backport to \`${targetBranch}\` failed.
This usually happens when the cherry-pick has merge conflicts and needs manual resolution.
To backport manually, run:
\`\`\`bash
git fetch
git worktree add .worktrees/backport-${targetBranch} ${targetBranch}
cd .worktrees/backport-${targetBranch}
git switch --create backport-${context.payload.pull_request.number}-to-${targetBranch}
git cherry-pick -x --mainline 1 ${mergeCommitSHA}
git push --set-upstream origin backport-${context.payload.pull_request.number}-to-${targetBranch}
\`\`\`
Workflow logs: ${runUrl}`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody,
});
- name: Fail workflow when cherry-pick failed
if: steps.cherry-pick.outcome == 'failure'
run: exit 1
- name: Reset cherry-pick
if: steps.cherry-pick.outputs.base != ''
run: git reset HEAD~1
- name: Create pull request
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
if: steps.cherry-pick.outputs.base != ''
with:
token: ${{ steps.octo-sts.outputs.token }}
base: ${{ steps.cherry-pick.outputs.base }}
branch: backport-${{steps.cherry-pick.outputs.original_pr_number}}-to-${{steps.cherry-pick.outputs.base}}
sign-commits: true
title: "[Backport ${{steps.cherry-pick.outputs.base}}] ${{steps.cherry-pick.outputs.original_title}}"
body: |
Backport ${{steps.cherry-pick.outputs.merge_commit_sha}} from #${{steps.cherry-pick.outputs.original_pr_number}}.
___
${{ steps.cherry-pick.outputs.original_body }}
labels: ${{ steps.cherry-pick.outputs.original_labels }},backport,bot
commit-message: |
${{ steps.cherry-pick.outputs.message }}
___
Co-authored-by: ${{ steps.cherry-pick.outputs.author }}