Git Checkout from different branches in jenkins - gitlab

I am trying to take the chekouts from a different branches,
stage ("git-checkout"){
steps {
checkout([$class: 'GitSCM',
branches: [[name: '**']],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'Gitlb_creds', url: 'https://gitlab.com']]])
}
I have passed a string parameter as branch in the job configuration where I have multiple branches in git lab repo but it is taking checkouts from only one single branch even after the changing the value.
But I want to trigger the pipeline as per my choice branch either from dev, master or any feature from the git lab repo.
Is there a possible way?

If you want the your BRANCH parameter to fill in during checkout, you have to specify like this:
stage ("git-checkout"){
steps {
checkout([$class: 'GitSCM',
branches: [[name: ${BRANCH}]],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'Gitlb_creds', url: 'https://gitlab.com']]])
}

Related

Gitlab checkout.groovy fails after upgrade to 12.1.17 from 12.0.1

In Jenkins i had an analysis job. The job used to checkout and build the merge request sent to the target branch. However, after upgrading the gitlab version from 12.0.1 to 12.1.17 i am unable to checkout source branch.
Below is the groovy script i was using.
#!/usr/bin/env groovy
def call() {
if (env.gitlabMergeRequestId) {
sh "echo '${env.gitlabMergeRequestId}'"
sh "echo 'Merge request detected. Merging...'"
def credentialsId = scm.userRemoteConfigs[0].credentialsId
checkout ([
$class: 'GitSCM',
branches: [[name: "${env.gitlabSourceNamespace}/${env.gitlabSourceBranch}"]],
extensions: [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout'],
[
$class: 'PreBuildMerge',
options: [
fastForwardMode: 'NO_FF',
mergeRemote: env.gitlabTargetNamespace,
mergeTarget: env.gitlabTargetBranch
]
]
],
userRemoteConfigs: [
[
credentialsId: credentialsId,
name: env.gitlabTargetNamespace,
url: env.gitlabTargetRepoHttpURL
],
[
credentialsId: credentialsId,
name: env.gitlabSourceNamespace,
url: env.gitlabSourceRepoHttpURL
]
]
])
} else {
sh "echo 'No merge request detected. Checking out current branch'"
checkout ([
$class: 'GitSCM',
branches: scm.branches,
extensions: [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout']
],
userRemoteConfigs: scm.userRemoteConfigs
])
}
}
I was able to solve it by adding in the branches
branches: [[name: "refs/heads/${env.gitlabSourceBranch}"]]

How to set conditions in the parallel build to proceed to next stage if one step is success

I am creating a declarative pipeline in Jenkins. There are 6 stages in it.
First Stage: Scenario Upload
Second Stage: Pull code from Git
Third Stage: Maven Build
Fourth Stage: Its a parallel stage. First step will launch mobile emulator and second step will check device connected or not.
Fifth Stage: I want to start this stage when the second step BUILD SUCCESS else stop the job
Sixth Stage: Send email
I am stuck with point 5 (Fifth Stage). Please help
pipeline {
agent any
stages {
stage("Scenario Upload") {
steps {
script {
def inputFile = input message: 'Upload file', parameters: [file(name: 'CyclosAppStatus.xlsx')]
new hudson.FilePath(new File("$workspace/Cucumber_BDD master/Result/CyclosAppStatus.xlsx")).copyFrom(inputFile)
inputFile.delete()
}
}
}
stage('Git Pull Code') {
steps {
git credentialsId: '708a126a-66bb-4eb5-8826-55cedf6497c3', url: 'https://github.com/divakar-ragupathy/Mobile_Automation_BDD.git'
}
}
stage('Maven Clean Build') {
steps {
bat label: '', script: '''Echo Maven Clean Build...
cd %WORKSPACE%\\ADB_Devices
mvn clean compile'''
}
}
stage('Building Android Setup') {
steps {
parallel(
Invoke_Emulator: {
bat label: '', script: '''Echo Invoking Emulator...
#echo off
set emulName=%Emulator_Name%
echo %emulName%
for /f "tokens=1 delims=:" %%e in ("%emulName%") do (
%ANDROID_AVD_PATH%emulator -avd "%%e" -no-boot-anim -no-snapshot-save -no-snapshot-load
)
endlocal'''
},
Checking_Device: {
bat label: '', script: '''Echo Checking Connected Device...
cd %WORKSPACE%\\ADB_Devices
mvn exec:java -Dexec.mainClass=com.expleo.adbListner.CheckConnectedAdbDevices -Dlog4j.configuration=file:///%WORKSPACE%\\ADB_Devices\\src\\log4j.properties -Dexec.args="%Emulator_Name%"'''
}
)
}
}
}
}
If you declare a variable without the "def" keyword it is global. You can use that to store the condition in the previous stages. In the 5th stage you can use a when block to check this condition.

