Jenkins

From bibbleWiki
Revision as of 00:45, 16 April 2023 by Iwiseman (talk | contribs) (Groovy)
Jump to navigation Jump to search

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.

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';\""

Groovy

Could not get groovy to work on Ubuntu 22.04 using apt, ended up using snap.

Example Stuff

<syntaxhighlighting 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}'
         }
        }
      }
   }
 }

} </syntaxhighlighting>

Plugins

Here is the list of some of the plugins I currently use

 Build Timestamp Plugin
 Docker Pipeline
 HTML Publisher plugin
 Amazon ECR plugin

Useful Stuff