Skip to content

Commit

Permalink
Merge #837
Browse files Browse the repository at this point in the history
837: check tag with package version early and determine if it's the latest version r=Alexhuszagh a=Emilgardis



Co-authored-by: Emil Gardström <[email protected]>
  • Loading branch information
bors[bot] and Emilgardis authored Jun 24, 2022
2 parents aea7bde + 773d2d0 commit 2b167b8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ jobs:
command: test
args: --locked --all-targets --workspace --all-features
timeout-minutes: 10

check:
runs-on: ubuntu-latest
outputs:
is-latest: ${{ steps.check.outputs.is-latest }}
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-rust
- run: cargo xtask ci-job check
id: check
shell: bash
generate-matrix:
runs-on: ubuntu-latest
outputs:
Expand Down Expand Up @@ -199,7 +208,7 @@ jobs:
build:
name: target (${{ matrix.pretty }},${{ matrix.os }})
runs-on: ${{ matrix.os }}
needs: [shellcheck, test, generate-matrix]
needs: [shellcheck, test, generate-matrix, check]
if: github.event_name == 'push'
strategy:
fail-fast: false
Expand Down Expand Up @@ -249,6 +258,7 @@ jobs:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
LABELS: ${{ steps.docker-meta.outputs.labels }}
LATEST: ${{ needs.check.outputs.is-latest || 'false' }}
shell: bash
- name: Set Docker image for test
if: steps.prepare-meta.outputs.has-image
Expand Down Expand Up @@ -295,10 +305,11 @@ jobs:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
LABELS: ${{ steps.docker-meta.outputs.labels }}
LATEST: ${{ needs.check.outputs.is-latest || 'false' }}
shell: bash

publish:
needs: [build]
needs: [build, check]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -309,7 +320,7 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}

conclusion:
needs: [shellcheck, fmt, clippy, test, generate-matrix, build, publish]
needs: [shellcheck, fmt, clippy, test, generate-matrix, build, publish, check]
if: always()
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
serde_json = "1.0"
once_cell = "1.12"
semver = "1"
10 changes: 7 additions & 3 deletions xtask/src/build_docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct BuildDockerImage {
ref_type: Option<String>,
#[clap(long, hide = true, env = "GITHUB_REF_NAME")]
ref_name: Option<String>,
#[clap(action, long = "latest", hide = true, env = "LATEST")]
is_latest: bool,
/// Specify a tag to use instead of the derived one, eg `local`
#[clap(long)]
tag: Option<String>,
Expand Down Expand Up @@ -38,7 +40,7 @@ pub struct BuildDockerImage {
/// Docker build progress output type.
#[clap(
long,
value_parser = clap::builder::PossibleValuesParser::new(["auto", "plain", "tty"]),
value_parser = clap::builder::PossibleValuesParser::new(["auto", "plain", "tty"]),
default_value = "auto"
)]
progress: String,
Expand Down Expand Up @@ -83,6 +85,7 @@ pub fn build_docker_image(
BuildDockerImage {
ref_type,
ref_name,
is_latest,
tag: tag_override,
repository,
labels,
Expand Down Expand Up @@ -168,6 +171,7 @@ pub fn build_docker_image(
&repository,
ref_type,
ref_name,
is_latest,
&version,
)?),
_ => {
Expand Down Expand Up @@ -283,6 +287,7 @@ pub fn determine_image_name(
repository: &str,
ref_type: &str,
ref_name: &str,
is_latest: bool,
version: &str,
) -> cross::Result<Vec<String>> {
let mut tags = vec![];
Expand All @@ -296,8 +301,7 @@ pub fn determine_image_name(
}
tags.push(target.image_name(repository, version));
// Check for unstable releases, tag stable releases as `latest`
if version.contains('-') {
// TODO: Don't tag if version is older than currently released version.
if is_latest {
tags.push(target.image_name(repository, "latest"))
}
}
Expand Down
55 changes: 42 additions & 13 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::Path;

use clap::Subcommand;
use cross::{cargo_command, CargoMetadata, CommandExt};

#[derive(Subcommand, Debug)]
pub enum CiJob {
/// Return needed metadata for building images
PrepareMeta {
// tag, branch
#[clap(long, env = "GITHUB_REF_TYPE")]
Expand All @@ -13,25 +13,28 @@ pub enum CiJob {
ref_name: String,
target: crate::ImageTarget,
},
/// Check workspace metadata.
Check {
// tag, branch
#[clap(long, env = "GITHUB_REF_TYPE")]
ref_type: String,
// main, v0.1.0
#[clap(long, env = "GITHUB_REF_NAME")]
ref_name: String,
},
}

pub fn ci(args: CiJob) -> cross::Result<()> {
pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
let cross_meta = metadata
.get_package("cross")
.expect("cross expected in workspace");

match args {
CiJob::PrepareMeta {
ref_type,
ref_name,
target,
} => {
let metadata = cross::cargo_metadata_with_args(
Some(Path::new(env!("CARGO_MANIFEST_DIR"))),
None,
true,
)?
.ok_or_else(|| eyre::eyre!("could not find cross workspace and its current version"))?;
let cross_meta = metadata
.get_package("cross")
.expect("cross expected in workspace");

// Set labels
let mut labels = vec![];

Expand All @@ -56,6 +59,7 @@ pub fn ci(args: CiJob) -> cross::Result<()> {
cross::docker::CROSS_IMAGE,
&ref_type,
&ref_name,
false,
&version,
)?[0],
);
Expand All @@ -64,6 +68,31 @@ pub fn ci(args: CiJob) -> cross::Result<()> {
gha_output("has-image", "true")
}
}
CiJob::Check { ref_type, ref_name } => {
let version = semver::Version::parse(&cross_meta.version)?;
if ref_type == "tag" {
if ref_name.starts_with('v') && ref_name != format!("v{version}") {
eyre::bail!("a version tag was published, but the tag does not match the current version in Cargo.toml");
}
let search = cargo_command()
.args(&["search", "--limit", "1"])
.arg("cross")
.run_and_get_stdout(true)?;
let (cross, rest) = search
.split_once(" = ")
.ok_or_else(|| eyre::eyre!("cargo search failed"))?;
assert_eq!(cross, "cross");
// Note: this version includes pre-releases.
let latest_version = semver::Version::parse(
rest.split('"')
.nth(1)
.ok_or_else(|| eyre::eyre!("cargo search returned unexpected data"))?,
)?;
if version >= latest_version && version.pre.is_empty() {
gha_output("is-latest", "true")
}
}
}
}
Ok(())
}
Expand Down
10 changes: 9 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ pub fn main() -> cross::Result<()> {
Commands::Test(args) => {
hooks::test(args, cli.toolchain.as_deref())?;
}
Commands::CiJob(args) => ci::ci(args)?,
Commands::CiJob(args) => {
let metadata = cross::cargo_metadata_with_args(
Some(std::path::Path::new(env!("CARGO_MANIFEST_DIR"))),
None,
true,
)?
.ok_or_else(|| eyre::eyre!("could not find cross workspace"))?;
ci::ci(args, metadata)?
}
}

Ok(())
Expand Down

0 comments on commit 2b167b8

Please sign in to comment.