Where to put credentials in gradle - gradle

Where can I put my credentials in gradle without making them public?
I would like to know some best pratics, and to understand well the difference between inserting them in gradle.properties or as environment variable.
It's important to me know how call them in build script, too.
Thanks in advance

You can put credentials in local.properties if you want to have it locally. You can retrieve them like this:
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def local = properties.getProperty('localVar')
and having local variable in local.properties:
localVar=helloWorld
The best practice would be putting it in an environment variable, in this way, you are able to run your gradle scripts anywhere even in a CI environment.
In build.gradle you can get environment variables by using
System.getenv("ENVIRONMENT_VARIABLE")

Related

Use variables from settings.xml but override with environment variables in Maven

I'm trying to use a property in my Maven POM file to specify the URL / username / password to a private repository. I would like to get the value for this property from a settings.xml file (from either the global, user or project settings file) but use the value from a specific environment variable to override any of that if it is set.
I've searched a bunch and I've found some references to using build profiles which doesn't seem to be quite what I need, but then again I'm not very fluent in Maven so I might just be missing something there. I feel like an idiot because I would think this is a pretty common use case.
In general I'm not too keen on defining secrets (passwords or API keys) in environment variables but for now we need to do that to avoid having to change the whole CI pipeline.

Pass credentials to karate .feature file from Jenkins server [duplicate]

I'd like to incorporate GitLab CI into my Karate testing. I'd like to loop through my tests with different user names and passwords to ensure our API endpoints are responding correctly to different users.
With that in mind, I'd like to be able to store the usernames and passwords as secure environment variables in GitLab (rather than in the karate-config as plain text) and have Karate pull them as needed from either the karate-config or the feature files.
Looking through the docs and StackOverflow questions, I haven't seen an example where it's being done.
Updating with new information
In regards to Peter's comment below, which is what I need I am trying to set it up as follows:
set client id in karate-config:
var client_id = java.lang.System.getenv('client_id');
in the actual config object:
clientId: client_id
In my feature file tried to access it:
* def client_id = clientId
It still comes through as null, unfortunately.
You can read environment variables in karate using karate.properties,
eg,
karate.properties['java.home']
If this helps you to read the environment variables that you are keeping securely on your gitlab, then you can use it in your karate-config for authentication.
But your config and environment variable will look cumbersome if you are having too many users.
If you want to run a few features with multiple users, I would suggest you look into this post,
Can we loop feature files and execute using multiple login users in karate
EDIT:
Using java interop as suggested by peter:
var systemPath = java.lang.System.getenv('PATH');
to see which are all variables are actually exposed try,
var evars= java.lang.System.getenv();
karate.log(evars);
and see the list of all environment variables.

How to get value of Configuration parameter in teamcity using Gradle?

I have been trying to get the value of configuration parameter since yesterday but no luck. On the documentation I can call it using this teamcity["<property name>"] but it wont work.
Say I have a configuration parameter called "isPaid". I want to get the "isPaid" param value in my gradle. How can I do that? Shouldn't that be
System.getProperty("teamcity[isPaid]")
Tried using both getProperty and getEnv but still did not work. Any ideas? Thanks
Configuration parameters (no prefix) are not passed into the build and are only meant to share settings within a build configuration.
https://www.jetbrains.com/help/teamcity/configuring-build-parameters.html

Getting settings object from the buildscript

I am trying to access the settings object from the root project's build script.
The reason is I want to define a list in the settings.gradle file which will be a list of subprojects, kind of:
settings.gradle
projectNames = ['prjA', 'prjB']
Would like to do something like:
build.gradle (root project)
projectNames = settings.projectNames
// Use projectName in tasks
And then access it in build.gradle for various tasks, such as resolving those names into URLs to git-clone them. However I can't seem to find a way to declare some arbitrary groovy object which is visible between these two scripts. Note I may potentially like that list to be related but not equal to the project names. I guess the question sums up to sharing POGOs between those two files and accessing the settings object.
I'm pretty new to Gradle.
There isn't a way to get to the settings object from a build script. However, both scripts share a gradle object, which you could use to set an extra property in the settings script (e.g. gradle.ext.foo = "bar"), and read it in the build script (e.g. println gradle.foo).
If you need access to the Settings instance from your build.gradle file after the
settings have been loaded and evaluated, you can register a lifecycle closure or listener.
A great place to start is the method Gradle#settingsEvaluated(Closure)
that provides the Settings object as a closure parameter.

Access to TeamCity build comments

A continuation on the answer to this question:
Is it possible to add a free text note to a team city build?
In TeamCity custom build dialog there is a field for "Build comments".
Is there a way to access this from within the build?
Either as a system property or environment variable?
As far as I know, the Build Comments are not exposed or accessible from your build script, however you can create custom build parameters that are accessible. You can create system properties or environment variables, either of which can be accessed in your build script. See the TeamCity docs on custom parameters for full details.
Maybe this question is outdated, but I'll answer just in case somebody else is interested.
Comments can be retrieved using TeamCity REST API:
http://teamcity.codebetter.com/guestAuth/app/rest/changes?locator=build:id:216886&fields=change(id,version,href,username,date,webUrl,comment)
If you need it from C# project, you may consider using FluentTc library:
IBuild build = new RemoteTc()
.Connect(_ => _.ToHost("teamcity.codebetter.com").AsGuest())
.GetLastBuild(
having => having.Id(216886),
with => with.IncludeChanges(and => and.IncludeComment()));
One way of achieving it is as follows:
http://[host]:[port]/httpAuth/app/rest/builds/id:<internal build id>
This will return xml with build Parameters, comments will be one of child nodes.

Resources