Jenkinsfile load groovy file doesn't work as expected - jenkins

I have a Jenkinsfile with the following :
node('buildprod_iamsg'){
echo "\u001B[34mSending status to dynamodb pib-deployments ...\u001B[0m"
checkout scm
def dynamo = load "aws/dynamo.groovy"
dynamo.add_metadata(substring, stripped_env, "down", lz, slackChannelName, slackToken, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time)
echo "\u001B[34mJob status sent to dynamodb pib-deployments\u001B[0m"
}
When I execute this It seems to checkout scm but then doesn't print anything. after and exits out. Inside my dynamo.groovy I have :
public add_metadata(substring, stripped_env, statuss, lz, slackChannelName, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time){
echo "reached here"
//--projection-expression lz
def expression_attribute_values = readFile("expression_attribute_values.json").trim()
echo expression_attribute_values
parseralgo(expression_attribute_values)
}
heres the result of execution :
Running on build_iamsg in /home/jenkins/workspace/djin_aweb_sample
[Pipeline] {
[Pipeline] echo
Sending status to dynamodb pib-deployments ...
[Pipeline] checkout
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://githubnet/djin-productivity/Deptwo.0.git/ # timeout=10
Fetching upstream changes from https://githubnet/djin-productivity/Deptwo.0.git/
> git --version # timeout=10
> git fetch --tags --progress https://githubnet/djin-productivity/Deptwo.0.git/ +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/dbint^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/dbint^{commit} # timeout=10
Checking out Revision 7daa4d196eafe4698f941f01631f5ccb13947793 (refs/remotes/origin/dbint)
Commit message: "adding dynamo integration with groovy load"
> git config core.sparsecheckout # timeout=10
> git checkout -f 7daa4d196eafe4698f941f01631f5ccb13947793
[Pipeline] load
[Pipeline] { (aws/dynamo.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // timeout
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: UNSTABLE
doesn't print reached here or the statement after in my jenkinsfile which is Job status sent to dynamodb pib-deployments

By the looks of it I think you forgot to add the slackToken parameter in your groovy script.
public add_metadata(substring, stripped_env, statuss, lz, slackChannelName, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time){

Related

Jenkins pipeline git checkout access GIT_COMMIT when default checkout is skipped

Observations:
Jenkinsfile
pipeline {
agent { node 'master' }
stages {
stage('Hello') {
steps {
echo env.GIT_COMMIT
}
}
}
}
Output:
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
e5b4b2deed1c8db486cef5641ef14c61fd48f9ed
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Jenkinsfile
pipeline {
agent { node 'master' }
options {
skipDefaultCheckout(true)
}
stages {
stage('Hello') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: '/tmp/r']]])
echo env.GIT_COMMIT
}
}
}
}
Output:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/HelloWorld_master
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /var/jenkins_home/workspace/HelloWorld_master/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url /tmp/r # timeout=10
Fetching upstream changes from /tmp/r
> git --version # timeout=10
> git --version # 'git version 2.30.2'
> git fetch --tags --force --progress -- /tmp/r +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision e5b4b2deed1c8db486cef5641ef14c61fd48f9ed (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f e5b4b2deed1c8db486cef5641ef14c61fd48f9ed # timeout=10
Commit message: "init"
First time build. Skipping changelog.
[Pipeline] echo
null
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Question:
I am using jenkins version 2.349 and the project is of type Multi-branch pipeline.
As I read on the Jenkins git plugin's page, it is supposed to inject the environment variables but it seems it only does when the default checkout isn't skipped but not when we explicitly checkout. I could only find ways to access those SCM values (in my case GIT_COMMIT) by using a script { } block. Is there a way to get those variables without jumping into a script block?

Why Bitbucket souce code repository not getting cloned using Jenkins?

I am using latest Jenkins in my Linux box. I am trying to clone a Bitbucket repository and checkout master branch using Jenkins pipeline. What I am trying to achieve is, after getting my source code cloned, I can make use of it and proceed with build. Below is my code;
node {
stage('Build') {
git branch: 'master',
credentialsId: '12345678-1234-1234-1234-123456789123',
url: 'https://username#bitbucket.org/username/test.git'
}
stage('Test') {
sh """pwd"""
}
}
and below is my console output;
Started by user User
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] git
The recommended git tool is: NONE
using credential 12345678-1234-1234-1234-123456789123
> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test_pipeline/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://username#bitbucket.org/username/test.git # timeout=10
Fetching upstream changes from https://username#bitbucket.org/username/test.git
> git --version # timeout=10
> git --version # 'git version 2.32.0'
using GIT_ASKPASS to set credentials bitbucket_token_1
> git fetch --tags --force --progress -- https://username#bitbucket.org/username/test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision ae8839d74d91d4ae28ba37a4ec91fa2265058b75 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
Commit message: "Initial commit"
> git rev-list --no-walk ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ pwd
/var/lib/jenkins/workspace/test_pipeline
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
When I cd to /var/lib/jenkins/workspace/test_pipeline I can see nothing. Why is this happening?

