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

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
}

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:

How can i find username/details of people who newly pushed code to github? I am using jenkins pipeline to build and deploy

I have created job in jenkins to trigger a build whenever changes happen in github. But i want to know how to get the details of the user who made the changes to configure email notification.
Could anyone help me with the solution please?
You can use git show command to fetch the author ID and email using below command. Assign this to a variable and use it anywhere in the pipeline.
To Fetch Author ID: git show -s --pretty=%an
To Fetch Author email: git show -s --pretty=%ae
Pipeline script can be written as like below.
pipeline {
agent any
options {
timestamps()
}
stages {
stage('Test Stage') {
steps {
checkout changelog: true, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/Sample_branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'TestCredentials', url: '']]]
script {
Author_ID=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
Author_Name=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
}
echo "${Author_ID} and ${Author_Name}"
}
}
}
}

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

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