Skip to content

Commit 43ae784

Browse files
committed
chore: detect the channel a PR wants to merge into
1 parent ec8a8a0 commit 43ae784

File tree

3 files changed

+140
-33
lines changed

3 files changed

+140
-33
lines changed

.github/workflows/main.yml

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ env:
1616
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
1717

1818
jobs:
19+
# Determine which channel will be merged into.
20+
channel:
21+
runs-on: ubuntu-latest
22+
outputs:
23+
CHANNEL: ${{ steps.channel.outputs.CHANNEL }}
24+
env:
25+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
26+
steps:
27+
- uses: actions/checkout@v3
28+
with:
29+
fetch-depth: 0 # fetch all branches
30+
- id: channel
31+
run: ci/which-channel.sh
32+
1933
# Check Code style quickly by running `rustfmt` over all code
2034
rustfmt:
2135
runs-on: ubuntu-latest
@@ -41,8 +55,33 @@ jobs:
4155
# Only check cargo lib for now
4256
- run: cargo clippy -p cargo --lib -- -D warnings
4357

58+
# Generate strategy matrix for different platforms and channels
59+
# (see ci/matrix.json)
60+
matrix:
61+
runs-on: ubuntu-latest
62+
needs:
63+
- channel
64+
outputs:
65+
matrix: ${{ steps.matrix.outputs.matrix }}
66+
steps:
67+
- uses: actions/checkout@v3
68+
- name: Generate strategy matrix
69+
id: matrix
70+
run: |
71+
CHANNEL=${{ needs.channel.outputs.CHANNEL }}
72+
MATRIX=$(
73+
jq --arg C "$CHANNEL" 'map (. |
74+
if ($C == "beta") then select(.rust | startswith("nightly") | not)
75+
elif ($C == "stable") then select(.rust | startswith("stable"))
76+
else . end)' ci/matrix.json
77+
)
78+
echo "$MATRIX"
79+
echo "MATRIX={\"include\":$(echo $MATRIX)}" >> "$GITHUB_OUTPUT"
80+
4481
test:
4582
runs-on: ${{ matrix.os }}
83+
needs:
84+
- matrix
4685
env:
4786
CARGO_PROFILE_DEV_DEBUG: 1
4887
CARGO_PROFILE_TEST_DEBUG: 1
@@ -51,36 +90,7 @@ jobs:
5190
# Deny warnings on CI to avoid warnings getting into the codebase.
5291
RUSTFLAGS: -D warnings
5392
strategy:
54-
matrix:
55-
include:
56-
- name: Linux x86_64 stable
57-
os: ubuntu-latest
58-
rust: stable
59-
other: i686-unknown-linux-gnu
60-
- name: Linux x86_64 beta
61-
os: ubuntu-latest
62-
rust: beta
63-
other: i686-unknown-linux-gnu
64-
- name: Linux x86_64 nightly
65-
os: ubuntu-latest
66-
rust: nightly
67-
other: i686-unknown-linux-gnu
68-
- name: macOS x86_64 stable
69-
os: macos-latest
70-
rust: stable
71-
other: x86_64-apple-ios
72-
- name: macOS x86_64 nightly
73-
os: macos-latest
74-
rust: nightly
75-
other: x86_64-apple-ios
76-
- name: Windows x86_64 MSVC stable
77-
os: windows-latest
78-
rust: stable-msvc
79-
other: i686-pc-windows-msvc
80-
- name: Windows x86_64 gnu nightly # runs out of space while trying to link the test suite
81-
os: windows-latest
82-
rust: nightly-gnu
83-
other: i686-pc-windows-gnu
93+
matrix: ${{ fromJSON(needs.matrix.outputs.MATRIX) }}
8494
name: Tests ${{ matrix.name }}
8595
steps:
8696
- uses: actions/checkout@v3
@@ -100,7 +110,6 @@ jobs:
100110
- name: Configure extra test environment
101111
run: echo CARGO_CONTAINER_TESTS=1 >> $GITHUB_ENV
102112
if: matrix.os == 'ubuntu-latest'
103-
104113
- run: cargo test
105114
- name: Clear intermediate test output
106115
run: ci/clean-test-output.sh
@@ -170,6 +179,9 @@ jobs:
170179

