Skip to content

Commit 392723e

Browse files
committed
ci: error out if someone sends a PR to the wrong branch
1 parent 81a97ce commit 392723e

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ jobs:
7272
- name: decide whether to skip this job
7373
run: src/ci/scripts/should-skip-this.sh
7474
if: success() && !env.SKIP_JOB
75+
- name: ensure the channel matches the target branch
76+
run: src/ci/scripts/verify-channel.sh
77+
if: success() && !env.SKIP_JOB
7578
- name: configure GitHub Actions to kill the build when outdated
7679
uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
7780
with:
@@ -434,6 +437,9 @@ jobs:
434437
- name: decide whether to skip this job
435438
run: src/ci/scripts/should-skip-this.sh
436439
if: success() && !env.SKIP_JOB
440+
- name: ensure the channel matches the target branch
441+
run: src/ci/scripts/verify-channel.sh
442+
if: success() && !env.SKIP_JOB
437443
- name: configure GitHub Actions to kill the build when outdated
438444
uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
439445
with:
@@ -541,6 +547,9 @@ jobs:
541547
- name: decide whether to skip this job
542548
run: src/ci/scripts/should-skip-this.sh
543549
if: success() && !env.SKIP_JOB
550+
- name: ensure the channel matches the target branch
551+
run: src/ci/scripts/verify-channel.sh
552+
if: success() && !env.SKIP_JOB
544553
- name: configure GitHub Actions to kill the build when outdated
545554
uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
546555
with:

src/ci/github-actions/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ x--expand-yaml-anchors--remove:
126126
run: src/ci/scripts/should-skip-this.sh
127127
<<: *step
128128

129+
- name: ensure the channel matches the target branch
130+
run: src/ci/scripts/verify-channel.sh
131+
<<: *step
132+
129133
- name: configure GitHub Actions to kill the build when outdated
130134
uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
131135
with:

src/ci/scripts/verify-channel.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# We want to make sure all PRs are targeting the right branch when they're
3+
# opened, otherwise we risk (for example) to land a beta-specific change to the
4+
# master branch. This script ensures the branch of the PR matches the channel.
5+
6+
set -euo pipefail
7+
IFS=$'\n\t'
8+
9+
source "$(cd "$(dirname "$0")" && pwd)/../shared.sh"
10+
11+
declare -A CHANNEL_BRANCH
12+
CHANNEL_BRANCH["nightly"]="master"
13+
CHANNEL_BRANCH["beta"]="beta"
14+
CHANNEL_BRANCH["stable"]="stable"
15+
16+
if isCiBranch auto || isCiBranch try; then
17+
echo "channel verification is only executed on PR builds"
18+
exit
19+
fi
20+
21+
channel=$(cat "$(ciCheckoutPath)/src/ci/channel")
22+
branch="$(ciBaseBranch)"
23+
if [[ "${branch}" != "${CHANNEL_BRANCH[$channel]}" ]]; then
24+
echo "error: PRs changing the \`${channel}\` channel should be sent to the \
25+
\`${CHANNEL_BRANCH[$channel]}\` branch!"
26+
27+
exit 1
28+
fi

src/ci/shared.sh

+12
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ function isCiBranch {
7373
fi
7474
}
7575

76+
function ciBaseBranch {
77+
if isAzurePipelines; then
78+
echo "unsupported on Azure Pipelines"
79+
exit 1
80+
elif isGitHubActions; then
81+
echo "${GITHUB_BASE_REF#refs/heads/}"
82+
else
83+
echo "ciBaseBranch only works inside CI!"
84+
exit 1
85+
fi
86+
}
87+
7688
function ciCommit {
7789
if isAzurePipelines; then
7890
echo "${BUILD_SOURCEVERSION}"

0 commit comments

Comments
 (0)