Use a variable in 'git url' jenkins piplines - jenkins

So when using a Jenkins pipeline I need to checkout a second git repository in a new folder:
dir('platform') {
println("\n\n\n=== === ===> gitBranch ${gitbranch} ");
dir('platform') {
deleteDir()
}
git url: 'git#gitlab.platform-automation.git', credentialsId: '1234567890', branch: 'feature/Packer-server-image-builds' // clones repo to subdir
}
When I try to use a variable to set the branch the command fails:
git url: 'git#gitlab.platform-automation.git', credentialsId: '1234567890', branch: '${gitbranch}'
What do I need to do to get this working?

use " instead of ' to use variables: "${gitbranch}"

Related

Jenkins webhook build pull requests from forks

We are using bitbucket-push-and-pull-request plugin in order to build our project. The pipeline is set to checkout the source repo when webhook is triggered and everything works fine, when PR's coming from the origin repo.
When the pull requests comes from forked repo then the problem appear where cannot find the commit because is still in the fork.
Any idea how we can solve it?
Here is a example of the jenkinsfile:
pipeline {
triggers {
bitBucketTrigger credentialsId: 'GIT_CREDS',
triggers: [
[$class: 'BitBucketPPRPullRequestServerTriggerFilter',
actionFilter: [$class: 'BitBucketPPRPullRequestServerCreatedActionFilter',
allowedBranches: ''
]
],
[$class: 'BitBucketPPRPullRequestServerTriggerFilter',
actionFilter: [$class: 'BitBucketPPRPullRequestServerMergedActionFilter',
allowedBranches: ''
]
],
[$class: 'BitBucketPPRPullRequestServerTriggerFilter',
actionFilter: [$class: 'BitBucketPPRPullRequestServerSourceUpdatedActionFilter',
allowedBranches: ''
]
]
]
}
agent any
stages {
stage('Checkout Bitbucket repo') {
steps {
script {
git branch: 'env.CHANGE_BRANCH',
credentialsId: 'GIT_CREDS',
url: env.GIT_URL
}
}
}
stage('Start the build') {
steps {
script {
sh 'echo "BUILD"'
}
}
}
}
}
Here is the LOG:
stderr: fatal: ambiguous argument '0648334d4491907a45a840a7326c3b8f54180144^{commit}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
I was able to fix this by adding Refspec in Advanced tab.
The refspec is:

Jenkins file diffs with declarative pipeline and multibranch

I have a scenario where I manage two pipelines within the same repo, with two different Jenkinsfiles.
I have set up two Jenkins multibranch pipelines to handle the two different Jenkinsfiles, by path discovery and set up github webhooks to trigger a build when a PR on particular branches is created.
I have not found a way to get changes in files for a particular PR so I thought of leveraging git by doing git diff --name-status origin/master...HEAD but it fails, since Jenkins only checks out the target branch.
The logstash:
using credential github-user-token-uname-pwd
Fetching changes from the remote Git repository
Fetching without tags
> git rev-parse --is-inside-work-tree # timeout=10
> git config remote.origin.url https://github.com/myreponame # timeout=10
Fetching upstream changes from https://github.com/myreponame
> git --version # timeout=10
using GIT_ASKPASS to set credentials Github token in uname-pwd form used by jenkins to register and manage webhooks
> git fetch --no-tags --force --progress https://github.com/myreponame +refs/heads/BRANCH-X:refs/remotes/origin/BRANCH-X
Checking out Revision 440df9b08667458fa4ade4be57ecbf59a4 (BRANCH-X)
Commit message: "move post build where it belongs"
> git config core.sparsecheckout # timeout=10
> git checkout -f 440df9b08667458fa4ade4be57ecbf59a4
> git rev-list --no-walk ab28e843c0fc7807c4cbd2d6f612e5d76b # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $SECRET_ACCESS_KEY or $ACCESS_KEY_ID
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] sh
+ git diff --name-status origin/master...HEAD
and the error I get:
fatal: ambiguous argument 'origin/master...HEAD': unknown revision or path not in the working tree.
Is there a way to retrieve the files' changes of a PR within multibranch pipelines or get Jenkins to be able to discover the source branch of said PR?
You could try to fetch origin and then diff with FETCH_HEAD
{ git fetch origin master
git diff —name-status FETCH_HEAD...HEAD
}
So here is what I had to do.
The error I was facing was due to Jenkins default checkout behaviour with multibranch pipelines: it checks out and track a branch and that branch only, it does not fetch other branches.
I eventually added
options {
skipDefaultCheckout true
}
and a stage that checks out all the origin remote branches
stage('Checking out repo'){
steps {
script {
checkout(
[
$class: 'GitSCM',
branches: [[name: 'origin/FEATUREBRANCH*']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch'],
[$class: 'CleanBeforeCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'github-deploy-key',
url: 'git#github.com:myawesomerepo.git']]
]
)
}
}
}

