Jenkins "Git Parameter" plugin with "useRepository" option - jenkins

I'm using "Git Parameter" plugin to allow users to pick branch\tag for configured repositories.
This plugin has "useRepository" option to allow linking with the configured repos in the "Pipeline script from SCM" option:
This assumes that i need to preconfigure some repos in the Jenkins pipeline (in the Jenkins UI) to be able to use "Git Parameter" plugin in the pipelines.
But i want to dynamically predefine list of repos from the pipeline code itself, without any configuration in the "Pipeline script from SCM" section.
Unfortunately this doesn't work.
I'm tried to add "checkout" block with "GitSCM" class before calling "gitParameter" but with no success.
Code example:
def app_components = [
BACKEND : ["NAME": "backend", "GIT": "ssh://git#xxx.local/_git/backend"],
FRONTEND : ["NAME": "frontend", "GIT": "ssh://git#xxx.local/_git/fronend"]
]
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
dynamicParameters = []
app_components.eachWithIndex { name, components, index ->
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'XXX', url: components.GIT]]
])
dynamicParameters << gitParameter(name: 'BRANCH_' + name, defaultValue: 'develop', quickFilterEnabled: true, type: 'PT_BRANCH_TAG', listSize: '10', useRepository: components.GIT)
}
def userInput = input(
id: 'userInput', message: 'Test message:?',
parameters: dynamicParameters
)
println(userInput);
}
}
}
}
Git parameters text fields are always empty:
Could you advice some solution in this scenario?

Play around with adding
gitParameter branchFilter: 'origin/(.*)',

Related

How to get the latest git commit time of another repository from a Jenkins declarative pipeline

I have a declarative pipeline. In this pipeline I want to add a verify step to check the last code commit time which lies in another repository. Based on the commit time, I do have to proceed with the next steps.
I am facing issues in fetching the commit time. I am very new to git commands, so unable to resolve the issue.
Points - My Jenkins file is in one repository, and I need the commit details of another repository. I need specifically the commit time (Epoc)
My code looks like this -
#Library('xx-libs') _
pipeline {
agent any
options {
timeout(time: 2, unit: 'HOURS')
}
parameters {
xxxxx
}
environment {
xxxxxx
}
stages {
stage('Checkout source code') {
steps {
script {
checkout ([$class: 'GitSCM',
branches: [[name: '*/'+branch]],
extensions: [
[$class: 'CloneOption', noTags: true, timeout: 20],
[$class: 'RelativeTargetDirectory', relativeTargetDir: '/tmp/core/'],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'code/System/Infra/Version/version.cpp']]],
[$class: 'CleanBeforeCheckout']
],
userRemoteConfigs: [[
credentialsId: 'azure-bearer-auto-updated',
url: 'https://xx.xx.com/xxx/xxx/_git/core'
]]
])
git_time = sh script: "git show -s --format=%ct", returnStdout: true
echo "$git_time"
}
}
}
} // stages
}

how to execute jenkins pipeline from config file

I have a generic multibranch project that I use on about 100 different git repos. The jenkins jobs are automatically generated and the only difference is the git repo.
Since they all build in the same way and I don't want to copy the same jenkins groovy file in all repos, I use "Build configuration -> mode -> by default jenkinsfile".
It breaks the rule to put the jenkinsfile in SCM as I would prefer to do. To minimize the impact, I would like that groovy file to only checkout the "real" jenkinsfile and execute it.
I use that script:
pipeline {
agent {label 'docker'}
stages {
stage('jenkinsfile checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'gipc_synthesis']],
submoduleCfg: [],
userRemoteConfigs: [[url: 'ssh://git#camtl1bitmirror.gad.local:7999/mtlstash/mvt/gipc_synthesis.git']]]
)
}
}
stage('Load user Jenkinsfile') {
//agent any
steps {
load 'gipc_synthesis/jenkins/synthesis_job.groovy'
}
}
}
}
The problem I have with that is I can't have another pipeline in the groovy file I am loading. I don't want to define only functions but really the whole pipeline in that file. Any solution to that problem? I am also interested in solution that would completely avoid the whole issue.
Thank you.
You can have a shared library with your pipeline inside:
// my-shared.git: vars/build.groovy
def call(String pathToGit) // and maybe additional params
{
pipeline {
agent { ... }
stages {
stage('jenkinsfile checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'gipc_synthesis']],
submoduleCfg: [],
userRemoteConfigs: [[url: pathToGit]]]
)
}
}
}
}
}
and use it in your Jenkinsfile e.g. like this:
#!groovy
#Library('my-shared') _
def pathToGit = 'ssh://git#camtl1bitmirror.gad.local:7999/mtlstash/mvt/gipc_synthesis.git'
build(pathToGit)

Trigger pipeline after a push on bitbucket server

