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

Automatic argument retrieval with fallback mechanism #1265

Open
GeertvanHorrik opened this issue Oct 10, 2016 · 5 comments
Open

Automatic argument retrieval with fallback mechanism #1265

GeertvanHorrik opened this issue Oct 10, 2016 · 5 comments

Comments

@GeertvanHorrik
Copy link
Contributor

For my own purposes I have created this convenience method. It checks for argument values in the following order:

  1. Specified via command line
  2. Specified via build server (build server variable)
  3. Specified via environment variable

This way we need less explicit variable definitions because most of them are automatically retrieved from the build server (in most cases, people are running cake from a build server and pass in variables already defined in the build server).

It's currently written for continua ci only, but this can easily be extended to other build servers as well and renamed to GetVariable

public string GetContinuaCIVariable(string variableName, string defaultValue)
{
    var argumentValue = Argument(variableName, "non-existing");
    if (argumentValue != "non-existing")
    {
        Information("Variable '{0}' is specified via an argument", variableName);

        return argumentValue;
    }

    if (ContinuaCI.IsRunningOnContinuaCI)
    {
        var buildServerVariables = ContinuaCI.Environment.Variable;
        if (buildServerVariables.ContainsKey(variableName))
        {
            Information("Variable '{0}' is specified via Continua CI", variableName);

            return buildServerVariables[variableName];
        }
    }

    if (HasEnvironmentVariable(variableName))
    {
        Information("Variable '{0}' is specified via an environment variable", variableName);

        return EnvironmentVariable(variableName);
    }

    Information("Variable '{0}' is not specified, returning default value", variableName);    

    return defaultValue;
}

What do you think, is it worth PR-ing?

@RehanSaeed
Copy link
Contributor

RehanSaeed commented Oct 13, 2016

I do something like this:

var mygetApiKey = HasArgument("MyGetApiKey") ? 
    Argument<string>("MyGetApiKey") : 
    EnvironmentVariable("MyGetApiKey");

@RehanSaeed
Copy link
Contributor

RehanSaeed commented Jun 22, 2017

I've since evolved my script to do this:

public T GetArgument<T>(string name, T defaultValue = default(T), Func<bool> canGetArgument = null, Func<T> getArgument = null)
{
    return HasArgument(name) ? Argument<T>(name) :
        (canGetArgument != null && getArgument != null && canGetArgument()) ? getArgument() :
        EnvironmentVariable(name) != null ? (T)Convert.ChangeType(EnvironmentVariable(name), typeof(T)) :
        defaultValue;
}

// With no default
var releaseNotes = GetArgument<string>("ReleaseNotes");
// With a default value
var configuration = GetArgument<string>("Configuration", "Release");
// With an override from a CI server
var buildNumber = GetArgument<int>("BuildNumber", 1, () => GoCD.IsRunningOnGoCD, () => GoCD.Environment.Pipeline.Counter);

@augustoproiete
Copy link
Member

Related Cake addins that try to help in this area:

@alekseiko97
Copy link

Hello everyone! Is this still a relevant issue as I see that the last change has been made almost 2 years ago?

@vasu2304
Copy link

Hi Everyone! Is this issue still open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants