Mapping data in Jenkins - jenkins

I'm trying to pass data to a pipeline with a map which the point is just to provide app name and environment and I will get all the other data I need to my main pipeline
I'm passing data to my deployment pipeline like so:
env.project_info= [
git_path:"",
git: "",
config_branch: "",
team_name: ""
]
Which works but I want to change it.
I'm trying to change it into a map in which I will pass only the app name and env and it will pass me all the data above.
Does someone have any tips on how to do it?

Related

Creating Dev and Prod deployments using CDK

New to AWS CDK, so please bear with me. I am trying to create multi-deployment CDK, wherein the Dev should be deployed to Account A and prod to Acocunt B.
I have created 2 stacks with the respective account numbers and so on.
mktd_dev_stack = Mktv2Stack(app, "Mktv2Stack-dev",
env=cdk.Environment(account='#####', region='us-east-1'),
stack_name = "myStack-Dev",
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
)
the Prod is similar with the Prod account and different name. When I run them I plan on doing
cdk deploy Mktv2Stack-dev
and simiar for prod.
I am using the cdk 2.xx on Python
What my question is, does this setup give me an ability to pass a parameter, say details which is a dict object of names and criteria for resources that will be set up ? Or is there a way for me to pass parameter/dict from app.py to my program_name.py so that I can look up values from the dict and set them to resources accordingly.
Regards Tanmay
TL;DR Use a single stack and pass in the stg/prod as an env var to app.py.
Pass config down from app.py > Stacks > Constructs as Python Parameters (constructor args). Avoid using CDK Parameters* for config, says AWS's CDK Application Best Practices.
Practically speaking, you pass the account or alias as a environment variable, which app.py reads to perform the metadata lookups and set the stack props. Here's a node-flavoured version of this pattern:
AWS_ACCOUNT=123456789012 npx cdk deploy '*' -a 'node ./bin/app' --profile test-account"
Why not 2 stacks in app.py, one for PROD and one for STAGING?
A 2-stack approach can certainly work. The downsides are that you rarely want to deploy both environments at the same time (outside a CI/CD context). And cross-account permissions are trickier to handle safely if mixed in a single cdk deploy.
Customising Constructs for different environments
Within your code, use a dict, class or whatever to return the configuration you want based on an account or region input. Finally, pass the variables to the constructs. Here's an example of code that uses account, region and isProduction props to customise a s3 bucket:
const queriesBucket = new s3.Bucket(this, 'QueriesBucket', {
bucketName: `${props.appName.toLowerCase()}-queries-${props.env.account}-${
props.env.region
}`,
removalPolicy: props.isProduction
? cdk.RemovalPolicy.RETAIN
: cdk.RemovalPolicy.DESTROY,
versioned: props.isProduction,
lifecycleRules: [
{
id: 'metadata-rule',
prefix: 'metadata',
noncurrentVersionExpiration: props.isProduction
? cdk.Duration.days(30)
: cdk.Duration.days(14),
},
],
});
* "Parameter" has different meaning in Python and CDK. Passing variables between constructs in code using Python Parameters (=method arguments) is a best practice. In CDK-speak a Parameter has the special meaning of a variable value passed to CloudFormation at deploy time. These are not CDK best practice.

Issue with Generic Webhook Trigger plugin in Jenkins

I am trying to use Generic Webhook Trigger plugin in Jenkins to trigger build in case any PR is raised on my GitHub repo. For starters I defined a variable "current_status" mapping it to "action" field within the json payload to be received from GitHub. While the build is getting triggered on raising a PR but the value for current_status is coming as null. The content-type for my GitHub webhook is "application/json"
The GitHub payload generated against the PR event has action field in it :
"action": "opened",
But when I try to print this variable using println "${params.current_status}" in my pipeline, the value is printed as null.
Also when I try to execute a step based on the value of the variable using
when {
expression { return params.current_status == "opened" }
}
the stage is skipped even though the value as per the action in the GitHub payload is "opened"
For debugging the issue when I selected the option to print the contributed variables in the job log I could see value of the current_status value as opened
But when I refer this variable in my pipeline its value comes out to be null somehow.
As a workaround made my pipeline parmeterized, using the same name for the variable as the one defined in the Generic Webhook Trigger Plugin section (current_status) and then referred to it within my Jenkinsfile and it worked.(the value for the variable reflected the value received in the json payload from GitHub).
Finally I found the solution. For anyone who might be facing the same issue you can refer to the variable defined within the Generic Webhook Trigger plugin directly as a Groovy variable. In my case I tried to the use the variable current_status directly without referrring it via params and it worked as expected.
The other approach of defining the variable via parmeterized pipeline might be helpful wherein we would want to run the build manually.

