Jenkins to Bamboo Migration & Running Groovy - jenkins

I'm fairly new to Jenkins and a total newbie to Bamboo. I have a Jenkins Pipeline and I'm trying to create an equivalent in Bamboo (I believe it's called a Plan).
I've got some groovy code that I want to run in my Bamboo plan.
I'll simplify the code below for brevity and clarity.
Assume this file is called me_myEvent.groovy and is stored at https://github.com/myuser/repo1
def processEvent( Map args ) {
String strArg1 = args.myArg1;
String strArg2 = args.myArg2;
// etc...
}
My Jenkins pipeline has a global pipeline library (myGitLibraryFromGlobal) linking to https://github.com/myuser/repo1 and my pipeline is:
#Library('myGitLibraryFromGlobal#master') abc
pipeline {
agent any
stages {
stage('First Stage') {
steps {
script {
def myObj = new com.mysite.me_myEvent();
def returnVal = myObj.processEvent(arg1: 'foo', arg2: 'bar');
}
}
})
}
}
I've got the GitHub repo saved in Bamboo as a global linked repository called abc123.
Can I achieve the same thing in Bamboo using the script task? What would this look like in Bamboo?

The short answer is NO, as Atlassian Bamboo doesn't support the DSL Groovy or Scripted Groovy pipeline. Also, please keep in mind that when you run the Jenkins Groovy pipeline, then Jenkins adds its own environment to the script execution, it is not just running "bare" groove script (i.e. without exposed Jenkins commands and variables).
If you need to run a "bare" groovy script supporting the idea of Configuration as Code, one solution is to create a Bamboo Java/YAML spec. Then you need to create ScriptTask.
// this is called when the plan and stages are created
new Job("JobName","JobKey").tasks(new VcsCheckoutTask(), // to download the groovy script
new ScriptTask().inlineBody("groovy me_myEvent.groovy").interpreterBinSh())
Note: your Bamboo build agent should have a pre-installed groovy.
Another solution is to use the Bamboo plugin Groovy Tasks for Bamboo.

Related

Block Jenkins pipeline build when another particular job is running

I am converting a Jenkins Freestyle build to a pure pipeline based job.
Current configuration uses https://plugins.jenkins.io/build-blocker-plugin/ as shown in the image.
How can I use it in a Declarative pipeline?
pipeline {
agent { label 'docker-u20' }
//Don't run until the "test-job" running
blockon("test-job")
stage{}
}
I did try to look into various jenkins docs but haven't found yet.

Jenkins Share common environment variables in a groovy method

I am building a declarative JenkinsFile, I have some common variables that I want to be shared across some Jenkins projects and jobs.
So I created a jenkins shared library, but for some reason i can't get my Jenkins file to to read the common environment variables from common groovy.
pipeline {
environment {
commonEnv()
Email_Notification_Enabled="true"
Slack_Notification_Enabled="false"
}
}
and in my groovy i had:
def call() {
a = "abc"
b = "abc"
}
It throws error that commonEnv() is not allowed in environments.
What is the possible way to achieve such behaviour.
You could write a Groovy method that sets the common environment variables. Please refer this Stack Overflow question to know how to do this. Include that method in Jenkins pipeline shared library.
Now call this Groovy method in declarative pipeline of each of your jobs. Remember that in a declarative pipeline, you may use Groovy only inside the script step. So, your pipeline would look something like:
pipeline {
stages {
stage("First stage") {
steps {
script {
// call to Groovy method that sets environment variables
}
// other steps
}
}
// other stages
}
}
Hope, it helps.
Since you need to have environment variables that are shared across all Jenkins projects and jobs, you should set them up on Jenkins instance level rather than on a Jenkins project or job level.
So, instead of doing it in a Jenkinsfile (which will do it at Jenkins job level), I will do it in Manage Jenkins > Configure System > Global properties > Environment Variables:
The environment variables could then be read in the pipeline script from Jenkins Global Variable env:
echo "This is my Jenkins global environment variable ${env.MY_ENV_VAR_NAME}"

How to set build name in Jenkins Job DSL?