Calling Jenkins git plugin from a shared library class

I have a long standing declarative pipeline infrastructure
I would like to start putting repeated code into shared libraries
The problem I am facing is calling the git plugin from a shared library function/class. I'm a bit lost as my experience is really only with Jenkins declarative stuff, not the Groovy/Java specifics.
Here is a snippet of the Jenkinsfile, (before using shared library):
pipeline {
agent any
stages {
stage('Prep Workspace') {
steps {
script {
if ((env.BRANCH_NAME == 'staging') || (env.BRANCH_NAME == 'production')) {
BRANCH=env.BRANCH_NAME
} else {
BRANCH='master'
}
}
echo "||------ Get ProjectOne Dependency ------||"
dir('deps/ProjectOne') {
git branch: "${BRANCH}",
changelog: false,
credentialsId: 'jenkinsgit',
poll: false,
url: 'git#github.com:myprivateorg/ProjectOne.git'
}
echo "||------ Get ProjectTwo Dependency ------||"
dir('deps/ProjectTwo') {
git branch: "${BRANCH}",
changelog: false,
credentialsId: 'jenkinsgit',
poll: false,
url: 'git#github.com:myprivateorg/ProjectTwo.git'
}
}
}
}
}
Note the repeated calls to pull down project files from git repos. The goal here, is to move the repeated code to a shared function call.
I've read the following portion in the manual, on how to use git in shared library:
https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-steps
Using the example in the documentation I've created the shared library file
In src/org/test/gitHelper.groovy:
package org.test;
def checkOutFrom(String repo, String branch='master') {
echo "||------ CLONING $repo ------||"
git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git#github.com:myprivateorg/$repo.git"
}
return this
Then in the Jenkinsfile:
#Library('jenkins-shared-library') _
pipeline {
agent any
stages {
stage('Prep Workspace') {
steps {
script {
if ((env.BRANCH_NAME == 'staging') || (env.BRANCH_NAME == 'production')) {
BRANCH=env.BRANCH_NAME
} else {
BRANCH='master'
}
def g = new org.test.gitHelper()
g.checkOutFrom('ProjectOne')
g.checkOutFrom('ProjectTwo')
}
}
}
}
}
This loads the class and calls the function fine, but fails when it hits git itself:
groovy.lang.MissingPropertyException: No such property: git for class: java.lang.String
I used g.getClass() to confirm it's of type class org.test.gitHelper and NOT java.lang.String so I'm not sure where it's getting that type from.
Please note I have also tried this way:
vars/pullRepo.groovy
def call(String repo, String branch) {
echo "||------ CLONING $repo ------||"
dir("deps/$repo") {
git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git#github.com:myprivateorg/$repo.git"
}
}
Jenkinsfile:
pullRepo('ProjectOne', 'master')
I get the exact same error: groovy.lang.MissingPropertyException: No such property: git for class: java.lang.String
For me, it works to pass the Jenkins context to the shared library like so:
Jenkinsfile:
pullRepo(this, repo, branch)
vars/pullRepo.groovy:
def call(def context, String repo, String branch) {
echo "||------ CLONING $repo ------||"
dir("deps/$repo") {
context.git branch: branch, changelog: false, credentialsId: 'jenkinsgit', poll: false, url: "git#github.com:myprivateorg/$repo.git"
}
}
Note that I'm passing the Jenkins context into the context variable, and calling git as a method of the context. You should also be able to do this by passing the context up to your class.

Clone of 'git#******/common-ui-layout.git' into submodule path 'common-ui-layout' failed