I'm creating this new Job based on pipeline on jenkins. I want my jenkinsfile to be on bitbucket reposiotry : Let's say my config file is on bitbucket.org/config.git
The job mission is to clean install a project bitbucket.org/myProject.git
How can I configure the pipeline so it will trigger if any push is made in bitbucket.org/myProject.git and following the steps defined in bitbucket.org/config.git?
I do not want to create multi-branch pipeline and I do not want my jenkins file to be on the same repository than my project to compile.
My current config is:
pipeline {
agent any
parameters {
string(defaultValue: '', description: 'URL', name: 'GIT_URL')
string(defaultValue: '', description: 'Credential', name: 'CREDENTIAL_ID')
}
stages {
stage ('Initialize') {
steps {
git branch: 'develop', credentialsId: "${params.CREDENTIAL_ID}", url: "${params.GIT_URL}"
}
}
stage ('Build') {
steps {
sh 'mvn clean install '
echo 'build'
}
}
}
You can use shared Libraries in Jenkins. you would still need a Jenkinsfile in your code, but that would not contain any logic. It would simply refer the shared library and pass any params like git repo path.
For more information on shared libraries please refer this link https://jenkins.io/doc/book/pipeline/shared-libraries/.
For triggering the build, you can define a trigger in your pipeline. Example :
triggers {
pollSCM('H/5 * * * *')
}
or use webhooks if you don't want to poll.
Actually, i managed to make it work.
In my jenkins pipeline, i activated "Build when a change is pushed to BitBucket".
node {
checkout([$class: 'GitSCM',
branches: [[name: 'feature/test-b']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption', disableSubmodules: false,
parentCredentials: false, recursiveSubmodules: true, reference: '',
trackingSubmodules: false]], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'admin',
url: 'http://localhost:7990/scm/bout/boutique-a.git']]])
}
When a change is made in boutique-a in the branch 'feature/test-b' my job is triggered which is cool.
Now i have this other issue, how can i trigger when change are made in feature/*
It looks like i cannot access to env.BRANCH_NAME when im not in a multibranch pipeline

dynamically replace branch name in jenkinsfile

I am new to Jenkins, and stuck at replacing branch name in Jenkinsfile from Jenkins.
I want to write a common Jenkinsfile, and in that I want to pick branch name from Jenkins. Below is the snippet from my Jenkinsfile
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
Whatever the branch name I select from Jenkins, it should replace the same in Jenkinsfile and do the build.
I have gone through the answers
parametrized build
Somewhat not able to do the same.
Can anyone refer me to the doc/link which I can go through and implement the same?
My Changes:
checkout([$class: 'GitSCM', branches: [[name: 'params.Branch_Select']],
enter image description here
I have fixed the above question. UNCHECK the box.
In your Jenkinsfile, add a parameters section which will accept BRANCH_NAME as parameter
parameters {
string(defaultValue: "master", description: 'enter the branch name to use', name: 'BRANCH_NAME')
}
and use that variable in your checkout step as follows:
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]]])
Note: You have to run the build once to see the 'Build with parameters' option.
Sample pipeline with just a checkout stage:
pipeline{
agent any
parameters {
string(defaultValue: "develop", description: 'enter the branch name to use', name: 'BRANCH_NAME')
}
stages{
stage('Checkout'){
steps{
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]], gitTool: 'jgit', userRemoteConfigs: [[url: "https://github.com/githubtraining/hellogitworld.git"]]])
//checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_NAME}"]],url:'https://github.com/brianmyers/hello'])
}
}
}
}

WaitForQualityGate exit with error on Jenkins slave

I'm running a Jenkins instance with 1 master / 1 slave, connected to a Sonarqube instance. I'm using pipeline jobs, and it works fine except on jenkins slave where the "WaitForQualityGate" stage doesn't work. It works fine on master.
My job exit with this error:
Java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.
Even if the stage "withSonarQubeEnv" is called before.
My configuration is:
Jenkins job have a "pipeline script" checking-out my source code + the pipeline script
Shared libraries are loaded implicitly
withSonarQubeEnv is called during "testCover" and waitForQualityGate is called during "testQualityGate"
Jenkins job pipeline script:
node(){
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'jenkinsfile'], [$class: 'IgnoreNotifyCommit'], [$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credential', url: 'https://pipeline.git']]]
checkout changelog: true, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'src'], [$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credential', url: 'https://sourcecode.git']]]
load 'jenkinsfile/Jenkinsfile'
}()
Shared library (testCover):
echo "Testing the coverage of the application"
withSonarQubeEnv('sonarqube') {
withCredentials([string(credentialsId: 'sonarqube-token', variable: 'sonarqube_token')]) {
def scannerCmd = "sonar-scanner -e";
scannerCmd += " -Dhttps.proxyHost=proxy.com";
scannerCmd += " -Dhttps.proxyPort=8888";
scannerCmd += " -Dhttp.proxyHost=proxy.com";
scannerCmd += " -Dhttp.proxyPort=8888";
scannerCmd += " -Dsonar.login=${env.sonarqube_token}";
scannerCmd += " -Dsonar.password=";
sh "${scannerCmd}"
}
}
Shared library (testQualityGate):
sleep 10
timeout(time: 3, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
Pipeline job:
{->
node {
dir('src'){
stage ('Init') {
initLib('node7')
}
stage ('Build app') {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',
credentialsId: 'npm-server',
usernameVariable: 'REG',
passwordVariable: 'TOKEN'
]]) {
sh "echo '\n//${env.REG}/:_authToken=${env.TOKEN}' >> .npmrc"
buildApp()
}
}
stage ('Test / Lint') {
testApp()
}
stage ('Cover / static analysis') {
testCover()
}
stage ('Quality Gate') {
testQualityGate()
}
stage ('Flowdock notification') {
notifyFlowdock()
}
}
}
}
EDIT: After investigating deeper, i found out that the problem might come from the 2 calls to node statement (1 in my pipeline script (job), 1 in my pipeline file). Unfortunately, that's not solving my issue =/
EDIT 2: I checked that the line "Working dir:" and "ANALYSIS SUCCESSFULL" are present in my build log, as Sonar plugin use those lines to find out the URL + the PATH for the ".sonar" folder (where the task-report.txt is), and they are ! So basically, it's working on Master node, but not on Slave, even if they both have the same output =/
I'm answering my own question to let you know that there was an actual issue that has been spotted in sonar plugin for jenkins. Here is the patch https://repox.sonarsource.com/sonarsource-public-builds/org/jenkins-ci/plugins/sonar/2.6.1.1212/sonar-2.6.1.1212.hpi
Thanks all the people in the google group (https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/sonarqube/z_K_wz_8Vw8) for the help provided : )

Resources