I am using Jenkins (v2.331) with Git Plugin (v4.11).
I found that Jenkins allowes regular expressions or ${ENV_VARIABLE} as a branch-specifier at the "Branches to build" option. My problem is:
Can I use both regular expression and ${ENV_VARIABLE} to specify the branches?
such as: :.*/${version}/\d{8} . I also tried to use wildcards:*/${version}/*.But it doesn't seem to get the correct result.
Related
I'm currently having a hard time figuring out how to check out a specific changeset using the build in cm command in a Jenkins Pipeline build. It seems that the changeset = 1234 parameter is ignored.
I tried the statement:
cm repository: item['Repo'], changeset: Stable_CS, server: item['server'], useUpdate: false, workspaceName: item['Repo']
Stable_CS is a variable that is filled with another cm command using the bat pipeline statement. It contains a string like 1234
Thanks very much in advance
I'm afraid that Plastic SCM plugin for Jenkins doesn't currently support checking out specific changesets, neither in freestyle projects nor pipelines. It can only target branches, as seen in the Plugin wiki page.
I have a Jenkins with the Github organization plugin that scans my organization and build all the branches/PRs of all the repositories within the organization that have a Jenkinsfile.
It works great, but I would like to retrieve for each build the commit SHA, in order to tag Docker images with both the branch name and the commit SHA.
Getting the branch name works great with env.BRANCH_NAME, however I cannot find any way to get the commit SHA.
The catch is that we are using JGit, so I cannot use git log to retrieve it.
I tried having a look at what is contained in the ENV using sh 'printenv', but there's nothing of any use there.
I also tried the following:
def checkoutResults = checkout scm
echo 'checkout results: ' + checkoutResults
but this yields the following result:
checkout results: [:]
Even though I cannot get the revision from my pipeline, Jenkins is getting it alright, as I can see in the logs:
...
Obtained Jenkinsfile from 98062e5f651ca698f4303c3bb8d20665ce491294
...
Checking out Revision 98062e5f651ca698f4303c3bb8d20665ce491294 (docker)
I am running the following versions:
Jenkins 2.73.3
Git plugin plugin 3.3.0
Pipeline: SCM Step plugin 2.6
Would appreciate any help in retrieving the commit SHA / revision in this particular situation.
The git plugin did not include the fix until the 3.3.2 release. You'll need to update to at least git plugin 3.3.2.
The current git plugin release as of 23 Nov 2017 is 3.6.4. It includes significant additions and changes for multi-branch pipelines.
There is also a known bug in the reporting of NAME and EMAIL values which has a regression test that confirms the bug affects all implementations (git and jgit). You can use that regression test as an example of using those values if needed.
If you cannot update from git plugin 3.3.0 to 3.3.2, you may be able to use the JGit classes from within the pipeline script to perform the same type of query as was offered with command line git in another answer to the question. I have never done it, but I believe it is possible.
I created a small groovy function
def getCommitSha(){
return sh(returnStdout: true, script: 'git rev-parse HEAD')
}
you can add it to your pipeline , or to your shared library if you are using one ( if not it's a good time to start ... :-) )
I'm doing sonar integration and would like to pass git branch as a parameter. It will be run on Jenkins server.
Before I was using next line of code to get current git branch:
def workingBranch = """git rev-parse --abbrev-ref HEAD""".execute().text.trim()
After I replaced it with:
grgit.branch.current.fullName
But this always gives me "HEAD". How to achieve same functionality?
I am doing something very similar. As it turns out, the Git plugin in Jenkins is tuned in several ways to minimize the git clone and checkout. There are 2 ways that I've found to deal with this.
The Easy Way
Use Jenkins' built-in environment variables, as you already suggested in the comments.
def workingBranch = System.getenv("GIT_BRANCH") ?: grgit.branch.current.fullName
The Job Configuration Way
You can also set up the job to checkout the branch as a local branch, rather than a detached HEAD. This is under "Additional Behaviors", named "Check out to specific local branch". There are many other questions that detail the settings for that, and/or the Declarative Pipeline approach, depending on what your needs.
Declarative Pipeline custom checkout
Configuring local branch option
I have setup Gerrit and Jenkins. I have configured Jenkins' gerrit-trigger plugin so Gerrit changes are validated by Jenkins.
Now, want to automatically create Jenkins jobs, using Jenkins jobgenerator plugin when a new repository is created in Gerrit.
Therefore I want to create a Gerrit hook (file ./hooks/project-created).
I have also created a Jenkins job to generate Jenkins validation job.
But when I simulate the hook manually, with
JENKINS_URL=http://127.0.0.1:8081/jenkins
java -jar jenkins-cli.jar -i ~/.ssh/id_rsa build puppet_creator -p PROJECT=foo BRANCH=bar
I get the error:
'PROJECT' is not a valid parameter. Did you mean null?
As mentioned above, the parameter are declared in my Jenkins Job, what's wrong? I have tried both String and Text parameter types.
Well, I should have RTFM properly:
Jenkins jobgenerator plugin has it's own kind of parameter: Generator Parameter and Generator Choice. One must use those instead of usual string, Choice Parameter):
So, this work:
I want to migrate our old free style dev builds, in which we use the branch name as a build parameter, to workflow builds.
This works fine so far, the only thing we are really missing is the ability to use the parameter, e.g. "branch_name", as the branch specifier for the Workflow script from SCM section.
On a free style build this works fine.
Any ideas how this could be achieved? We don't want a dev to change the configuration all the time before starting a build.
Try to disable "Lightweight checkout" checkbox.
This behavior is noted when clicking the help question mark for the "Lightweight checkout" option:
Also build parameters will not be substituted into SCM configuration in this mode.
Found that in latest comments of JENKINS-28447
Sounds like JENKINS-28447:
When selecting the "Groovy CPS DSL from SCM" option for a worflow job,
the SCM plugins do not appear to resolve build parameters or
environment variables. I am using the git plugin and when I use it
from other jobs I can specify a build parameter, like "BuildBranch",
and use that when specifying what branch should be built
The workaround would be to use an inline bootstrap script that calls load after checkout, as described in the tutorial.
Alternately, go further by creating a multibranch workflow project, so that each branch is built automatically with its own history.
I have a workflow DSL script described here: https://groups.google.com/forum/#!msg/jenkinsci-users/jSKwSKbaXq8/dG2mn6iyDQAJ
In that script, I have a build parameter called FREEBSD_SRC_URL , which is
passed down to the workflow. Based on different parameters in that URL,
a different branch can be checked out.
If you are using git, you can still use the same technique for passing a build parameter down to the script, but you would need to do things slightly differently. For example, you could define a parameter BRANCH_NAME in your job and do something like this in your workflow script:
String checkout_url = "https://github.com/jenkinsci/jenkins"
String branch_name = "master"
if (getBinding().hasVariable("CHECKOUT_URL")) {
// override default URL from build parameter
checkout_url = CHECKOUT_URL
}
if (getBinding().hasVariable("BRANCH_NAME")) {
// override default branch from build parameter
branch_name = BRANCH_NAME
}
node {
// Do the git checkout
git branch: "${branch_name}", url: "${checkout_url}"
}