Skip to content

Commit

Permalink
Re-add install-rust action
Browse files Browse the repository at this point in the history
This re-adds install-rust action removed in a91c37d.

rustup sometimes fails due to temporary network problems even when
RUSTUP_MAX_RETRIES is set, so an action with retry is needed.
  • Loading branch information
taiki-e committed Sep 27, 2024
1 parent d03f3b3 commit 3d23ff7
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ jobs:
ls -- /Applications | grep -F Xcode
if: startsWith(matrix.os, 'macos')
install-rust:
runs-on: ubuntu-latest
steps:
- uses: taiki-e/checkout-action@v1
- uses: ./install-rust
with:
toolchain: nightly
component: rustfmt,clippy
target: x86_64-unknown-linux-musl,i686-unknown-linux-gnu
- run: |
rustc -Vv
rustup -V
cargo -Vv
rustfmt -V
cargo clippy -V
rustup component list
rustup target list
setup-docker:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ supposed to only be used in infra managed by us.
## Actions

- [**free-device-space**](free-device-space): Free device space.
- [**install-rust**](install-rust): Install Rust toolchain.
- [**setup-docker**](setup-docker): Setup docker.

## Reusable workflows
Expand Down Expand Up @@ -38,7 +39,6 @@ These actions were previously included in this repository but have been moved in
These actions were previously included in this repository but have been removed.

- **deploy-gh-pages**: removed because no longer used.
- **install-rust**: removed in favor of calling `rustup` directly.
- **update-dependabot-pr**: removed because no longer used.

[checkout-action]: https://github.com/taiki-e/checkout-action
Expand Down
20 changes: 20 additions & 0 deletions install-rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# install-rust

GitHub Action for installing Rust toolchain.
There is no stability guarantee for this action, since it's supposed to only be
used in infra managed by us.

## Usage

See [action.yml](action.yml)

```yaml
- uses: taiki-e/github-actions/install-rust@main
with:
# Default toolchain to install.
toolchain: stable
# Components to add (comma-separated), default is empty.
component: rustfmt,clippy
# Targets to add (comma-separated), default is empty.
target: x86_64-unknown-linux-musl
```
27 changes: 27 additions & 0 deletions install-rust/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: install-rust
description: GitHub Action for installing Rust toolchain

inputs:
toolchain:
description: Default toolchain to install
required: true
# default: #publish:toolchain
component:
description: Components to add (comma-separated)
required: false
target:
description: Targets to add (comma-separated)
required: false

# Note:
# - inputs.* should be manually mapped to INPUT_* due to https://github.com/actions/runner/issues/665
# - Use GITHUB_*/RUNNER_* instead of github.*/runner.* due to https://github.com/actions/runner/issues/2185
runs:
using: composite
steps:
- run: bash --noprofile --norc "${GITHUB_ACTION_PATH:?}/main.sh"
shell: bash
env:
INPUT_TOOLCHAIN: ${{ inputs.toolchain }}
INPUT_COMPONENT: ${{ inputs.component }}
INPUT_TARGET: ${{ inputs.target }}
40 changes: 40 additions & 0 deletions install-rust/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0 OR MIT
set -CeEuo pipefail
IFS=$'\n\t'
trap -- 's=$?; printf >&2 "%s\n" "${0##*/}:${LINENO}: \`${BASH_COMMAND}\` exit with ${s}"; exit ${s}' ERR

g() {
IFS=' '
local cmd="$*"
IFS=$'\n\t'
printf '::group::%s\n' "${cmd#retry }"
"$@"
printf '::endgroup::\n'
}
retry() {
for i in {1..10}; do
if "$@"; then
return 0
else
sleep "${i}"
fi
done
"$@"
}

export RUSTUP_MAX_RETRIES="${RUSTUP_MAX_RETRIES:-10}"

# --no-self-update is necessary because the windows environment cannot self-update rustup.exe.
rustup_args=(--no-self-update --profile minimal)
toolchain="${INPUT_TOOLCHAIN:?}"
if [[ -n "${INPUT_COMPONENT:-}" ]]; then
rustup_args+=("--component=${INPUT_COMPONENT}")
fi
if [[ -n "${INPUT_TARGET:-}" ]]; then
rustup_args+=("--target=${INPUT_TARGET}")
fi

g retry rustup toolchain add "${toolchain}" "${rustup_args[@]}"

g rustup default "${toolchain}"
68 changes: 68 additions & 0 deletions install-rust/update-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0 OR MIT
set -CeEuo pipefail
IFS=$'\n\t'
trap -- 's=$?; printf >&2 "%s\n" "${0##*/}:${LINENO}: \`${BASH_COMMAND}\` exit with ${s}"; exit ${s}' ERR
cd -- "$(dirname -- "$0")"/..

# Update stable/beta/nightly branches.
#
# USAGE:
# ./install-rust/update-branch.sh

retry() {
for i in {1..10}; do
if "$@"; then
return 0
else
sleep "${i}"
fi
done
"$@"
}
bail() {
printf >&2 'error: %s\n' "$*"
exit 1
}

if [[ $# -gt 1 ]]; then
bail "invalid argument '$2'"
fi
if { sed --help 2>&1 || true; } | grep -Eq -e '-i extension'; then
in_place=(-i '')
else
in_place=(-i)
fi

# Make sure there is no uncommitted change.
git diff --exit-code
git diff --exit-code --staged

# Make sure that the release was created from an allowed branch.
if ! git branch | grep -Eq '\* main$'; then
bail "current branch is not 'main'"
fi
if ! git remote -v | grep -F origin | grep -Eq 'github\.com[:/]taiki-e/'; then
bail "cannot publish a new release from fork repository"
fi

set -x

retry git push origin main

toolchains=(
stable
beta
nightly
)

for toolchain in "${toolchains[@]}"; do
git checkout -b "${toolchain}"
sed -E "${in_place[@]}" "s/required: true/required: false/g" install-rust/action.yml
sed -E "${in_place[@]}" "s/# default: #publish:toolchain/default: ${toolchain}/g" install-rust/action.yml
git add install-rust/action.yml
git commit -m "${toolchain}"
retry git push origin -f refs/heads/"${toolchain}"
git checkout main
git branch -D "${toolchain}"
done

0 comments on commit 3d23ff7

Please sign in to comment.