From 67df0bb8148af839cae4554d455fc55cb068304d Mon Sep 17 00:00:00 2001 From: nikita-skobov Date: Sat, 17 Oct 2020 19:42:28 -0500 Subject: [PATCH 1/4] adds latest commit hash to version string by using a build script --- build.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 7 ++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d0c860b --- /dev/null +++ b/build.rs @@ -0,0 +1,33 @@ +use std::process::Command; +use std::process::Stdio; + +pub fn get_latest_commit() -> String { + let command_and_args = [ + "git", "log", "--oneline", + "--pretty=%h", "-n", "1", + ]; + let mut proc = Command::new(command_and_args[0]); + proc.args(&command_and_args[1..]); + + proc.stdin(Stdio::null()); + let output = proc.output(); + + + let err_str = "Haha oops! this build failed to get the latest commit hash. ¯\\_(ツ)_/¯"; + match output { + Err(_) => return err_str.into(), + Ok(out) => { + let status_code = out.status.code().unwrap_or(1); + if status_code == 0 { + return String::from_utf8_lossy(&out.stdout).into(); + } else { + return err_str.into(); + } + } + } +} + +fn main() { + let latest_commit = get_latest_commit(); + println!("cargo:rustc-env=LATEST_COMMIT={}", latest_commit); +} diff --git a/src/main.rs b/src/main.rs index 7be54dc..fd2c052 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,13 @@ mod git_helpers; mod exec_helpers; fn get_cli_input<'a>() -> ArgMatches<'a> { + let version_str = format!( + "{} {}", + env!("CARGO_PKG_VERSION"), + env!("LATEST_COMMIT"), + ); let mut base_app = App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) + .version(version_str.as_str()) .author(env!("CARGO_PKG_AUTHORS")) .about(env!("CARGO_PKG_DESCRIPTION")); From 63428ad729ecb58b783ab5a51649daa9f8cf1238 Mon Sep 17 00:00:00 2001 From: nikita-skobov Date: Sat, 17 Oct 2020 19:58:59 -0500 Subject: [PATCH 2/4] moves version bump to run before build this way the build will include the version bump, and then we just push it at the end if everything ran correctly, otherwise the version bump only exists on a tmp branch --- .github/workflows/build_and_test.yml | 32 ++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 471a6d7..0b1ba20 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -9,9 +9,30 @@ on: required: true jobs: + version_bump: + runs-on: ubuntu-latest + steps: + - name: checkout_code + uses: actions/checkout@v2.3.2 + - name: git_config + run: git config --local user.name "github_CI" && git config --local user.email "github_CI" + - name: version_bump + # when running this pipeline make sure the inputs.name is just the version without the v (eg: 1.1.1) + run: sed -i '5s/.*/version = "${{ github.event.inputs.name }}"/' Cargo.toml + - name: version_check + uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b + with: + command: check + - name: temp_branch + continue-on-error: true + run: git checkout -b tmp_branch + - name: commit_and_push_new_docs + continue-on-error: true + run: git add Cargo.toml Cargo.lock && git commit -m "version bump ${{ github.event.inputs.name }}" && git push origin HEAD:tmp_branch build_test: # The type of runner that the job will run on runs-on: ${{ matrix.job.os }} + needs: version_bump strategy: fail-fast: false matrix: @@ -31,6 +52,8 @@ jobs: uses: mig4/setup-bats@v1.0.1 - name: checkout_code uses: actions/checkout@v2.3.2 + with: + ref: tmp_branch - name: install_git_filter_repo run: bash .github/install-git-filter-repo.sh ${{ matrix.job.os }} - name: build_executable @@ -165,6 +188,8 @@ jobs: path: ./target/release - name: lstest run: ls -l ./target/release/mgt + - name: merge_from_tmp + run: git checkout tmp_branch && git checkout - && git merge --squash tmp_branch - name: make_mgt_executable run: chmod +x ./target/release/mgt - name: helptest @@ -173,13 +198,6 @@ jobs: run: ./doc/gen_docs.sh - name: git_config run: git config --local user.name "github_CI" && git config --local user.email "github_CI" - - name: version_bump - # when running this pipeline make sure the inputs.name is just the version without the v (eg: 1.1.1) - run: sed -i '5s/.*/version = "${{ github.event.inputs.name }}"/' Cargo.toml - - name: version_check - uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b - with: - command: check - name: commit_and_push_new_docs continue-on-error: true run: git add doc/ Cargo.toml Cargo.lock && git commit -m "updates docs for ${{ github.event.inputs.name }}" && git push From 9607183413845468e7c1ed56e741b3600de580db Mon Sep 17 00:00:00 2001 From: nikita-skobov Date: Sat, 17 Oct 2020 20:18:14 -0500 Subject: [PATCH 3/4] github action fixes --- .github/workflows/build_and_test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 0b1ba20..1bea5fb 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -5,7 +5,7 @@ on: inputs: name: description: 'release tag version' - default: 'vX.Y.Z' + default: 'X.Y.Z' required: true jobs: @@ -189,7 +189,7 @@ jobs: - name: lstest run: ls -l ./target/release/mgt - name: merge_from_tmp - run: git checkout tmp_branch && git checkout - && git merge --squash tmp_branch + run: git fetch origin tmp_branch && git checkout tmp_branch && git checkout - && git merge --ff-only tmp_branch - name: make_mgt_executable run: chmod +x ./target/release/mgt - name: helptest @@ -200,4 +200,4 @@ jobs: run: git config --local user.name "github_CI" && git config --local user.email "github_CI" - name: commit_and_push_new_docs continue-on-error: true - run: git add doc/ Cargo.toml Cargo.lock && git commit -m "updates docs for ${{ github.event.inputs.name }}" && git push + run: git add doc/ && git commit -m "updates docs for ${{ github.event.inputs.name }}" && git push From 98a87413158bd3fe544a51bd3e7407b4bc073d42 Mon Sep 17 00:00:00 2001 From: nikita-skobov Date: Sat, 17 Oct 2020 20:36:17 -0500 Subject: [PATCH 4/4] cleanup tmp branch after update --- .github/workflows/build_and_test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 1bea5fb..ddfcdad 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -201,3 +201,6 @@ jobs: - name: commit_and_push_new_docs continue-on-error: true run: git add doc/ && git commit -m "updates docs for ${{ github.event.inputs.name }}" && git push + - name: cleanup_tmp_branch + run: git push origin --delete tmp_branch + continue-on-error: true