171180
build_std:
172181
runs-on: ubuntu-latest
182+
needs:
183+
- channel
184+
if: ${{ needs.channel.outputs.CHANNEL == 'master' }}
173185
steps:
174186
- uses: actions/checkout@v3
175187
- run: rustup update nightly && rustup default nightly
@@ -180,6 +192,8 @@ jobs:
180192
CARGO_RUN_BUILD_STD_TESTS: 1
181193
docs:
182194
runs-on: ubuntu-latest
195+
needs:
196+
- channel
183197
steps:
184198
- uses: actions/checkout@v3
185199
- run: rustup update nightly && rustup default nightly
@@ -196,10 +210,12 @@ jobs:
196210
env:
197211
RUSTDOCFLAGS: -D warnings
198212
- run: cd src/doc && mdbook build --dest-dir ../../target/doc
199-
- run: |
213+
- name: Run linkchecker.sh
214+
run: |
215+
BRANCH=${{ needs.channel.outputs.CHANNEL }}
200216
cd src/doc
201217
curl -sSLo linkcheck.sh \
202-
https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh
218+
"https://raw.githubusercontent.com/rust-lang/rust/$BRANCH/src/tools/linkchecker/linkcheck.sh"
203219
sh linkcheck.sh --all cargo
204220
205221
success:

ci/matrix.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[
2+
{
3+
"name": "Linux x86_64 stable",
4+
"os": "ubuntu-latest",
5+
"rust": "stable",
6+
"other": "i686-unknown-linux-gnu"
7+
},
8+
{
9+
"name": "Linux x86_64 beta",
10+
"os": "ubuntu-latest",
11+
"rust": "beta",
12+
"other": "i686-unknown-linux-gnu"
13+
},
14+
{
15+
"name": "Linux x86_64 nightly",
16+
"os": "ubuntu-latest",
17+
"rust": "nightly",
18+
"other": "i686-unknown-linux-gnu"
19+
},
20+
{
21+
"name": "macOS x86_64 stable",
22+
"os": "macos-latest",
23+
"rust": "stable",
24+
"other": "x86_64-apple-ios"
25+
},
26+
{
27+
"name": "macOS x86_64 nightly",
28+
"os": "macos-latest",
29+
"rust": "nightly",
30+
"other": "x86_64-apple-ios"
31+
},
32+
{
33+
"name": "Windows x86_64 MSVC stable",
34+
"os": "windows-latest",
35+
"rust": "stable-msvc",
36+
"other": "i686-pc-windows-msvc"
37+
},
38+
{
39+
"name": "Windows x86_64 gnu nightly",
40+
"os": "windows-latest",
41+
"rust": "nightly-gnu",
42+
"other": "i686-pc-windows-gnu"
43+
}
44+
]

ci/which-channel.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
# This script outputs the channel where a CI workflow wants to merge into.
3+
#
4+
# Inputs:
5+
# BASE_SHA The commit SHA of the branch where the PR wants to merge into.
6+
#
7+
# GitHub Action Outputs:
8+
# CHANNEL Target channel where the PR wants to merge into.
9+
10+
set -euo pipefail
11+
12+
# When `BASE_SHA` is missing, we assume it is from bors merge commit,
13+
# so hope `HEAD~` to find the previous commit on master branch.
14+
base_sha=$(git rev-parse "${BASE_SHA:-HEAD~1}")
15+
16+
# Get symbolic names for the base_sha.
17+
# Assumption: Cargo branches are always in the format of `rust-1.*.0`,
18+
# otherwise `git name-rev` will return "undefined".
19+
ref=$(git name-rev --name-only --refs='origin/rust-1.*.0' $base_sha)
20+
21+
# Get the latest `rust-1.*.0` branch from remote origin.
22+
# Assumption: The latest branch is always beta branch.
23+
beta=$(git branch --remotes --list 'origin/rust-1.*.0' | sort | tail -n1 | tr -d "[:space:]")
24+
25+
master=$(git rev-parse origin/master)
26+
27+
# Backport pull requests always target at a `rust-1.*.0` branch.
28+
if [[ "$ref" = "undefined" ]] || [[ "$base_sha" = "$master" ]]
29+
then
30+
# Should be nightly but for convenience in CI let's call it master.
31+
channel="master"
32+
else
33+
if [[ "$ref" = "$beta" ]]
34+
then
35+
channel="beta"
36+
else
37+
channel="stable"
38+
fi
39+
fi
40+
41+
echo "Base sha: $base_sha"
42+
echo "Git Ref: $ref"
43+
echo "master: $master"
44+
echo "beta: $beta"
45+
echo "Channel: $channel"
46+
47+
echo "CHANNEL=$channel" >> "$GITHUB_OUTPUT"

0 commit comments

Comments
 (0)