Jenkins
Introduction
This page is to provide some tips using the Jenkins Build implementation
Setup
The default site don't work so follow instructions on
https://pkg.origin.jenkins.io/debian-stable/
By default the home directory is
/var/lib/jenkins
You need to move this as /var/lib is not allowed for docker.
To do this you need to change the jenkins.service which resides in /lib/systemd/system/jenkins.service.
... # Directory where Jenkins stores its configuration and workspaces #Environment="JENKINS_HOME=/var/lib/jenkins" #WorkingDirectory=/var/lib/jenkins Environment="JENKINS_HOME=/home/jenkins" ...
Also if you are using LCOV you will need to implement a permanent change for CSP policy
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "default-src 'self'; style-src 'self' 'unsafe-inline';")
Without this the CSS is rendered incorrectly. Again to change this is done in /lib/systemd/system/jenkins.service.
Environment="JAVA_OPTS=-Djava.awt.headless=true -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'self'; style-src 'self' 'unsafe-inline';\""
HTTPS
Given you have a cert and key we can do
openssl pkcs12 -export \ -in /etc/localcerts/192.168.1.XX/server.crt \ -inkey /etc/localcerts/192.168.1.XX/server.key \ -name jenkins.bibble.co.nz -out jenkins.p12
keytool -importkeystore -srckeystore jenkins.p12 \ -srcstorepass 'xxxxx' -srcstoretype PKCS12 \ -srcalias jenkins.bibble.co.nz -deststoretype JKS \ -destkeystore jenkins.jks -deststorepass 'H1stage5!' \ -destalias jenkins.bibble.co.nz
Then you need to change the service in /lib/systemd/system/jenkins.service
Environment="JENKINS_PORT="-1"" Environment="JENKINS_HTTPS_PORT="8443"" Environment="JENKINS_HTTPS_KEYSTORE="/etc/jenkins/jenkins.jks"" Environment="JENKINS_HTTPS_KEYSTORE_PASSWORD="xxxxxxx"" Environment="JENKINS_HTTPS_LISTEN_ADDRESS="0.0.0.0""
Groovy
Could not get groovy to work on Ubuntu 22.04 using apt, ended up using snap.
Example Stuff
pipeline {
agent any
tools {
nodejs 'default-nodejs'
}
environment {
gitCredentialId = 'MY_USER'
gitUrl = 'https://ihcnz.visualstudio.com/XXXX/_git/XXXX_TASKS'
deployBranch = 'main'
dockerImage = ''
dockerImageVersionTag = "build-$BUILD_ID_$BUILD_TIMESTAMP"
}
parameters {
string(
name: 'Image_Name',
defaultValue: 'test-image',
description: '')
booleanParam(
name: 'PushImage',
defaultValue: false
)
}
stages {
stage('Hello') {
steps {
echo "Home1: $HOME"
}
stage('Pull Image Test') {
steps {
script {
docker.withRegistry("https://xxxx.dkr.ecr.ap-southeast-2.amazonaws.com", "ecr:ap-southeast-2:myAWSID") {
sh "docker pull xxxx.dkr.ecr.ap-southeast-2.amazonaws.com/myfolder/myTag:0.0.85"
}
}
}
}
stage('Cloning Git') {
steps {
git(
url: gitUrl,
credentialsId: gitCredentialId,
branch: deployBranch
)
}
}
stage('Startup') {
steps {
script {
sh 'npm install'
}
}
}
stage('Coverage') {
steps {
script {
sh 'npm run jenkins-test'
}
}
post {
always {
publishHTML target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'output/coverage/jest/lcov-report',
reportFiles : 'index.html',
reportName : 'PROJECT Test Report'
]
}
}
}
stage('Docker Build Image') {
steps {
script {
sh 'docker build --no-cache .'
}
}
}
stage('Docker Tag Image') {
steps {
script {
sh 'docker tag myimage:latest xxxx.dkr.ecr.ap-southeast-2.amazonaws.com/myfolder:${dockerImageVersionTag}'
}
}
}
stage('Docker Push Image') {
steps {
script {
docker.withRegistry("https://xxxx.dkr.ecr.ap-southeast-2.amazonaws.com", "ecr:ap-southeast-2:myAWSID") {
sh 'docker push myimage:latest xxxx.dkr.ecr.ap-southeast-2.amazonaws.com/myfolder:${dockerImageVersionTag}'
}
}
}
}
}
}
Plugins
Here is the list of some of the plugins I currently use
Build Timestamp Plugin Docker Pipeline HTML Publisher plugin Amazon ECR plugin Pipeline: AWS
Useful Stuff
- Restart http://localhost:8080/safeRestart
- Environment http://localhost:8080/env-vars.html