According to the documentation in https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.wrapper.MavenWrapperContext.buildName
Following code should update build name in Build History in Jenkins jobs:
// define the build name based on the build number and an environment variable
job('example') {
wrappers {
buildName('#${BUILD_NUMBER} on ${ENV,var="BRANCH"}')
}
}
Unfortunately, it is not doing it.
Is there any way to change build name from Jenkins Job DSL script?
I know I can change it from Jenkins Pipeline Script but it is not needed for me in this particular job. All I use in the job is steps.
steps {
shell("docker cp ...")
shell("git clone ...")
...
}
I would like to emphasise I am looking for a native Jenkins Job DSL solution and not a Jenkins Pipeline Script one or any other hacky way like manipulation of environment variables.
I have managed to solve my issue today.
The script did not work because it requires build-name-setter plugin installed in Jenkins. After I have installed it works perfectly.
Unfortunately, by default jobdsl processor does not inform about missing plugins. The parameter enabling that is described here https://issues.jenkins-ci.org/browse/JENKINS-37417
Here's a minimal pipeline changing the build's display name and description. IMHO this is pretty straight forward.
pipeline {
agent any
environment {
VERSION = "1.2.3-SNAPSHOT"
}
stages {
stage("set build name") {
steps {
script {
currentBuild.displayName = "v${env.VERSION}"
currentBuild.description = "#${BUILD_NUMBER} (v${env.VERSION})"
}
}
}
}
}
It results in the following representation in Jenkins' UI:
setBuildName("your_build_name") in a groovyPostBuild step may do the trick as well.
Needs Groovy Postbuild Plugin.

Running a groovy script through jenkinsfile which runs on a remote linux box

I have a abc.groovy script which takes an argument. In my local I run it as
$ groovy abc.groovy <argumentValue>
I have stored this abc.groovy in a remote linux box under path "/home/path/to a directory/" and I have a jenkins pipeline job with a Jenkinsfile. How can I call abc.groovy from the JenkinsFile.
You can use GroovyShell to evaluate your script.
GroovyShell shell = new GroovyShell()
def execute = shell.parse(new File('/path/to/abc.groovy'))
execute.method()
You'll want to use the load step in your Jenkinsfile like this:
def pipeline {
agent 'slave'
stages {
stage ('Load Groovy Script') {
steps {
load 'path/to/abc.groovy'
}
}
}
(This example uses the declarative pipeline syntax, but is easily ported to scripted)
Note: you can't pass parameters to the groovy script in the load step, however this isn't hard to work around.

Jenkins Pipeline: Are agents required to utilize Jenkinsfile?

I am investigating the use of Jenkins Pipeline (specifically using Jenkinsfile). The context of my implementation is that I'm deploying a Jenkins instance using Chef. Part of this deployment may include some seed jobs, which will pull job configurations from source control (Jenkinsfile), to automate creation of our build jobs via Chef.
I've investigated the Jenkins documentation for both Pipeline as well as Jenkinsfile, and it seems to me that in order to use Jenkins Pipeline agents are required to be configured and set up in addition to Jenkins Master.
Am I understanding this correctly? Must Jenkins agents exist in order to use Jenkins Pipeline's Jenkinsfile? This specific line in the Jenkinsfile documentation leads me to believe this to be true:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
The Declarative Pipeline example above contains the minimum necessary
structure to implement a continuous delivery pipeline. The agent
directive, which is required, instructs Jenkins to allocate an
executor and workspace for the Pipeline.
Thanks in advance for any Jenkins guidance!
The 'agent' part of the pipeline is required however this does not mean that you are required to have an external agent in addition to your master. If all you have is the master this pipeline will execute on the master. If you have additional agents available the pipeline would execute on whichever agent happens to be available when you run the pipeline.
If you go into
Manage Jenkins -> Manage Nodes and Clouds, you can see 'Master' itself is treated as one of the Default Nodes. With declarative format agent anyindicates any available agent which (including 'Master' as well from node configuration see below).
In case if you configure any New node, this can then be treated as New Agent in the pipeline agent any can be replaced by agent 'Node_Name'
You may can refer this LINK which give hint on Agent, Node and Slave briefly.

Resources