Jenkins Pipeline failing but no errors?

I have the folowing Pipeline Script that seems to be failing at some point. The CredentialId is correct, the github URL is correct, the path to dotnet is correct, and I'm like 99% sure the syntax is correct because the output (below) is reading my script correctly. So, I'm just wondering if I'm doing something incorrect.
I've never used Jenkins or any CI/CD tools before so this is all new to me.
My hopeful goal is to be able to get Jenkins to build and run a .NET app every-time something is merged with master, but I'm just trying to get this to build successfully every hour, or just to get it to build successfully when I click Build Now in Jenkins itself.
(Also, if there is an easy way to modify my script to run with every master merge, instead of every hour, that would be super helpful!)
pipeline{
agent any
environment {
dotnet ='C:\\Program Files (x86)\\dotnet\\'
}
triggers {
pollSCM 'H * * * *'
}
stages{
stage('Checkout') {
steps {
git credentialsId: '0b02e39b-17f0-4628-ba3d-24146ec7a469', url: 'https://github.com/bellingboe/OktaKenkinsCI.git', branch: 'master'
}
}
stage('Restore packages'){
steps{
bat "dotnet restore C:\\Users\\Brenden\\Documents\\GitHub\\OktaKenkinsCI\\OktaKenkinsCI.csproj"
}
}
stage('Clean'){
steps{
bat "dotnet clean C:\\Users\\Brenden\\Documents\\GitHub\\OktaKenkinsCI\\OktaKenkinsCI.csproj"
}
}
stage('Build'){
steps{
bat "dotnet build C:\\Users\\Brenden\\Documents\\GitHub\\OktaKenkinsCI\\OktaKenkinsCI.csproj --configuration Releas'"
}
}
}
}
which gives me the following output:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\WINDOWS\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\workspace\JenkinsPipeline
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] git
The recommended git tool is: NONE
using credential 0b02e39b-17f0-4628-ba3d-24146ec7a469
> git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git.exe config remote.origin.url https://github.com/bellingboe/OktaKenkinsCI.git # timeout=10
Fetching upstream changes from https://github.com/bellingboe/OktaKenkinsCI.git
> git.exe --version # timeout=10
> git --version # 'git version 2.29.2.windows.3'
using GIT_ASKPASS to set credentials
> git.exe fetch --tags --force --progress -- https://github.com/bellingboe/OktaKenkinsCI.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> git.exe rev-parse "origin/master^{commit}" # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Restore packages)
Stage "Restore packages" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Clean)
Stage "Clean" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE
Is there something in my script that's wrong? I can't really tell what would be failing as I'm not seeing any errors at all.
I figured it out. My master branch was named "main" in github.
and I had a typo!

Jenkins Pipeline - Jenkins SCM step. Checkout returns invalid data not referring to parameters provided

