Jenkins Environment Variables
- Create a Jenkins free style project and invoke shell
printenv
* The result is
JENKINS_HOME=/var/lib/jenkins
GIT_PREVIOUS_SUCCESSFUL_COMMIT=09e0c01774bd717d3fe049c290b620b3d2124578
SSH_CLIENT=172.31.30.175 41402 22
USER=ubuntu
CI=true
RUN_CHANGES_DISPLAY_URL=http://35.85.155.160:8080/job/gameoflife-fs/7/display/redirect?page=changes
MAVEN_GOAL=package
XDG_SESSION_TYPE=tty
SHLVL=0
NODE_LABELS=MAVEN_JDK8 node1
HUDSON_URL=http://35.85.155.160:8080/
GIT_COMMIT=09e0c01774bd717d3fe049c290b620b3d2124578
MOTD_SHOWN=pam
OLDPWD=/home/ubuntu
HOME=/home/ubuntu
BUILD_URL=http://35.85.155.160:8080/job/gameoflife-fs/7/
HUDSON_COOKIE=55d1dcb1-4a26-465c-a6a7-4532597e58ce
JENKINS_SERVER_COOKIE=f4e759fd8c6d4913
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
WORKSPACE=/home/ubuntu/remote/workspace/gameoflife-fs
LOGNAME=ubuntu
NODE_NAME=node1
_=/usr/bin/java
RUN_ARTIFACTS_DISPLAY_URL=http://35.85.155.160:8080/job/gameoflife-fs/7/display/redirect?page=artifacts
GIT_BRANCH=origin/master
XDG_SESSION_CLASS=user
EXECUTOR_NUMBER=1
XDG_SESSION_ID=1
RUN_TESTS_DISPLAY_URL=http://35.85.155.160:8080/job/gameoflife-fs/7/display/redirect?page=tests
BUILD_DISPLAY_NAME=#7
HUDSON_HOME=/var/lib/jenkins
JOB_BASE_NAME=gameoflife-fs
PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
BUILD_ID=7
XDG_RUNTIME_DIR=/run/user/1000
BUILD_TAG=jenkins-gameoflife-fs-7
JENKINS_URL=http://35.85.155.160:8080/
LANG=C.UTF-8
JOB_URL=http://35.85.155.160:8080/job/gameoflife-fs/
GIT_URL=https://github.com/khajadevopsmarch23/game-of-life.git
BUILD_NUMBER=7
SHELL=/bin/bash
RUN_DISPLAY_URL=http://35.85.155.160:8080/job/gameoflife-fs/7/display/redirect
HUDSON_SERVER_COOKIE=f4e759fd8c6d4913
JOB_DISPLAY_URL=http://35.85.155.160:8080/job/gameoflife-fs/display/redirect
JOB_NAME=gameoflife-fs
PWD=/home/ubuntu/remote/workspace/gameoflife-fs
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
SSH_CONNECTION=172.31.30.175 41402 172.31.21.190 22
GIT_PREVIOUS_COMMIT=09e0c01774bd717d3fe049c290b620b3d2124578
WORKSPACE_TMP=/home/ubuntu/remote/workspace/gameoflife-fs@tmp
- The complete list of variables
The following variables are available to shell and batch build steps:
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).
BRANCH_IS_PRIMARY
For a multibranch project, if the SCM source reports that the branch being built is a primary branch, this will be set to "true"; else unset. Some SCM sources may report more than one branch as a primary branch while others may not supply this information.
CHANGE_ID
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.
CHANGE_URL
For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.
CHANGE_TITLE
For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset.
CHANGE_AUTHOR
For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset.
CHANGE_AUTHOR_DISPLAY_NAME
For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset.
CHANGE_AUTHOR_EMAIL
For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset.
CHANGE_TARGET
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.
CHANGE_BRANCH
For a multibranch project corresponding to some kind of change request, this will be set to the name of the actual head on the source control system which may or may not be different from BRANCH_NAME. For example in GitHub or Bitbucket this would have the name of the origin branch whereas BRANCH_NAME would be something like PR-24.
CHANGE_FORK
For a multibranch project corresponding to some kind of change request, this will be set to the name of the forked repo if the change originates from one; else unset.
TAG_NAME
For a multibranch project corresponding to some kind of tag, this will be set to the name of the tag being built, if supported; else unset.
TAG_TIMESTAMP
For a multibranch project corresponding to some kind of tag, this will be set to a timestamp of the tag in milliseconds since Unix epoch, if supported; else unset.
TAG_UNIXTIME
For a multibranch project corresponding to some kind of tag, this will be set to a timestamp of the tag in seconds since Unix epoch, if supported; else unset.
TAG_DATE
For a multibranch project corresponding to some kind of tag, this will be set to a timestamp in the format as defined by java.util.Date#toString() (e.g., Wed Jan 1 00:00:00 UTC 2020), if supported; else unset.
JOB_DISPLAY_URL
URL that will redirect to a Job in a preferred user interface
RUN_DISPLAY_URL
URL that will redirect to a Build in a preferred user interface
RUN_ARTIFACTS_DISPLAY_URL
URL that will redirect to Artifacts of a Build in a preferred user interface
RUN_CHANGES_DISPLAY_URL
URL that will redirect to Changelog of a Build in a preferred user interface
RUN_TESTS_DISPLAY_URL
URL that will redirect to Test Results of a Build in a preferred user interface
CI
Statically set to the string "true" to indicate a "continuous integration" execution environment.
BUILD_NUMBER
The current build number, such as "153".
BUILD_ID
The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds.
BUILD_DISPLAY_NAME
The display name of the current build, which is something like "#153" by default.
JOB_NAME
Name of the project of this build, such as "foo" or "foo/bar".
JOB_BASE_NAME
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".
BUILD_TAG
String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". All forward slashes ("/") in the JOB_NAME are replaced with dashes ("-"). Convenient to put into a resource file, a jar file, etc for easier identification.
EXECUTOR_NUMBER
The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1.
NODE_NAME
Name of the agent if the build is on an agent, or "built-in" if run on the built-in node (or "master" until Jenkins 2.306).
NODE_LABELS
Whitespace-separated list of labels that the node is assigned.
WORKSPACE
The absolute path of the directory assigned to the build as a workspace.
WORKSPACE_TMP
A temporary directory near the workspace that will not be browsable and will not interfere with SCM checkouts. May not initially exist, so be sure to create the directory as needed (e.g., mkdir -p on Linux). Not defined when the regular workspace is a drive root.
JENKINS_HOME
The absolute path of the directory assigned on the controller file system for Jenkins to store data.
JENKINS_URL
Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration).
BUILD_URL
Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set).
JOB_URL
Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set).
GIT_COMMIT
The commit hash being checked out.
GIT_PREVIOUS_COMMIT
The hash of the commit last built on this branch, if any.
GIT_PREVIOUS_SUCCESSFUL_COMMIT
The hash of the commit last successfully built on this branch, if any.
GIT_BRANCH
The remote branch name, if any.
GIT_LOCAL_BRANCH
The local branch name being checked out, if applicable.
GIT_CHECKOUT_DIR
The directory that the repository will be checked out to. This contains the value set in Checkout to a sub-directory, if used.
GIT_URL
The remote URL. If there are multiple, will be GIT_URL_1, GIT_URL_2, etc.
GIT_COMMITTER_NAME
The configured Git committer name, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.name Value field of the Jenkins Configure System page.
GIT_AUTHOR_NAME
The configured Git author name, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.name Value field of the Jenkins Configure System page.
GIT_COMMITTER_EMAIL
The configured Git committer email, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.email Value field of the Jenkins Configure System page.
GIT_AUTHOR_EMAIL
The configured Git author email, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.email Value field of the Jenkins Configure System page.
- Lets build the game of life project and copy the war file into
/tmp/<PROJECT-NAME>/<BUILD-ID>/gameoflife.war
- Lets do the same with Declarative pipelines Refer Here for changes
Notification Scenario
- Send an email build success to the
team-all-qt@qt.com
when the project is success. - Send an email build failure to Author
- While sending email ensure you also send url of the jenkins project with currrent build
- Refer Here for the changes done.
Building dotnet project and publishing test results
- Refer Here for the project and below are the Jenkinsfile complete version and changes done
pipeline {
agent { label 'dotnet6' }
triggers { pollSCM('* * * * *') }
stages {
stage('vcs') {
steps {
git branch: 'main',
url: 'https://github.com/khajadevopsmarch23/MusicStore.git'
}
}
stage('build') {
steps {
sh 'dotnet restore ./MusicStore/MusicStore.csproj && dotnet build ./MusicStore/MusicStore.csproj'
}
}
stage('test') {
steps {
sh 'dotnet test --logger "junit;LogFilePath=TEST-musicstoretest.xml" ./MusicStoreTest/MusicStoreTest.csproj'
junit testResults: '**/TEST-*.xml'
}
}
}
}
- Added Jenkinsfile Refer Here
- Added testresults with failure
- Corrected Test results Refer Here
Jenkins User Management
- In Jenkins we can add users.
- Jenkins has following ways for usermangement
- Delegate to servlet container: This is external application which maintains users
- Jenkins own user database
- LDAP
- Unix User/group database
- None
- The above Configuration is Manage Jenkins => Configure Global Security
- Now to add users for Jenkins own user database
- Manage Jenkins => Users
- Now for authorization
- Jenkins can also provide role base authorization Refer Here
Administrative Activities in Jenkins
- Backup and Recovery:
- Jenkins stores all the information in /var/lib/jenkins (JENKINS_HOME) of jenkins master, backup this folder (rsync)
- High Availability
- Plugin installations:
- Available from jenkins community (Manage Plugins => Available
=> Install) - Custom Plugins: Ensure you have jpi (jenkins plugin interface) or hpi (hudson plugin interface) of the custom plugin
- Available from jenkins community (Manage Plugins => Available