Skip to content

Commit df794cd

Browse files
committed
improve CI for tag publishing
1 parent 2c2a65e commit df794cd

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

.github/workflows/ci.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
on:
22
pull_request:
33
push:
4-
branches: [main, staging, trying]
4+
branches: [main, staging, trying, v*.*.*]
55
tags:
66
- "v*.*.*"
77

@@ -351,7 +351,13 @@ jobs:
351351
runs-on: ubuntu-latest
352352
steps:
353353
- uses: actions/checkout@v3
354+
with:
355+
fetch-depth: 0 # fetch tags for publish
356+
ssh-key: "${{ secrets.COMMIT_KEY }}" # use deploy key to trigger workflow
354357
- uses: ./.github/actions/setup-rust
358+
- run: cargo xtask ci-job release
359+
env:
360+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
355361
- uses: ./.github/actions/cargo-publish
356362
with:
357363
cargo-registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ keywords = ["cross", "compilation", "testing", "tool"]
66
license = "MIT OR Apache-2.0"
77
name = "cross"
88
repository = "https://github.com/cross-rs/cross"
9-
version = "0.2.4"
9+
version = "0.2.5"
1010
edition = "2021"
1111
include = [
1212
"src/**/*",

xtask/src/ci.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod release;
12
mod target_matrix;
23

34
use crate::util::gha_output;
@@ -32,6 +33,7 @@ pub enum CiJob {
3233
#[clap(long, env = "COMMIT_AUTHOR")]
3334
author: String,
3435
},
36+
Release(release::Release),
3537
}
3638

3739
pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
@@ -117,6 +119,9 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
117119
CiJob::TargetMatrix { message, author } => {
118120
target_matrix::run(message, author)?;
119121
}
122+
CiJob::Release(r) => {
123+
r.run(&mut cross::shell::Verbosity::Verbose(1).into())?;
124+
}
120125
}
121126
Ok(())
122127
}

xtask/src/ci/release.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use clap::Args;
2+
use cross::{shell::MessageInfo, CommandExt};
3+
4+
#[derive(Debug, Args)]
5+
pub struct Release {
6+
#[clap(long, default_value = "main", env = "DEFAULT_BRANCH")]
7+
default_branch: String,
8+
#[clap(long, hide = true, env = "GITHUB_REF_TYPE")]
9+
pub ref_type: Option<String>,
10+
#[clap(long, hide = true, env = "GITHUB_REF_NAME")]
11+
ref_name: Option<String>,
12+
}
13+
14+
impl Release {
15+
pub fn run(&self, msg_info: &mut MessageInfo) -> Result<(), color_eyre::Report> {
16+
if self.ref_type.as_deref() == Some("branch") {
17+
self.tag(msg_info)?;
18+
}
19+
Ok(())
20+
}
21+
22+
pub fn tag(&self, msg_info: &mut MessageInfo) -> Result<(), color_eyre::Report> {
23+
color_eyre::eyre::ensure!(
24+
self.ref_type.as_deref() == Some("branch"),
25+
"tag() should only be called on a branch"
26+
);
27+
let current_branch = self.ref_name.as_deref().unwrap();
28+
let version = pkgid()?.rsplit_once('#').unwrap().1.trim().to_string();
29+
let tag = format!("v{version}");
30+
31+
let has_tag = std::process::Command::new("git")
32+
.args(["tag", "--list"])
33+
.run_and_get_stdout(msg_info)?
34+
.lines()
35+
.any(|it| it.trim() == tag);
36+
if !has_tag {
37+
let dry_run = std::env::var("CI").is_err()
38+
|| (current_branch != self.default_branch
39+
&& !wildmatch::WildMatch::new("v*.*.*").matches(current_branch));
40+
41+
eprint!("Taging!");
42+
let mut tagging = std::process::Command::new("git");
43+
tagging.args(["tag", &tag]);
44+
let mut push = std::process::Command::new("git");
45+
push.args(["push", "--tags"]);
46+
if dry_run {
47+
eprintln!(" (dry run)");
48+
tagging.print(msg_info)?;
49+
push.print(msg_info)?;
50+
} else {
51+
eprintln!();
52+
tagging.run(msg_info, false)?;
53+
push.run(msg_info, false)?;
54+
}
55+
}
56+
Ok(())
57+
}
58+
}
59+
60+
#[track_caller]
61+
fn pkgid() -> Result<String, color_eyre::Report> {
62+
cross::cargo_command()
63+
.arg("pkgid")
64+
.current_dir(crate::util::get_cargo_workspace())
65+
.run_and_get_stdout(&mut cross::shell::Verbosity::Verbose(1).into())
66+
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn assert_pkgid_hashtag() {
74+
let pkgid = pkgid().unwrap();
75+
assert!(!pkgid.contains('@'));
76+
assert!(pkgid.contains("cross"));
77+
}
78+
}

0 commit comments

Comments
 (0)