Looking help on Jenkins, i have written jenkinsfile where in one stage i am running sh git submodule update --init --recursive command my job is getting failed with Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) error. but i can see on log common-ui-layout directory is present with full access, as per my analysis i found that while hitting the submodule command its not getting authenticated or its not finding the common-ui-layout folder. Im posting my jenkinsfile here, Please provide the fix of this issue.
pipeline {
agent {
label 'agent.com'
}
stages {stage("submodule clone"){
steps
{
checkout(
[
$class: 'GitSCM',
branches: [
[
name: 'master'
]
],
doGenerateSubmoduleConfigurations: false,
extensions: [
[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]
],
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: '<***ID****>',
url: 'https://*****gitlab.com/****/common-ui-layout.git'
]
]
]
)
}
}
stage("fetch data"){
steps {
git branch: 'patch-1',
credentialsId: '<***ID****>',
url: 'https://****.gitlab.com/*****/****.git'
sh "pwd"
sh "ls -lat"
}
}
stage ("Installing pre-req"){
steps{
sh '''
yarn install;
yarn global add #angular/cli
'''
}
}
stage('Build app') {
steps {
sh "yarn install";
sh "pwd";
sh 'git submodule update --init --recursive';
//sh "git submodule update --recursive –remote";
sh "yarn run ng build";
println "BUILD NUMBER = $BUILD_NUMBER"
println "Build Success.."
}
}
}
}
Please refer the error snipt here
The above error got fixed after adding the rsa key. after that i got another error when i hit the git submodule update --init --recursive command in pipeline and return the with below error-
Cloning into 'common-ui-layout'...
fatal: could not read Username for 'https://xxx.xxx.com': No such device or address
please refer the snippet here
Please suggest me where i am missing?

How can I use the Jenkins Copy Artifacts Plugin from within the pipelines (jenkinsfile)?

I am trying to find an example of using the Jenkins Copy Artifacts Plugin from within Jenkins pipelines (workflows).
Can anyone point to a sample Groovy code that is using it?
With a declarative Jenkinsfile, you can use following pipeline:
pipeline {
agent any
stages {
stage ('push artifact') {
steps {
sh 'mkdir archive'
sh 'echo test > archive/test.txt'
zip zipFile: 'test.zip', archive: false, dir: 'archive'
archiveArtifacts artifacts: 'test.zip', fingerprint: true
}
}
stage('pull artifact') {
steps {
copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: env.JOB_NAME, selector: specific(env.BUILD_NUMBER)
unzip zipFile: 'test.zip', dir: './archive_new'
sh 'cat archive_new/test.txt'
}
}
}
}
Before version 1.39 of the CopyArtifact, you must replace second stage with following (thanks #Yeroc) :
stage('pull artifact') {
steps {
step([ $class: 'CopyArtifact',
filter: 'test.zip',
fingerprintArtifacts: true,
projectName: '${JOB_NAME}',
selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}']
])
unzip zipFile: 'test.zip', dir: './archive_new'
sh 'cat archive_new/test.txt'
}
}
With CopyArtifact, I use '${JOB_NAME}' as project name which is the current running project.
Default selector used by CopyArtifact use last successful project build number, never current one (because it's not yet successful, or not). With SpecificBuildSelector you can choose '${BUILD_NUMBER}' which contains current running project build number.
This pipeline works with parallel stages and can manage huge files (I'm using a 300Mb file, it not works with stash/unstash)
This pipeline works perfectly with my Jenkins 2.74, provided you have all needed plugins
If you are using agents in your controller and you want to copy artifacts between each other you can use stash/unstash, for example:
stage 'build'
node{
git 'https://github.com/cloudbees/todo-api.git'
stash includes: 'pom.xml', name: 'pom'
}
stage name: 'test', concurrency: 3
node {
unstash 'pom'
sh 'cat pom.xml'
}
You can see this example in this link:
https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow
If builds are not running in the same pipeline you can use direct CopyArtifact plugin, here is example: https://www.cloudbees.com/blog/copying-artifacts-between-builds-jenkins-workflow and example code:
node {
// setup env..
// copy the deployment unit from another Job...
step ([$class: 'CopyArtifact',
projectName: 'webapp_build',
filter: 'target/orders.war']);
// deploy 'target/orders.war' to an app host
}
name = "/" + "${env.JOB_NAME}"
def archiveName = 'relNum'
try {
step($class: 'hudson.plugins.copyartifact.CopyArtifact', projectName: name, filter: archiveName)
} catch (none) {
echo 'No artifact to copy from ' + name + ' with name relNum'
writeFile file: archiveName, text: '3'
}
def content = readFile(archiveName).trim()
echo 'value archived: ' + content
try that using copy artifact plugin

Resources