use the same Jenkinsfile for several github repositories

I have 40-50 github repositories , each repo contain one maven job.
I want to create multibranch pipeline job for each repository.
can I use the same Jenkinsfile for all projects without add Jenkinsfile for each repository. (take it from another SCM repo) ?
I know that I can use shared library to create a full pipeline , but I prefer something cleaner.
To accomplish this, I would suggest to create a pipeline with two parameters and pass the values based on the repo to build. 1) GIT BRANCH - to build and deploy required branch
2) GIT URL - to provide the git URL to checkout the code.
Providing a reference template.
node('NODE NAME')
{
withEnv([REQUIRED ENV VARIBALES])
{ withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS ID', passwordVariable: 'PW', usernameVariable: 'USER']])
{ try
{ stage 'Build'
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: gitbranch]], doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'CREDENTIALS ID',
url: 'GIT URL']]]
****
MAVEN BUILD
****
stage 'Docker Image build & Push'
*****
DOCKER BUILD AND PUSH TO REPO
*****
}
catch (err) {
notify("Failed ${err}")
currentBuild.result = 'FAILURE'
}
stage 'Deploy to ENV'
*****
DEPLOYMENT TO REQUIRED ENV
*****
notify('Success -Deployed to Environment')
catch (err) {
notify("Failed ${err}")
currentBuild.result = 'FAILURE'
}
}
}
}
def notify(status)
{
****
NOTIFICATION FUCNTION
****
}
Link the Jenkinsfile in the pipeline job and provide the values- build with parameters, while building the Jenkins job.
Hope this helps.

How to select different git repos(main & forked) in groovy Jenkinsfile to run Jenkins2.0 job

I have a Jenkinsfile which is used by Jenkins2.0 to run on a GitHub repo when code is merged the job will checkout the code, run unit tests and build it.
I want to use the same Jenkinsfile on forked GitHub repo but only want to checkout the code and build the code.
import hudson.model.*;
stage('Checkout') {
node{
git branch: '<BRANCH_NAME>', credentialsId: '<JENKINS_CREDENTIAL_ID>', url: 'git#<URL_OF_REPOSITORY>'
}
}
stage('Test'){
node{
"run unit tests"
}
}
stage('Build'){
node{
"build project"
}
}
You can remove the test section and add your own forked git clone url
import hudson.model.*;
stage('Checkout') {
node{
git branch: '<BRANCH_NAME>', credentialsId: '<JENKINS_CREDENTIAL_ID>', url: 'git#<URL_OF_YOUR_FORKED_REPOSITORY>'
}
}
stage('Build'){
node{
"build project"
}
}

Jenkins Multibranch Pipeline Jenkinsfile detect Git repo that launched job

I have a multi branch pipeline configuration job working fine so far....
However every single repository has exactly the same jenkinsfile except for the git repo name.
a typical jenkinsfile looks like:
node('docker-slave') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'NEXUS', usernameVariable: 'NEXUS_USERNAME', passwordVariable: 'NEXUS_PASSWORD']]) {
git url: 'git#bitbucket.org:myco/myprob.git', branch: env.branch_name, credentialsId: '08df8ab41de0', variable: 'CREDENTIALS'
stage 'Test'
sh 'env > env.txt'
sh 'cat env.txt'
sh 'make verify'
}
}
What I'd like to do is detect which git repo triggered the build so I don't have to hardcode it in the jenkinsfile.
So what I'd like is to change the git line to something like (notice GIT_URL):
git url: env.GIT_URL, branch: env.branch_name, credentialsId: '08df8ab41de0', variable: 'CREDENTIALS'
This gets me closer to my eventual goal of storing my Jenkinsfile in a common location instead of having to copy it repo to repo and modify it repo to repo.
Any ideas?
Thanks
phil
it turns out that in the script the following code does exactly what I need:
checkout sum
The git url line isn't needed....
so in the end the code looks like:
node('docker-slave') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'NEXUS', usernameVariable: 'NEXUS_USERNAME', passwordVariable: 'NEXUS_PASSWORD']]) {
checkout scm
stage 'Test'
sh 'make verify'
}

Resources