bump_patch_version.rs

  1use gh_workflow::*;
  2
  3use crate::tasks::workflows::{
  4    runners,
  5    steps::{self, CheckoutStep, named},
  6    vars::{StepOutput, WorkflowInput},
  7};
  8
  9pub fn bump_patch_version() -> Workflow {
 10    let branch = WorkflowInput::string("branch", None).description("Branch name to run on");
 11    let bump_patch_version_job = run_bump_patch_version(&branch);
 12    named::workflow()
 13        .on(Event::default()
 14            .workflow_dispatch(WorkflowDispatch::default().add_input(branch.name, branch.input())))
 15        .concurrency(
 16            Concurrency::new(Expression::new(format!(
 17                "${{{{ github.workflow }}}}-{branch}"
 18            )))
 19            .cancel_in_progress(true),
 20        )
 21        .add_job(bump_patch_version_job.name, bump_patch_version_job.job)
 22}
 23
 24fn run_bump_patch_version(branch: &WorkflowInput) -> steps::NamedJob {
 25    fn checkout_branch(branch: &WorkflowInput, token: &StepOutput) -> CheckoutStep {
 26        steps::checkout_repo()
 27            .with_token(token)
 28            .with_ref(branch.to_string())
 29    }
 30
 31    fn bump_version() -> Step<Run> {
 32        named::bash(indoc::indoc! {r#"
 33            channel="$(cat crates/zed/RELEASE_CHANNEL)"
 34
 35            tag_suffix=""
 36            case $channel in
 37              stable)
 38                ;;
 39              preview)
 40                tag_suffix="-pre"
 41                ;;
 42              *)
 43                echo "this must be run on either of stable|preview release branches" >&2
 44                exit 1
 45                ;;
 46            esac
 47            which cargo-set-version > /dev/null || cargo install cargo-edit -f --no-default-features --features "set-version"
 48            version="$(cargo set-version -p zed --bump patch 2>&1 | sed 's/.* //')"
 49            echo "version=$version" >> "$GITHUB_OUTPUT"
 50            echo "tag_suffix=$tag_suffix" >> "$GITHUB_OUTPUT"
 51        "#})
 52        .id("bump-version")
 53    }
 54
 55    fn commit_changes(
 56        version: &StepOutput,
 57        token: &StepOutput,
 58        branch: &WorkflowInput,
 59    ) -> Step<Use> {
 60        named::uses(
 61            "IAreKyleW00t",
 62            "verified-bot-commit",
 63            "126a6a11889ab05bcff72ec2403c326cd249b84c", // v2.3.0
 64        )
 65        .id("commit")
 66        .add_with((
 67            "message",
 68            format!("Bump to {version} for @${{{{ github.actor }}}}"),
 69        ))
 70        .add_with(("ref", format!("refs/heads/{branch}")))
 71        .add_with(("files", "**"))
 72        .add_with(("token", token.to_string()))
 73    }
 74
 75    fn create_version_tag(
 76        version: &StepOutput,
 77        tag_suffix: &StepOutput,
 78        commit_sha: &StepOutput,
 79        token: &StepOutput,
 80    ) -> Step<Use> {
 81        named::uses(
 82            "actions",
 83            "github-script",
 84            "f28e40c7f34bde8b3046d885e986cb6290c5673b", // v7
 85        )
 86        .with(
 87            Input::default()
 88                .add(
 89                    "script",
 90                    indoc::formatdoc! {r#"
 91                        github.rest.git.createRef({{
 92                            owner: context.repo.owner,
 93                            repo: context.repo.repo,
 94                            ref: 'refs/tags/v{version}{tag_suffix}',
 95                            sha: '{commit_sha}'
 96                        }})
 97                    "#},
 98                )
 99                .add("github-token", token.to_string()),
100        )
101    }
102
103    let (authenticate, token) = steps::authenticate_as_zippy().into();
104    let bump_version_step = bump_version();
105    let version = StepOutput::new(&bump_version_step, "version");
106    let tag_suffix = StepOutput::new(&bump_version_step, "tag_suffix");
107    let commit_step = commit_changes(&version, &token, branch);
108    let commit_sha = StepOutput::new_unchecked(&commit_step, "commit");
109
110    named::job(
111        Job::default()
112            .cond(Expression::new(
113                "github.repository_owner == 'zed-industries'",
114            ))
115            .runs_on(runners::LINUX_XL)
116            .add_step(authenticate)
117            .add_step(checkout_branch(branch, &token))
118            .add_step(bump_version_step)
119            .add_step(commit_step)
120            .add_step(create_version_tag(
121                &version,
122                &tag_suffix,
123                &commit_sha,
124                &token,
125            )),
126    )
127}