I am setting up CI/CD for my project and decided to use Jenkins Pipeline. What I need basically:
Build from chosen branch(chosen from dropdown)
Execute some stages conditionally(depending on branch)
I configured pipeline to fetch Jenkinsfile from scm:
Then I decided to write cutom scm checkout as following:
stage('Checkout specific branch') {
steps {
echo "Checking out ${params.branchToBuild}..."
script {
def actualBranchCheckout = checkout([$class: 'GitSCM',
branches: [
[name: "${params.branchToBuild}"]
],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: "**"]],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: "${env.REPO_SSH_CREDS_ID}", url: "${env.REPO_SSH_URL}"]
]
])
}
}
}
And I have sveral concerns. GIT_ environment variables are set when checking out branch for Jenkinsfile.
GIT_BRANCH=origin/master
GIT_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_LOCAL_BRANCH=master, GIT_PREVIOUS_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2
That is fine, but when my checkout is performed the are not getting updated with actual values and remain the same.
Okay, the method itself returns map which may contain what I need so I can manually reset them, right?
But they turned out to be the same when checking out develop branch:
Here are the logs:
Obtained Jenkinsfile from git git#bitbucket.org:team/repo.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Repo/repo_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential jenkins-bitbucket
Cloning the remote Git repository
Cloning repository git#bitbucket.org:team/repo.git
> git init /var/lib/jenkins/workspace/Repo/repo_pipeline # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 31e727bf81b3379c9aa80224b8f941bc867acd77 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 31e727bf81b3379c9aa80224b8f941bc867acd77
Commit message: "print out variables"
> git rev-list --no-walk 11a5800b6ca31bc81545fa4874d73fa275c820c2 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout specific branch)
[Pipeline] echo
Checking out develop...
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential jenkins-bitbucket
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse origin/develop^{commit} # timeout=10
Checking out Revision 11a5800b6ca31bc81545fa4874d73fa275c820c2 (origin/develop)
> git config core.sparsecheckout # timeout=10
> git checkout -f 11a5800b6ca31bc81545fa4874d73fa275c820c2
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b develop 11a5800b6ca31bc81545fa4874d73fa275c820c2
Commit message: "another try"
First time build. Skipping changelog.
[Pipeline] echo
[GIT_BRANCH:origin/master, GIT_COMMIT:31e727bf81b3379c9aa80224b8f941bc867acd77, GIT_LOCAL_BRANCH:master, GIT_PREVIOUS_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_PREVIOUS_SUCCESSFUL_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_URL:git#bitbucket.org:team/repo.git]
I have another option to do everything manually, like git rev-parse --abbrev-ref HEAD for branch and so on. Do anyone know how to solve this/ is it a known issue or I am doing something wrong way?

How do you print out the value of a class property in a Jenkinsfile?

I'm creating a Jenkinsfile for use with GitHub Enterprise. I used the GUI settings in the pipeline job to specify a Jenkinsfile from a GitHub repo.
I'm using the scripted syntax instead of the declarative syntax.
I am able to checkout the repo in my Jenkinsfile using checkout scm. I want to use some information about the checkout in my script, such as the branch name and commit hash. However, I can't figure out how to access variables of the scm class.
When I run the job, it fails in the Checkout stage. The checkout from git seems to work properly, but it fails without printing any errors. If I delete the echo scm.GIT_BRANCH line it works fine.
node {
stage('Checkout') {
checkout scm
echo scm.GIT_BRANCH
}
}
Here's the output:
Started by user spark
Obtained nightly/Jenkinsfile from git https://github.enterprise.instance.com/spark/ci_flow_test
[Pipeline] node
Running on jenkins-server in /home/spark/ci_flow_test/pipeline_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] checkout
> /apps/git/git18/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /apps/git/git18/bin/git config remote.origin.url https://github.enterprise.instance.com/spark/ci_flow_test # timeout=10
Fetching upstream changes from https://github.enterprise.instance.com/spark/ci_flow_test
> /apps/git/git18/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials
> /apps/git/git18/bin/git fetch --tags --progress https://github.enterprise.instance.com/spark/ci_flow_test +refs/heads/*:refs/remotes/origin/*
> /apps/git/git18/bin/git rev-parse refs/remotes/origin/working^{commit} # timeout=10
> /apps/git/git18/bin/git rev-parse refs/remotes/origin/origin/working^{commit} # timeout=10
Checking out Revision 396f172c6061ba2760a71cba817df24836ec7e3b (refs/remotes/origin/working)
Commit message: "try echo"
> /apps/git/git18/bin/git config core.sparsecheckout # timeout=10
> /apps/git/git18/bin/git checkout -f 396f172c6061ba2760a71cba817df24836ec7e3b
> /apps/git/git18/bin/git rev-list 778c36171927020bd1afbd7206d86bf94abd1ed8 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] mail
[Pipeline] echo
Post script
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE
You can use the checkout scm return value to get git infomation
node {
stage('Checkout') {
def d = checkout scm
echo "branch: " + d.GIT_BRANCH
echo "commit: " + d.GIT_COMMIT
}
}
// supported fields
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_BRANCH
GIT_COMMIT
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_LOCAL_BRANCH
GIT_PREVIOUS_COMMIT
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

Resources