Skip to content

Commit

Permalink
chore: detect the channel a PR wants to merge into
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed May 25, 2023
1 parent ec8a8a0 commit 43ae784
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 33 deletions.
82 changes: 49 additions & 33 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ env:
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse

jobs:
# Determine which channel will be merged into.
channel:
runs-on: ubuntu-latest
outputs:
CHANNEL: ${{ steps.channel.outputs.CHANNEL }}
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # fetch all branches
- id: channel
run: ci/which-channel.sh

# Check Code style quickly by running `rustfmt` over all code
rustfmt:
runs-on: ubuntu-latest
Expand All @@ -41,8 +55,33 @@ jobs:
# Only check cargo lib for now
- run: cargo clippy -p cargo --lib -- -D warnings

# Generate strategy matrix for different platforms and channels
# (see ci/matrix.json)
matrix:
runs-on: ubuntu-latest
needs:
- channel
outputs:
matrix: ${{ steps.matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v3
- name: Generate strategy matrix
id: matrix
run: |
CHANNEL=${{ needs.channel.outputs.CHANNEL }}
MATRIX=$(
jq --arg C "$CHANNEL" 'map (. |
if ($C == "beta") then select(.rust | startswith("nightly") | not)
elif ($C == "stable") then select(.rust | startswith("stable"))
else . end)' ci/matrix.json
)
echo "$MATRIX"
echo "MATRIX={\"include\":$(echo $MATRIX)}" >> "$GITHUB_OUTPUT"
test:
runs-on: ${{ matrix.os }}
needs:
- matrix
env:
CARGO_PROFILE_DEV_DEBUG: 1
CARGO_PROFILE_TEST_DEBUG: 1
Expand All @@ -51,36 +90,7 @@ jobs:
# Deny warnings on CI to avoid warnings getting into the codebase.
RUSTFLAGS: -D warnings
strategy:
matrix:
include:
- name: Linux x86_64 stable
os: ubuntu-latest
rust: stable
other: i686-unknown-linux-gnu
- name: Linux x86_64 beta
os: ubuntu-latest
rust: beta
other: i686-unknown-linux-gnu
- name: Linux x86_64 nightly
os: ubuntu-latest
rust: nightly
other: i686-unknown-linux-gnu
- name: macOS x86_64 stable
os: macos-latest
rust: stable
other: x86_64-apple-ios
- name: macOS x86_64 nightly
os: macos-latest
rust: nightly
other: x86_64-apple-ios
- name: Windows x86_64 MSVC stable
os: windows-latest
rust: stable-msvc
other: i686-pc-windows-msvc
- name: Windows x86_64 gnu nightly # runs out of space while trying to link the test suite
os: windows-latest
rust: nightly-gnu
other: i686-pc-windows-gnu
matrix: ${{ fromJSON(needs.matrix.outputs.MATRIX) }}
name: Tests ${{ matrix.name }}
steps:
- uses: actions/checkout@v3
Expand All @@ -100,7 +110,6 @@ jobs:
- name: Configure extra test environment
run: echo CARGO_CONTAINER_TESTS=1 >> $GITHUB_ENV
if: matrix.os == 'ubuntu-latest'

- run: cargo test
- name: Clear intermediate test output
run: ci/clean-test-output.sh
Expand Down Expand Up @@ -170,6 +179,9 @@ jobs:

build_std:
runs-on: ubuntu-latest
needs:
- channel
if: ${{ needs.channel.outputs.CHANNEL == 'master' }}
steps:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
Expand All @@ -180,6 +192,8 @@ jobs:
CARGO_RUN_BUILD_STD_TESTS: 1
docs:
runs-on: ubuntu-latest
needs:
- channel
steps:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
Expand All @@ -196,10 +210,12 @@ jobs:
env:
RUSTDOCFLAGS: -D warnings
- run: cd src/doc && mdbook build --dest-dir ../../target/doc
- run: |
- name: Run linkchecker.sh
run: |
BRANCH=${{ needs.channel.outputs.CHANNEL }}
cd src/doc
curl -sSLo linkcheck.sh \
https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh
"https://raw.githubusercontent.com/rust-lang/rust/$BRANCH/src/tools/linkchecker/linkcheck.sh"
sh linkcheck.sh --all cargo
success:
Expand Down
44 changes: 44 additions & 0 deletions ci/matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"name": "Linux x86_64 stable",
"os": "ubuntu-latest",
"rust": "stable",
"other": "i686-unknown-linux-gnu"
},
{
"name": "Linux x86_64 beta",
"os": "ubuntu-latest",
"rust": "beta",
"other": "i686-unknown-linux-gnu"
},
{
"name": "Linux x86_64 nightly",
"os": "ubuntu-latest",
"rust": "nightly",
"other": "i686-unknown-linux-gnu"
},
{
"name": "macOS x86_64 stable",
"os": "macos-latest",
"rust": "stable",
"other": "x86_64-apple-ios"
},
{
"name": "macOS x86_64 nightly",
"os": "macos-latest",
"rust": "nightly",
"other": "x86_64-apple-ios"
},
{
"name": "Windows x86_64 MSVC stable",
"os": "windows-latest",
"rust": "stable-msvc",
"other": "i686-pc-windows-msvc"
},
{
"name": "Windows x86_64 gnu nightly",
"os": "windows-latest",
"rust": "nightly-gnu",
"other": "i686-pc-windows-gnu"
}
]
47 changes: 47 additions & 0 deletions ci/which-channel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
# This script outputs the channel where a CI workflow wants to merge into.
#
# Inputs:
# BASE_SHA The commit SHA of the branch where the PR wants to merge into.
#
# GitHub Action Outputs:
# CHANNEL Target channel where the PR wants to merge into.

set -euo pipefail

# When `BASE_SHA` is missing, we assume it is from bors merge commit,
# so hope `HEAD~` to find the previous commit on master branch.
base_sha=$(git rev-parse "${BASE_SHA:-HEAD~1}")

# Get symbolic names for the base_sha.
# Assumption: Cargo branches are always in the format of `rust-1.*.0`,
# otherwise `git name-rev` will return "undefined".
ref=$(git name-rev --name-only --refs='origin/rust-1.*.0' $base_sha)

# Get the latest `rust-1.*.0` branch from remote origin.
# Assumption: The latest branch is always beta branch.
beta=$(git branch --remotes --list 'origin/rust-1.*.0' | sort | tail -n1 | tr -d "[:space:]")

master=$(git rev-parse origin/master)

# Backport pull requests always target at a `rust-1.*.0` branch.
if [[ "$ref" = "undefined" ]] || [[ "$base_sha" = "$master" ]]
then
# Should be nightly but for convenience in CI let's call it master.
channel="master"
else
if [[ "$ref" = "$beta" ]]
then
channel="beta"
else
channel="stable"
fi
fi

echo "Base sha: $base_sha"
echo "Git Ref: $ref"
echo "master: $master"
echo "beta: $beta"
echo "Channel: $channel"

echo "CHANNEL=$channel" >> "$GITHUB_OUTPUT"

0 comments on commit 43ae784

Please sign in to comment.