I am new to Jenkins. I have it installed on a Windows server, running as a service. I have builds running fine, however, I am not able at all to get the environment variables to work. I have tried $BRANCH_NAME, ${BRANCH_NAME}, %BRANCH_NAME% and none of them work. I have setup a parameterized build where I set up a variable BRCHNAME with the default value of one of my branch names, but the variable does not get interpreted properly, it comes through as %BRCHNAME% when it executes the command.
This is from the console output:
C:\Program Files\Git\bin\git.exe rev-parse "origin/%BRCHNAME%^{commit}" # timeout=10
Related
If there is a Jenkins shell script build step, environment variables are set so that, for example, if you echo $WORKSPACE you see the current working directory. I am writing a custom plugin which also can execute shell commands but WORKSPACE is not set when this plugin executes.
I could see that Jenkins sets all those env variables prior to executing the shell commands the Jenkins project specifies so that they would not be already set for a custom plugin. If that is the case, it would seem like there is no way to access those env variables.
But if there is a way to obtain the values of the env variables that would be useful.
Read-only variables
These variables are pre-defined for both Builds and Preview environments. They are set automatically and cannot be changed. You can reference them in your gatsby-config.js or anywhere else you would normally reference an environment variable.
BRANCH: The name of the current git branch. Useful for swapping environment variables depending on the branch.
CI: Always true.
GATSBY_CLOUD: Always true. Useful for checking if your build is running on Gatsby Cloud.
This is what gatsby cloud doc said.
but when I print out console for the process.env.BRANCH, it was undefined while process.env.GATSBY_CLOUD is true as expected.
Any help is welcomed.
For one of my projects that I have on GitHub, I wanted to build it as a docker image and push it to my docker hub. The project is a sbt one with a Scala codebase.
Here is how my JenkinsFile is defined:
#!groovy
node {
// set this in Jenkins server under Manage Jenkins > Credentials > System > Global Credentials
docker.withRegistry('https://hub.docker.com/', 'joesan-docker-hub-credentials') {
git credentialsId: '630bd271-01e7-48c3-bc5f-5df059c1abb8', url: 'https://github.com/joesan/monix-samples.git'
sh "git rev-parse HEAD > .git/commit-id"
def commit_id = readFile('.git/commit-id').trim()
println comit_id
stage "build" {
def app = docker.build "Monix-Sample"
}
stage "publish" {
app.push 'master'
app.push "${commit_id}"
}
}
}
When I tried to run this from my Jenkins server, I get the following error:
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:161)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:65)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:157)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:232)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE
Since this is running inside a VM on Azure, I thought the VM was not able to reach outside, but that seems not to be the case as I was able to ssh into the VM and git pull from the Git repo. So what is the problem here? How could I make this work?
for me unchecking "lightweight checkout" fixed the issue
I experienced the exact same error. My setting:
Pipeline build inside a dockerized Jenkins (version 2.32.3)
In the configuration of the job, I specified a check out into a subdirectory: Open the configuration, e.g. https://myJenkins/job/my-job/configure. At the bottom, see section Pipeline -> Additional Behaviours -> Check out into a sub-directory with Local subdirectory for repo set to, e.g., my-sub-dir.
Expectation: Upon check out, the Jenkinsfile ends up in my-sub-dir/Jenkinsfile.
Via the option Script path, you configure the location of the Jenkinsfile so that Jenkins can start the build. I put my-sub-dir/Jenkinsfile as value.
I then received the exception you pasted in your question. I fixed it by setting Script Path to Jenkinsfile. If you don't specify a sub-directory for check out, then still try double checking values for Script Path.
Note: I have another Jenkins instance at work. There I have to specify Script Path including the custom check out sub-directory (as mentioned in Expectation above).
GO TO Job-->Config-->Pipline and uncheck checkbox lightweight checkout"
lightweight checkout : selected, try to obtain the Pipeline script contents >directly from
the SCM without performing a full checkout. The advantage of this mode
is its efficiency; however, you will not get any changelogs or polling
based on the SCM. (If you use checkout scm during the build, this will
populate the changelog and initialize polling.) Also build parameters
will not be substituted into SCM configuration in this mode. Only
selected SCM plugins support this mode.
I'm relatively new to the Jenkins world and I'm having issues with the use of environment variables.
I'm currently running builds from code I check out from a git repository. I have an environment variable called GIT_BUILD_NUMBER that I pull from the checkout and use successfully in a Windows Batch Command Block:
::Build Project
python buildProj.py --branch %Branch% --bin_src %WORKSPACE% --build_number %GIT_BUILD_NUMBER%
This executes successfully with the proper git build number. In the very next "Execute Windows Batch command" block, I simply try to use that same variable in a directory path and this causes the build to fail:
J:\builds\%GIT_BUILD_NUMBER%\
I've also tried this:
J:\\builds\\%GIT_BUILD_NUMBER%\\
When I echo this line, I just get:
J:\builds\\
Any insight would be greatly appreciated.
Near the top of my build console I see a "Last Built Revision:" w/ a revision #. How can I access this last built rev # in my build script? I'm using Gradle, but I don't think that matters here. Does Jenkins provide the last built rev # in a system property? Surely this must be trivial to access from my build script...
You can directly access the Jenkins BUILD_NUMBER as system environment variable.
task getBuildNumber << {
ext.env = System.getenv()
ext.buildNumber = env.BUILD_NUMBER?.toInteger()
println "Build Number: $buildNumber"
}
Turns out, the Git plugin DOES export the last build revision as an environment variable. So instead of using the accepted answer:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
you can just use this instead:
GIT_PREVIOUS_COMMIT
One failproof way to see exactly what's available to your build script is to choose Add Build Step > Execute Shell then simply add the following:
export
view your console (for the build) and you should see lots of great environment variables available to you. The git-related variables that were available to me (using the git plugin) were:
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_BRANCH
GIT_COMMIT
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_PREVIOUS_COMMIT
GIT_URL
Lastly, to see a less comprehensive list of available environment variables, you can also just go to this url: http://[your-jenkins-domain-and-port]/env-vars.html
I do not think the git plugin exports the last built revision as an environment variable, but the information is easily available using a simple shell command like:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
BUILD_URL always points to the build's own page and the rest of the information seems to be available using the xml api.
The current build-number is provided as the Jenkins-variable BUILD_NUMBER
In Unix it is set for you as ${BUILD_NUMBER}
In Windows it is available as %BUILD_NUMBER%
The complete list of variables is available on your Jenkins server, at: http://[your-jenkins-server]/env-vars.html
The Git plugin returns information from the checkout() command. In Pipeline script, this can be used to get the previous commit:
def scmVars = checkout scm
scmVars.GIT_PREVIOUS_COMMIT