Jenkins: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= This page is to provide some tips using the Jenkins Build implementation =Setup= By default the home directory is /var/lib/jenkins You need to move this as /var/lib is not allowed for docker.<br> <br> 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/j..."
 
 
(15 intermediate revisions by the same user not shown)
Line 2: Line 2:
This page is to provide some tips using the Jenkins Build implementation
This page is to provide some tips using the Jenkins Build implementation
=Setup=
=Setup=
The default site don't work so follow instructions on
  https://pkg.origin.jenkins.io/debian-stable/
By default the home directory is  
By default the home directory is  
   /var/lib/jenkins
   /var/lib/jenkins
Line 11: Line 14:
   # Directory where Jenkins stores its configuration and workspaces
   # Directory where Jenkins stores its configuration and workspaces
   #Environment="JENKINS_HOME=/var/lib/jenkins"
   #Environment="JENKINS_HOME=/var/lib/jenkins"
   #WorkingDirectory=/var/lib/jenkins
   WorkingDirectory=/var/lib/jenkins
 
 
   Environment="JENKINS_HOME=/home/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 'xxxxxx' \
  -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==
<syntaxhighlight lang="groovy">
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}'
          }
        }
      }
    }
  }
}
</syntaxhighlight>
=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
<br>
When Trying to get this to work it failed with a string error. Which was an issue setting up the node plugin. The validation of data is very poor
<syntaxhighlight lang='bash'>
Caused: BUG! exception in phase 'semantic analysis' in source unit 'WorkflowScript' unexpected NullpointerException
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1070)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.lambda$doParse$0(CpsGroovyShell.java:135)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:132)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
</syntaxhighlight>
=Useful Stuff=
*Restart http://localhost:8080/safeRestart
*Environment http://localhost:8080/env-vars.html

Latest revision as of 07:49, 28 April 2024

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 'xxxxxx' \
 -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


When Trying to get this to work it failed with a string error. Which was an issue setting up the node plugin. The validation of data is very poor

Caused: BUG! exception in phase 'semantic analysis' in source unit 'WorkflowScript' unexpected NullpointerException
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1070)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.lambda$doParse$0(CpsGroovyShell.java:135)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:132)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

Useful Stuff