Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect whether vcpkg is running in a CI environment #713

Merged
merged 7 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/bundles.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,77 @@ Run-Vcpkg search zlib --vcpkg-root=$bundle `
--x-packages-root=$packagesRoot `
--x-scripts-root=$env:VCPKG_ROOT/scripts
Throw-IfFailed

# Test CI environment detection
$detected_ci_key = 'detected_ci_environment'
$known_ci_vars = (
"env:VCPKG_NO_CI",
"env:TF_BUILD",
"env:APPVEYOR",
"env:CODEBUILD_BUILD_ID",
"env:CIRCLECI",
"env:GITHUB_ACTIONS",
"env:GITLAB_CI",
"env:HEROKU_TEST_RUN_ID",
"env:JENKINS_URL",
"env:TRAVIS",
"env:CI",
"env:BUILD_ID"
)

foreach ($var in $known_ci_vars) {
if (Test-Path $var) {
Remove-Item $var
}
}

$env:VCPKG_NO_CI="1"
$env:TF_BUILD="1"
$env:CI="1"

$a = Run-Vcpkg z-print-config `
--vcpkg-root=$bundle `
--overlay-triplets=$env:VCPKG_ROOT/triplets `
--x-scripts-root=$env:VCPKG_ROOT/scripts
$a
Throw-IfFailed
$a = $($a | ConvertFrom-JSON -AsHashtable)
if ($a[$detected_ci_key] -ne 'VCPKG_NO_CI') {
throw "Expected VCPKG_NO_CI as detected CI environment but found: $($a[$detected_ci_key])"
}

Remove-Item env:VCPKG_NO_CI
$a = Run-Vcpkg z-print-config `
--vcpkg-root=$bundle `
--overlay-triplets=$env:VCPKG_ROOT/triplets `
--x-scripts-root=$env:VCPKG_ROOT/scripts
$a
Throw-IfFailed
$a = $($a | ConvertFrom-JSON -AsHashtable)
if ($a[$detected_ci_key] -ne 'Azure_Pipelines') {
throw "Expected Azure_Pipelines as detected CI environment but found: $($a[$detected_ci_key])"
}

Remove-Item env:TF_BUILD
$a = Run-Vcpkg z-print-config `
--vcpkg-root=$bundle `
--overlay-triplets=$env:VCPKG_ROOT/triplets `
--x-scripts-root=$env:VCPKG_ROOT/scripts
$a
Throw-IfFailed
$a = $($a | ConvertFrom-JSON -AsHashtable)
if ($a[$detected_ci_key] -ne 'Generic') {
throw "Expected Generic as detected CI environment but found: $($a[$detected_ci_key])"
}

Remove-Item env:CI
$a = Run-Vcpkg z-print-config `
--vcpkg-root=$bundle `
--overlay-triplets=$env:VCPKG_ROOT/triplets `
--x-scripts-root=$env:VCPKG_ROOT/scripts
$a
Throw-IfFailed
$a = $($a | ConvertFrom-JSON -AsHashtable)
if ($a[$detected_ci_key] -ne $null) {
throw "Expected no CI environment but found $($a[$detected_ci_key]))"
}
1 change: 1 addition & 0 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace vcpkg
CommandArgs,
CommandContext,
CommandName,
DetectedCiEnvironment,
Error,
InstallPlan_1,
ListFile,
Expand Down
4 changes: 4 additions & 0 deletions include/vcpkg/vcpkgcmdarguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ namespace vcpkg
f.manifests = manifests_enabled();
return f;
}
const Optional<StringLiteral>& detected_ci_environment() const { return m_detected_ci_environment; }

bool output_json() const { return json.value_or(false); }

Expand All @@ -248,6 +249,7 @@ namespace vcpkg

void debug_print_feature_flags() const;
void track_feature_flag_metrics() const;
void track_environment_metrics() const;

Optional<std::string> asset_sources_template() const;

Expand All @@ -263,5 +265,7 @@ namespace vcpkg
std::map<std::string, std::vector<std::string>, std::less<>> command_options;

std::vector<std::string> forwardable_arguments;

Optional<StringLiteral> m_detected_ci_environment;
};
}
1 change: 1 addition & 0 deletions src/vcpkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ int main(const int argc, const char* const* const argv)

args.debug_print_feature_flags();
args.track_feature_flag_metrics();
args.track_environment_metrics();