parametrized jenkinsFile : file parameter

I want to configure a parametrized job in jenkins, who manipulate file:
parameters([
file(defaultValue: 'DEFAULT', name : 'tomcatCodesUrl' , description: 'URL of service where to find tomcat mapping json file'),
the issus is , this parameter only return the name of the file. how can I acces to this content?
Currently there is no easy way to do this. You can find discussion about this in JENKINS-27413
Yeah that parameter is as redundant as it can be. Might aswell just use string.
Anyway. You can get the files content with readFile:
def content = readFile encoding: 'utf-8', file: 'tomcatCodesUrl'
How to use file parameter in jenkins
This post might be helpful. The upshot is, when users upload a file, it will be saved into the root directory of the project's workspace. You can directly access the file using any programming language you like, given the file name. The file content is not returned to you as a parameter, but anyway, since you know its saved place (workspace dir) and file name, you are in control.

Jenkins Extended Choice Parameter - using the values

I'm new to Jenkins so this is probably an easy one. I have the Extended Choice Parameter plugin installed. I'm using the Multi Select parameter type to pick from a list of servers (SERVER1,SERVER2,SERVER3) I've set Source for Value, Default Value, and Value Description.
I save it, and it looks great. I can pick any or all servers for the build. Now for the big question.. how do I use these values in the build? Basically I have a step in the build that can take in a comma separated list that is called by a shell command:
d:\python\deploy.py?serverlist=$blah
What do I put in for $blah to use that list of servers?
Just to be clear, if I'm on command line I would do the following:
d:\python\deploy.py?serverlist=SERVER1,SERVER2,SERVER3
I'm sure it's something simple but I just cannot find it in the docs or an example.
We could get the servers list like this
d:\python\deploy.py?serverlist=$SERVERLIST
or this on Windows
d:\python\deploy.py?serverlist=%SERVERLIST%
To see the list of environment variables which we could you, try this URL (change localhost by your Jenkins URL, TEST by the job name, 10 by the build number)
https://localhost:8080/job/TEST/10/injectedEnvVars/
UPDATE to #sniperd's edition:
This URL will shows us the parameters list in the Job:
http://localhost:8080/job/TEST/59/parameters/

Cannot receive parameter from Script content to Predefined Parameter in Jenkins

I would like to pass hostname parameter which is declared at Script Content to Predefined Parameter in Trigger/calls on build other project where my child project will receive parameter from the parent project.My code looks like this in Script Content:
`machine_name="$(hostname)"`
So in order to pass my parameter to the child project I declared:
host_name=${machinename}
in Script Content .But when I check in my child project it display as ${machinename} which is not I want .Can someone tell me what am I missing or what step that I done wrong or is there any way to perform this ?
Try using ENv Inject plugin, all you need to do is below:
Your script should contain below step:
machine_name="$(hostname)" > inject.txt
Now use Inject environment variables build step and in
property file path give inject.txt
what's the use?
By this step, now you machine_name variable holds the hostname value throughout the job.
Next, in your Parameter in Trigger/calls on build other project
host_name=${machine_name}
And use the same variable in child job.
I think there is no need for multiple assignments above, but still you can try this.
'_' was missing. host_name=$machine_name.
you cannot set or pass the parameter value from the script o/p. either make it as env variable using groovy or set in to property file & read.

Resources