diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b89418108f..928b01309e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -251,11 +251,11 @@ jobs: cp -r src/agent/script/win64/libfuzzer-coverage src/deployment/tools/win64/libfuzzer-coverage echo $GITHUB_RUN_ID | tee src/deployment/.build.id echo $GITHUB_SHA | tee src/deployment/.sha - cp CURRENT_VERSION src/deployment/VERSION + ./src/ci/get-version.sh > src/deployment/VERSION (cd src/deployment ; zip -r onefuzz-deployment-$(cat VERSION).zip . ) cp src/deployment/onefuzz-deployment*zip release-artifacts cp -r artifacts/sdk release-artifacts - cp -r artifacts/windows-cli/onefuzz.exe release-artifacts/onefuzz-cli-$(cat CURRENT_VERSION).exe + cp -r artifacts/windows-cli/onefuzz.exe release-artifacts/onefuzz-cli-$(./src/ci/get-version.sh).exe - uses: actions/upload-artifact@v2.1.4 with: name: release-artifacts diff --git a/src/agent/onefuzz-agent/build.rs b/src/agent/onefuzz-agent/build.rs index 89396d856d..65a15f8d7a 100644 --- a/src/agent/onefuzz-agent/build.rs +++ b/src/agent/onefuzz-agent/build.rs @@ -1,3 +1,4 @@ +use std::env; use std::error::Error; use std::fs::File; use std::io::prelude::*; @@ -6,7 +7,7 @@ use std::process::Command; fn run_cmd(args: &[&str]) -> Result> { let cmd = Command::new(args[0]).args(&args[1..]).output()?; if cmd.status.success() { - Ok(String::from_utf8_lossy(&cmd.stdout).to_string()) + Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string()) } else { Err(From::from("failed")) } @@ -16,21 +17,40 @@ fn read_file(filename: &str) -> Result> { let mut file = File::open(filename)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; + contents = contents.trim().to_string(); Ok(contents) } -fn main() -> Result<(), Box> { +fn print_version(include_sha: bool) -> Result<(), Box> { + let mut version = read_file("../../../CURRENT_VERSION")?; let sha = run_cmd(&["git", "rev-parse", "HEAD"])?; - let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() { - "-local_changes" - } else { - "" - }; - println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes); - let version = read_file("../../../CURRENT_VERSION")?; + if include_sha { + version.push('-'); + version.push_str(&sha); + + // if we're a non-release build, check to see if git has + // unstaged changes + if run_cmd(&["git", "diff", "--quiet"]).is_err() { + version.push('.'); + version.push_str("localchanges"); + } + } + + println!("cargo:rustc-env=GIT_VERSION={}", sha); println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version); Ok(()) } + +fn main() -> Result<(), Box> { + // If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise + // modify it to indicate local build + let include_sha = if let Ok(val) = env::var("GITHUB_REF") { + !val.starts_with("refs/tags/") + } else { + true + }; + print_version(include_sha) +} diff --git a/src/agent/onefuzz-supervisor/build.rs b/src/agent/onefuzz-supervisor/build.rs index 89396d856d..65a15f8d7a 100644 --- a/src/agent/onefuzz-supervisor/build.rs +++ b/src/agent/onefuzz-supervisor/build.rs @@ -1,3 +1,4 @@ +use std::env; use std::error::Error; use std::fs::File; use std::io::prelude::*; @@ -6,7 +7,7 @@ use std::process::Command; fn run_cmd(args: &[&str]) -> Result> { let cmd = Command::new(args[0]).args(&args[1..]).output()?; if cmd.status.success() { - Ok(String::from_utf8_lossy(&cmd.stdout).to_string()) + Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string()) } else { Err(From::from("failed")) } @@ -16,21 +17,40 @@ fn read_file(filename: &str) -> Result> { let mut file = File::open(filename)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; + contents = contents.trim().to_string(); Ok(contents) } -fn main() -> Result<(), Box> { +fn print_version(include_sha: bool) -> Result<(), Box> { + let mut version = read_file("../../../CURRENT_VERSION")?; let sha = run_cmd(&["git", "rev-parse", "HEAD"])?; - let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() { - "-local_changes" - } else { - "" - }; - println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes); - let version = read_file("../../../CURRENT_VERSION")?; + if include_sha { + version.push('-'); + version.push_str(&sha); + + // if we're a non-release build, check to see if git has + // unstaged changes + if run_cmd(&["git", "diff", "--quiet"]).is_err() { + version.push('.'); + version.push_str("localchanges"); + } + } + + println!("cargo:rustc-env=GIT_VERSION={}", sha); println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version); Ok(()) } + +fn main() -> Result<(), Box> { + // If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise + // modify it to indicate local build + let include_sha = if let Ok(val) = env::var("GITHUB_REF") { + !val.starts_with("refs/tags/") + } else { + true + }; + print_version(include_sha) +} diff --git a/src/api-service/dev-deploy.sh b/src/api-service/dev-deploy.sh index 2f24655e34..5075161264 100755 --- a/src/api-service/dev-deploy.sh +++ b/src/api-service/dev-deploy.sh @@ -14,12 +14,32 @@ fi set -ex TARGET=${1} -cd ${APP_DIR} -(cd ../pytypes && python setup.py sdist bdist_wheel && cp dist/*.whl ../api-service/__app__) -cd __app__ +pushd ${APP_DIR} +VERSION=$(../ci/get-version.sh) +../ci/set-versions.sh + +# clean up any previously built onefuzztypes packages +rm __app__/onefuzztypes*.whl + +# build a local copy of onefuzztypes +rm -rf local-pytypes +cp -r ../pytypes local-pytypes +pushd local-pytypes +rm -f dist/* +python setup.py sdist bdist_wheel +cp dist/*.whl ../__app__ +popd +rm -r local-pytypes + +# deploy a the instance with the locally built onefuzztypes +pushd __app__ uuidgen > onefuzzlib/build.id -sed -i s,onefuzztypes==0.0.0,./onefuzztypes-0.0.0-py3-none-any.whl, requirements.txt +TYPELIB=$(ls onefuzztypes*.whl) +sed -i s,.*onefuzztypes.*,./${TYPELIB}, requirements.txt func azure functionapp publish ${TARGET} --python -sed -i s,./onefuzztypes-0.0.0-py3-none-any.whl,onefuzztypes==0.0.0, requirements.txt -rm 'onefuzztypes-0.0.0-py3-none-any.whl' -cat onefuzzlib/build.id +sed -i s,./onefuzztypes.*,onefuzztypes==0.0.0, requirements.txt +rm onefuzztypes*.whl +popd + +../ci/unset-versions.sh +cat __app__/onefuzzlib/build.id \ No newline at end of file diff --git a/src/ci/get-version.sh b/src/ci/get-version.sh new file mode 100755 index 0000000000..68692d5abf --- /dev/null +++ b/src/ci/get-version.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +set -e + +SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) +BASE_VERSION=$(cat ${SCRIPT_DIR}/../../CURRENT_VERSION) +BRANCH=$(git rev-parse --abbrev-ref HEAD) +GIT_HASH=$(git rev-parse HEAD) + +if [ "${GITHUB_REF}" != "" ]; then + TAG_VERSION=${GITHUB_REF#refs/tags/} + + # this isn't a tag + if [ ${TAG_VERSION} == ${GITHUB_REF} ]; then + echo ${BASE_VERSION}-${GIT_HASH} + else + echo ${BASE_VERSION} + fi +else + if $(git diff --quiet); then + echo ${BASE_VERSION}-${GIT_HASH} + else + echo ${BASE_VERSION}-${GIT_HASH}.localchanges + fi +fi diff --git a/src/ci/set-versions.sh b/src/ci/set-versions.sh index c112f3b7b8..022c1f61f5 100755 --- a/src/ci/set-versions.sh +++ b/src/ci/set-versions.sh @@ -5,7 +5,10 @@ set -ex -VERSION=$(cat CURRENT_VERSION) +SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) +GET_VERSION=${SCRIPT_DIR}/get-version.sh +VERSION=$(${GET_VERSION}) +cd ${SCRIPT_DIR}/../../ SET_VERSIONS="src/pytypes/onefuzztypes/__version__.py src/api-service/__app__/onefuzzlib/__version__.py src/cli/onefuzz/__version__.py" SET_REQS="src/cli/requirements.txt src/api-service/__app__/requirements.txt" diff --git a/src/ci/unset-versions.sh b/src/ci/unset-versions.sh new file mode 100755 index 0000000000..a6ec0b0473 --- /dev/null +++ b/src/ci/unset-versions.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +set -ex + +SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) +GET_VERSION=${SCRIPT_DIR}/get-version.sh +VERSION=$(${GET_VERSION}) +cd ${SCRIPT_DIR}/../../ + +SET_VERSIONS="src/pytypes/onefuzztypes/__version__.py src/api-service/__app__/onefuzzlib/__version__.py src/cli/onefuzz/__version__.py" +SET_REQS="src/cli/requirements.txt src/api-service/__app__/requirements.txt" + +sed -i 's/__version__ = .*/__version__ = "0.0.0"/' ${SET_VERSIONS} +sed -i "s/onefuzztypes==.*/onefuzztypes==0.0.0/" ${SET_REQS} diff --git a/src/proxy-manager/build.rs b/src/proxy-manager/build.rs index 260d93fd96..443d0f2340 100644 --- a/src/proxy-manager/build.rs +++ b/src/proxy-manager/build.rs @@ -1,3 +1,4 @@ +use std::env; use std::error::Error; use std::fs::File; use std::io::prelude::*; @@ -6,7 +7,7 @@ use std::process::Command; fn run_cmd(args: &[&str]) -> Result> { let cmd = Command::new(args[0]).args(&args[1..]).output()?; if cmd.status.success() { - Ok(String::from_utf8_lossy(&cmd.stdout).to_string()) + Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string()) } else { Err(From::from("failed")) } @@ -16,21 +17,40 @@ fn read_file(filename: &str) -> Result> { let mut file = File::open(filename)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; + contents = contents.trim().to_string(); Ok(contents) } -fn main() -> Result<(), Box> { +fn print_version(include_sha: bool) -> Result<(), Box> { + let mut version = read_file("../../CURRENT_VERSION")?; let sha = run_cmd(&["git", "rev-parse", "HEAD"])?; - let with_changes = if run_cmd(&["git", "diff", "--quiet"]).is_err() { - "-local_changes" - } else { - "" - }; - println!("cargo:rustc-env=GIT_VERSION={}{}", sha, with_changes); - let version = read_file("../../CURRENT_VERSION")?; + if include_sha { + version.push('-'); + version.push_str(&sha); + + // if we're a non-release build, check to see if git has + // unstaged changes + if run_cmd(&["git", "diff", "--quiet"]).is_err() { + version.push('.'); + version.push_str("localchanges"); + } + } + + println!("cargo:rustc-env=GIT_VERSION={}", sha); println!("cargo:rustc-env=ONEFUZZ_VERSION={}", version); Ok(()) } + +fn main() -> Result<(), Box> { + // If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise + // modify it to indicate local build + let include_sha = if let Ok(val) = env::var("GITHUB_REF") { + !val.starts_with("refs/tags/") + } else { + true + }; + print_version(include_sha) +}