if (Debug::g_debugging)
{
Expand Down
6 changes: 5 additions & 1 deletion src/vcpkg/commands.zprintconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace vcpkg::Commands::Z_PrintConfig
}
}

void PrintConfigCommand::perform_and_exit(const VcpkgCmdArguments&,
void PrintConfigCommand::perform_and_exit(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet default_triplet,
Triplet host_triplet) const
Expand All @@ -38,6 +38,10 @@ namespace vcpkg::Commands::Z_PrintConfig
obj.insert("host_triplet", host_triplet.canonical_name());
obj.insert("vcpkg_root", paths.root.native());
obj.insert("tools", paths.tools.native());
if (auto ci_env = args.detected_ci_environment().get())
{
obj.insert("detected_ci_environment", *ci_env);
}
if (auto i = paths.maybe_installed().get())
{
obj.insert("installed", i->root().native());
Expand Down
1 change: 1 addition & 0 deletions src/vcpkg/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace vcpkg
{StringMetric::CommandArgs, "command_args", "0000000011111111aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffff"},
{StringMetric::CommandContext, "command_context", "artifact"},
{StringMetric::CommandName, "command_name", "z-preregister-telemetry"},
{StringMetric::DetectedCiEnvironment, "detected_ci_environment", "Generic"},
{StringMetric::Error, "error", "build failed"},
{StringMetric::InstallPlan_1,
"installplan_1",
Expand Down
69 changes: 69 additions & 0 deletions src/vcpkg/vcpkgcmdarguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,56 @@
#include <vcpkg/metrics.h>
#include <vcpkg/vcpkgcmdarguments.h>

namespace
{
using namespace vcpkg;

constexpr std::pair<StringLiteral, StringLiteral> KNOWN_CI_VARIABLES[]{
// Opt-out from CI detection
{"VCPKG_NO_CI", "VCPKG_NO_CI"},

// Azure Pipelines
// https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables#system-variables
{"TF_BUILD", "Azure_Pipelines"},

// AppVeyor
// https://www.appveyor.com/docs/environment-variables/
{"APPVEYOR", "AppVeyor"},

// AWS Code Build
// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
{"CODEBUILD_BUILD_ID", "AWS_CodeBuild"},

// CircleCI
// https://circleci.com/docs/env-vars#built-in-environment-variables
{"CIRCLECI", "Circle_CI"},

// GitHub Actions
// https://docs.github.com/en/actions/learn-github-actions/
{"GITHUB_ACTIONS", "GitHub_Actions"},

// GitLab
// https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
{"GITLAB_CI", "GitLab_CI"},

// Heroku
// https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables
{"HEROKU_TEST_RUN_ID", "Heroku_CI"},

// Jenkins
// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
{"JENKINS_URL", "Jenkins_CI"},

// Travis CI
// https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
{"TRAVIS", "Travis_CI"},

// Generic CI environment variables
{"CI", "Generic"},
{"BUILD_ID", "Generic"},
};
}

namespace vcpkg
{
const std::string* ParsedArguments::read_setting(StringLiteral setting) const noexcept
Expand Down Expand Up @@ -717,6 +767,16 @@ namespace vcpkg
from_env(get_env, ASSET_SOURCES_ENV, asset_sources_template_env);
from_env(get_env, REGISTRIES_CACHE_DIR_ENV, registries_cache_dir);

// detect whether we are running in a CI environment
for (auto&& ci_env_var : KNOWN_CI_VARIABLES)
{
if (get_env(ci_env_var.first).has_value())
{
m_detected_ci_environment = ci_env_var.second;
break;
}
}

{
const auto vcpkg_disable_lock = get_env(IGNORE_LOCK_FAILURES_ENV);
if (vcpkg_disable_lock.has_value() && !ignore_lock_failures.has_value())
Expand Down Expand Up @@ -888,6 +948,15 @@ namespace vcpkg
}
}

void VcpkgCmdArguments::track_environment_metrics() const
{
if (auto ci_env = m_detected_ci_environment.get())
{
Debug::println("Detected CI environment: ", *ci_env);
LockGuardPtr<Metrics>(g_metrics)->track_string_property(StringMetric::DetectedCiEnvironment, *ci_env);
}
}

Optional<std::string> VcpkgCmdArguments::asset_sources_template() const
{
std::string asset_sources_template = asset_sources_template_env.value_or("");
Expand Down