how to get BRANCH_NAME in jenkins windows command - jenkins

how to get BRANCH_NAME in jenkins windows command?
I tried
echo BRANCH_NAME
echo %BRANCH_NAME%
echo env.BRANCH_NAME
but jenkins console print the string "BRANCH_NAME" not the value of branch name.
I need branch_name to create in IIS dynamic aplications based on branch name and create a folder with branc_name

After installing Pipeline Multibranch Plugin(https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin)
Use env.BRANCH_NAME or Just BRANCH_NAME
For other environment-variables go to (<%http://localhost:8080%>/env-vars.html)

Related

Jenkins pipeline job

How to set branch name in variable in Jenkins Pipeline job?
I have Jenins pipeline below which returns null in BRANCH_NAME
node{
def BRANCH_NAME = env.BRANCH_NAME
echo BRANCH_NAME
}
To get branch name through env.BRANCH_NAME, your pipeline need to be Multiple Branch Pipeline.
BRANCH_NAME
For a multibranch project, this will be set to the name of the branch being built,
for example in case you wish to deploy to production from
master but not from feature branches;
if corresponding to some kind of change request, the name is generally arbitrary
(refer to CHANGE_ID and CHANGE_TARGET).

How can I select a Jenkins pipeline file from a branch specified in a build parameter

I have a parameterized Jenkins pipeline build. One of the parameters (branch) is the branch to build. The pipeline file is stored in the branch.
In the Pipeline definition, when I use */${branch} as the branch to build in place of */main the ${branch} does not get replaced but shows up as a literal. If I hard code the branch it works as expected.
The ${branch} does get replaced as expected in the pipeline file. So the branch parameter is being set.
Is there a way to get the parameter value into the "Pipeline script from SCM" retrieval from git?
You can try another approach as following:
In pipeline configuration page, change pipeline from SCM to pipeline script
Put following pipeline script in input box
node('<Jenkins node label>') {
properties([
parameters([
// parameter for branch
])
])
git url: '', credentailId:'', branch: "${branch}"
load '<relative path to your Jenkinsfile>'
}
You can use the branch parameter, when you disable the "Lightweight Checkout".
See JENKINS-28447

Using Jenkins global credentials in a pipeline (Jenkinsfile)

I am currently automating a project in Jenkins. I am using a pipeline that reads and executes a Jenkinsfile from a Source Management Tool (GIT in my case). For it to happen, I give the git URL and supply credentials with 'Jenkins Credentials Provider'and execute the build. It reads the Jenkinsfile and checks out the code, but fails at the next stage:
pipeline{
agent any
stages{
...
stage('Cloning GIT Repo'){
steps{
echo 'Cloning the repository'
git url: 'http://test.git'
}
}
...
It gives the error:
No credentials specified
It there a way for me to use the global credentials, I specified in the Jenkins UI earlier?
You can use credentialsId param
git(
url: 'http://test.git',
credentialsId: 'jenkins-credentials',
branch: "${branch}"
)
https://jenkins.io/doc/book/pipeline/jenkinsfile/#optional-step-arguments
https://jenkins.io/doc/pipeline/examples/#push-git-repo

Load Jenkins build parameters from SCM

Is it possible to have Jenkins pull a prameterized build from SCM (Git)?
I'm currently using "Pipeline script from SCM" where Jenkins retrieves the pipeline script but not the build parameters e.g. "String Parameter", "Choice Parameter", etc.
Jenkinsfile (Jenkins pipelines) are capable of this.
https://jenkins.io/doc/book/pipeline/syntax/#parameters
Be aware that parameters are post processed. So the 1st build will just be "Build" not "Build with parameters". After the 1st build, it will change.
This can be alleviated by using default values and always referencing the params using the full params.PARAM_NAME syntax. Don't just reference it as PARAM_NAME as this will cause Jenkins to search for env.PARAM_NAME by default.
Yes, you can retrieve parameters from a Parametrized Build in a Pipeline script from SCM Jenkins job.
To access them in the Jenkins file, use env.[PARAMETER_NAME].
For example:
echo 'Param value: ' + env.SOME_PARAMETER

What is the branch name variable for Jenkins multibranch pipelines?

I need to know which branch is being built in my Jenkins multibranch pipeline in order for it to run steps correctly.
We are using a gitflow pattern with dev, release, and master branches that all are used to create artifacts. The dev branch auto deploys, the other two do not. Also there are feature, bugfix and hotfix branches. These branches should be built, but not produce an artifact. They should just be used to inform the developer if there is a problem with their code.
In a standard build, I have access to the $GIT_BRANCH variable to know which branch is being built, but that variable isn't set in my multibranch pipeline. I have tried env.GIT_BRANCH too, and I tried to pass $GIT_BRANCH as a parameter to the build. Nothing seems to work. I assumed that since the build knows about the branch being built (I can see the branch name at the top of the console output) that there is something that I can use - I just can't find any reference to it.
The env.BRANCH_NAME variable contains the branch name.
As of Pipeline Groovy Plugin 2.18, you can also just use BRANCH_NAME
(env isn't required but still accepted.)
There is not a dedicated variable for this purpose yet (JENKINS-30252). In the meantime you can take advantage of the fact that the subproject name is taken from the branch name, and use
env.JOB_NAME.replaceFirst('.+/', '')
This has now been resolved, see Krzysztof KrasoĊ„'s answer.
There are 2 branches to consider in a Jenkins multibranch pipeline job:
The Jenkins job branch - env.BRANCH_NAME. This may have the same name as a git branch, but might also be called PR-123 or similar
The git branch - env.GIT_BRANCH. This is the actual branch name in git.
So a job might have BRANCH_NAME=PR-123 and GIT_BRANCH=my-scm-branch-name
Jenkins documentation has a list of all the env variable for your perusal here
Another way is using the git command to obtain the branch name on the current jenkins pipeline. For example, you can add the following snippet to print the branch name in your Jenkinsfile.
...
script {
def BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
echo ${BRANCH}
}
...
I found this stackoverflow post example useful: Git Variables in Jenkins Workflow plugin
sh '//...
git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
git_branch = readFile('GIT_BRANCH').trim()
echo git_branch
//...
'

Resources