Skip to content

Commit

Permalink
check if version is latest or not
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Jun 24, 2022
1 parent 69bb233 commit 953c23e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ jobs:
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
Expand Down Expand Up @@ -256,6 +259,7 @@ jobs:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
LABELS: ${{ steps.docker-meta.outputs.labels }}
IS_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 @@ -302,6 +306,7 @@ jobs:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
LABELS: ${{ steps.docker-meta.outputs.labels }}
IS_LATEST: ${{ needs.check.outputs.is-latest || 'false' }}
shell: bash

publish:
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"
8 changes: 6 additions & 2 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(long, hide = true, env = "IS_LATEST")]
is_latest: Option<bool>,
/// Specify a tag to use instead of the derived one, eg `local`
#[clap(long)]
tag: Option<String>,
Expand Down Expand Up @@ -80,6 +82,7 @@ pub fn build_docker_image(
BuildDockerImage {
ref_type,
ref_name,
is_latest,
tag: tag_override,
repository,
labels,
Expand Down Expand Up @@ -164,6 +167,7 @@ pub fn build_docker_image(
&repository,
ref_type,
ref_name,
is_latest.unwrap_or_default(),
&version,
)?),
_ => {
Expand Down Expand Up @@ -276,6 +280,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 @@ -289,8 +294,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
28 changes: 24 additions & 4 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Subcommand;
use cross::CargoMetadata;
use cross::{cargo_command, CargoMetadata, CommandExt};

#[derive(Subcommand, Debug)]
pub enum CiJob {
Expand Down Expand Up @@ -59,6 +59,7 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
cross::docker::CROSS_IMAGE,
&ref_type,
&ref_name,
false,
&version,
)?[0],
);
Expand All @@ -68,9 +69,28 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
}
}
CiJob::Check { ref_type, ref_name } => {
let version = cross_meta.version.clone();
if ref_type == "tag" && 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 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")
}
}
}
}
Expand Down

0 comments on commit 953c23e

Please sign in to comment.