Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Using a Sealights Token defined in Jenkins Credentials

You can define an entry in your Jenkins Credentials to store your Agent token and use it in your pipeline script.

...

Code Block
languagegroovy
withCredentials([string(credentialsId: 'sl.agent.token.dev.cs', variable: 'SL_TOKEN')]) {
  sh '''
  echo -n "$SL_TOKEN" > sltoken.txt
  '''
}

Sample script for Maven integration (Java Agent)

Code Block
languagegroovy
pipeline {
    agent any

    tools {
      jdk '1.8'
    }
    
   stages {
       
        stage('Git') {
            steps {
                // Wipe out workspace
                cleanWs()

                // Get some code from a GitHub repository
                git 'https://github.com/marcsealights/java-gps-calorie-calculator.git'
            }
        }       
       
       stage ("Adding Sealights") {
            steps {
                             withCredentials([string(credentialsId: 'sl.agent.token.dev.cs', variable: 'SL_TOKEN')]) {
                  sh '''
                  echo -n "$SL_TOKEN" > sltoken.txt

                 ''' echo "Downloading Sealights Agent..."
            }      wget -nv  https://agents.sealights.co/sealights-java/sealights-java-latest.zip
                  unzip -oq sealights-java-latest.zip
     script {            echo "Local agent version is now:" `cat sealights-java-version.txt` "\n"
sh '''                 '''
   echo  '{           }
             "tokenFile": "sltoken.txt",  
                writeFile file: 'slmaven.json', text: '''\
 "createBuildSessionId": true,              |{
          "appName": "${env.JOB_NAME}",     |  "executionType": "full",
                |  "branchNametokenFile": "mastersltoken.txt",
                    |    "buildNamecreateBuildSessionId": "${env.BUILD_NUMBER}"true,
                 |        "packagesIncluded"appName": "*info.puzz.*${env.JOB_NAME}",  //Specific
to your app                      |   "packagesExcludedbranchName": "${env.BRANCH_NAME}",
                   |     "filesIncludedbuildName": "*.class${env.BUILD_NUMBER}", 
                  |     "filesExcludedpackagesIncluded": "*test-classesinfo.puzz.*",  //Specific to your app
                |   "recursivepackagesExcluded": true"",
                |        "includeResources": true,
                  |      "testStage": "Unit Tests",
                  |      "labIdsealightsJvmParams": null{},
                |        "executionType"proxy": "full"null,
                  |      "logEnabled": truefalse,
                      |  "logDestination": "console",
                    |    "logLevel": "erroroff",
                |}
       "logFolder": "/tmp",        '''.stripMargin().stripIndent()

				script {
					if (true) {
						// Define SCM details
      "sealightsJvmParams": {},
                        "enabled": true
                     }' > slmaven.json

                    set +x
                    echo "Downloading Sealights Latest Agent..."
                    wget -nv https://agents.sealights.co/sealights-java/sealights-java-latest.zip
                    unzip -o sealights-java-latest.zip
                    echo "Local agent version is now:" `cat sealights-java-version.txt`
                    echo
                    echo "Updating POM with Sealights..."
                    						env.scmOnPremProvider = "github"
						env.scmOnPremBaseUrl = "https://github.com/marcsealights/java-gps-calorie-calculator/blob/"
						env.scmOnPremVersion = '2.19.0' // Adjust as necessary

					    // Dynamically set SCM version if needed
						// env.scmOnPremVersion = sh(script: 'curl -sI https://github.mycompany.com/api/v3 | grep Version | awk \'{print $2}\' | tr -d \'\\r\'', returnStdout: true).trim() //github
						// env.scmOnPremVersion = sh(script: 'curl -s https://mybitbucket.mycompany.com/rest/api/1.0/application-properties | jq -r .version', returnStdout: true).trim() //bitbucket
						// env.scmOnPremVersion = sh(script: 'curl https://mygitlab.mycompany.com/api/v4/version | jq -r .version', returnStdout: true).trim() //gitlab

						// Prepare the JSON modifier based on SCM details
						env.OnPremJvmParam = "\"sealightsJvmParams\": { \"sl.scm.provider\": \"${env.scmOnPremProvider}\", \"sl.scm.baseUrl\": \"${env.scmOnPremBaseUrl}\", \"sl.scm.version\": \"${env.scmOnPremVersion}\" },"

						// Update slmaven.json by replacing the empty sealightsJvmParams object with OnPremJvmParam content
						sh """
						sed -i '/"sealightsJvmParams": {},/c\\
						${env.OnPremJvmParam}
						' slmaven.json
						"""
					}
				}
				
				script {
					// Attempt to update POM with Sealights configuration
					sh 'java -jar sl-build-scanner.jar -pom -configfile slmaven.json -workspacepath .
                 ''' 
                '
				}
            }
        }
       
      stage('Build') {
            steps {
                // Run Maven with regular command
                sh "mvn clean package"
         }
      }
   }
}

...