From 9e580a6ede05ba8784400645bc7c4f7c506b4bb1 Mon Sep 17 00:00:00 2001 From: iHsin Date: Sat, 21 Sep 2024 13:10:27 +0800 Subject: [PATCH] refactor(ci): rename CLASH_RS_CI to CLASH_DOCKER_TEST (#601) * ci: rename CLASH_RS_CI to CLASH_DOCKER_TEST * ci(cross): add environment passthrough * simplification --- .github/workflows/ci.yml | 34 +++++++++++++++++------ Cross.toml | 4 +-- clash/build.rs | 10 +++++-- clash_lib/build.rs | 8 +++--- clash_lib/src/proxy/relay/mod.rs | 2 +- clash_lib/src/proxy/shadowsocks/mod.rs | 2 +- clash_lib/src/proxy/socks/outbound/mod.rs | 2 +- clash_lib/src/proxy/trojan/mod.rs | 2 +- clash_lib/src/proxy/utils/mod.rs | 2 +- clash_lib/src/proxy/vmess/mod.rs | 2 +- clash_lib/src/proxy/wg/mod.rs | 2 +- 11 files changed, 46 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 852e7116a..6e9d3e965 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -137,7 +137,21 @@ jobs: cross: false postfix: ".exe" extra-args: "--all-features" - rustflags: "-Ctarget-feature=+crt-static --cfg tokio_unstable" + rustflags: >- + -Ctarget-feature=+crt-static + -Clink-args=/NODEFAULTLIB:libvcruntimed.lib + -Clink-args=/NODEFAULTLIB:vcruntime.lib + -Clink-args=/NODEFAULTLIB:vcruntimed.lib + -Clink-args=/NODEFAULTLIB:libcmtd.lib + -Clink-args=/NODEFAULTLIB:msvcrt.lib + -Clink-args=/NODEFAULTLIB:msvcrtd.lib + -Clink-args=/NODEFAULTLIB:libucrt.lib + -Clink-args=/NODEFAULTLIB:libucrtd.lib + + -Clink-args=/DEFAULTLIB:libcmt.lib + -Clink-args=/DEFAULTLIB:libvcruntime.lib + -Clink-args=/DEFAULTLIB:ucrt.lib + --cfg tokio_unstable - os: windows-latest target: aarch64-pc-windows-msvc release-name: aarch64-pc-windows-msvc-static-crt @@ -189,11 +203,18 @@ jobs: toolchain: ${{ matrix.toolchain || 'nightly' }} target: ${{ matrix.target }} components: ${{ matrix.components || 'rustfmt, clippy' }} + - name: Install Protoc uses: arduino/setup-protoc@v3 with: version: "23.x" repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Set environment variables + run: | + echo "CLASH_GIT_REF=${GITHUB_REF}" >> $GITHUB_ENV + echo "CLASH_GIT_SHA=${GITHUB_SHA}" >> $GITHUB_ENV + - name: Cargo fmt uses: clechasseur/rs-cargo@v2 with: @@ -207,8 +228,6 @@ jobs: use-cross: ${{ matrix.cross }} command: clippy args: --all --target ${{ matrix.target }} ${{ matrix.extra-args }} -- -D warnings - env: - RUSTFLAGS: ${{ matrix.rustflags || '--cfg tokio_unstable' }} - name: Cargo test (docker test on linux) uses: clechasseur/rs-cargo@v2 @@ -219,6 +238,7 @@ jobs: args: --all --target ${{ matrix.target }} ${{ matrix.extra-args }} env: CROSS_CONTAINER_OPTS: "--network host" + CLASH_DOCKER_TEST: "true" RUSTFLAGS: ${{ matrix.rustflags || '--cfg tokio_unstable' }} - name: Cargo test (no docker test on windows-non-arm and macos) @@ -229,7 +249,6 @@ jobs: command: test args: --all --target ${{ matrix.target }} ${{ matrix.extra-args }} env: - CLASH_RS_CI: "true" RUSTFLAGS: ${{ matrix.rustflags || '--cfg tokio_unstable' }} - name: Cargo build @@ -353,9 +372,8 @@ jobs: with: submodules: true - - name: Get the current Git commit hash + - name: Set docker image's tag run: | - echo "OWNER=${GITHUB_REPOSITORY_OWNER@L}" >> $GITHUB_ENV echo "TAG_VERSION=${REGISTRY}/${GITHUB_REPOSITORY_OWNER@L}/${IMAGE_NAME}:${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV echo "TAG_LATEST=${REGISTRY}/${GITHUB_REPOSITORY_OWNER@L}/${IMAGE_NAME}:latest" >> $GITHUB_ENV @@ -386,7 +404,7 @@ jobs: uses: docker/login-action@v3 with: registry: ghcr.io - username: ${{ env.OWNER }} + username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push release @@ -395,7 +413,7 @@ jobs: context: . file: .github/Dockerfile platforms: linux/amd64,linux/arm64 - push: ${{ startsWith(github.ref, 'refs/tags/v') }} + push: true tags: ${{ env.TAG_VERSION }},${{ env.TAG_LATEST }} diff --git a/Cross.toml b/Cross.toml index b46139fb3..a05dc5b64 100644 --- a/Cross.toml +++ b/Cross.toml @@ -5,8 +5,8 @@ pre-build = [ ] [build.env] -# Docker in docker -volumes = ["/var/run/docker.sock=/var/run/docker.sock"] +volumes = ["/var/run/docker.sock=/var/run/docker.sock"] # Docker in docker +passthrough = ["CLASH_GIT_REF", "CLASH_GIT_SHA", "RUSTFLAGS", "CLASH_DOCKER_TEST"] [target.x86_64-unknown-linux-gnu] image = "ghcr.io/cross-rs/x86_64-unknown-linux-gnu:main" diff --git a/clash/build.rs b/clash/build.rs index 5fe939e9a..c39a4d900 100644 --- a/clash/build.rs +++ b/clash/build.rs @@ -1,8 +1,12 @@ #![feature(let_chains)] fn main() { - println!("cargo:rerun-if-env-changed=GITHUB_REF"); - let version = if let Some("refs/heads/master") = option_env!("GITHUB_REF") - && let Some(sha) = option_env!("GITHUB_SHA") + let vars = ["CLASH_GIT_REF", "CLASH_GIT_SHA"]; + for var in vars { + println!("cargo:rerun-if-env-changed={var}"); + } + + let version = if let Some("refs/heads/master") = option_env!("CLASH_GIT_REF") + && let Some(sha) = option_env!("CLASH_GIT_SHA") { let short_sha = &sha[..7]; // Nightly relase below diff --git a/clash_lib/build.rs b/clash_lib/build.rs index 444b0e8f6..b948ec80f 100644 --- a/clash_lib/build.rs +++ b/clash_lib/build.rs @@ -1,8 +1,8 @@ fn main() -> std::io::Result<()> { - println!("cargo::rustc-check-cfg=cfg(ci)"); - println!("cargo:rerun-if-env-changed=CLASH_RS_CI"); - if std::env::var("CLASH_RS_CI").is_ok() { - println!("cargo::rustc-cfg=ci"); + println!("cargo::rustc-check-cfg=cfg(docker_test)"); + println!("cargo:rerun-if-env-changed=CLASH_DOCKER_TEST"); + if let Some("1" | "true") = option_env!("CLASH_DOCKER_TEST") { + println!("cargo::rustc-cfg=docker_test"); } println!("cargo:rerun-if-changed=src/common/geodata/geodata.proto"); diff --git a/clash_lib/src/proxy/relay/mod.rs b/clash_lib/src/proxy/relay/mod.rs index c54ab1c23..0889dbc89 100644 --- a/clash_lib/src/proxy/relay/mod.rs +++ b/clash_lib/src/proxy/relay/mod.rs @@ -199,7 +199,7 @@ impl OutboundHandler for Handler { } #[cfg(feature = "shadowsocks")] -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use tokio::sync::RwLock; diff --git a/clash_lib/src/proxy/shadowsocks/mod.rs b/clash_lib/src/proxy/shadowsocks/mod.rs index 285b7f716..5e93903c3 100644 --- a/clash_lib/src/proxy/shadowsocks/mod.rs +++ b/clash_lib/src/proxy/shadowsocks/mod.rs @@ -291,7 +291,7 @@ impl OutboundHandler for Handler { } } -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use super::super::utils::test_utils::{ diff --git a/clash_lib/src/proxy/socks/outbound/mod.rs b/clash_lib/src/proxy/socks/outbound/mod.rs index 1857f1491..321eed690 100644 --- a/clash_lib/src/proxy/socks/outbound/mod.rs +++ b/clash_lib/src/proxy/socks/outbound/mod.rs @@ -276,7 +276,7 @@ impl OutboundHandler for Handler { } } -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use std::sync::Arc; diff --git a/clash_lib/src/proxy/trojan/mod.rs b/clash_lib/src/proxy/trojan/mod.rs index f879b872a..feb60bd24 100644 --- a/clash_lib/src/proxy/trojan/mod.rs +++ b/clash_lib/src/proxy/trojan/mod.rs @@ -254,7 +254,7 @@ impl OutboundHandler for Handler { } } -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use std::collections::HashMap; diff --git a/clash_lib/src/proxy/utils/mod.rs b/clash_lib/src/proxy/utils/mod.rs index 2e98c56fd..52d3599cb 100644 --- a/clash_lib/src/proxy/utils/mod.rs +++ b/clash_lib/src/proxy/utils/mod.rs @@ -3,7 +3,7 @@ use std::{ net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, }; -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] pub mod test_utils; mod platform; diff --git a/clash_lib/src/proxy/vmess/mod.rs b/clash_lib/src/proxy/vmess/mod.rs index 20f3b11ac..ae5736f3b 100644 --- a/clash_lib/src/proxy/vmess/mod.rs +++ b/clash_lib/src/proxy/vmess/mod.rs @@ -286,7 +286,7 @@ impl OutboundHandler for Handler { } } -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use crate::proxy::utils::test_utils::{ config_helper::test_config_base_dir, diff --git a/clash_lib/src/proxy/wg/mod.rs b/clash_lib/src/proxy/wg/mod.rs index f7dfa47c9..6c028e025 100644 --- a/clash_lib/src/proxy/wg/mod.rs +++ b/clash_lib/src/proxy/wg/mod.rs @@ -317,7 +317,7 @@ impl OutboundHandler for Handler { } } -#[cfg(all(test, not(ci)))] +#[cfg(all(test, docker_test))] mod tests { use crate::proxy::utils::{