Home > Back-end >  Create JSON file from Groovy variables in Pipeline - jenkins job
Create JSON file from Groovy variables in Pipeline - jenkins job

Time:10-04

i am new to jenkins, i have a pipline job with parameters.

I want to create a JSON file and write my parameters there. (and then let my jar file read that JSON file and run according to it) how can i do this in groovy?

this is my jenkins file:

pipeline {
    agent {
        label "create_pass_criteria"
    }
    

    parameters {
        string(name: 'IP', description: 'Please enter your ip')
        password(name: 'PASSWORD',description: 'Please enter your mx password')
        string(name: 'NAME', description: 'Please enter the name ')
    }

    tools {
        maven 'maven-3.3.9'

    }
    options
            {
                buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '20'))
                gitLabConnection('gitlab')
            }


    stages {
        stage('Git Clone') {
            steps {
                updateGitlabCommitStatus name: 'Build', state: 'running'
                checkout([
                        $class                           : 'GitSCM',
                        branches                         : [[name: '*/master']],
                        doGenerateSubmoduleConfigurations: false,
                        extensions                       : [],
                        submoduleCfg                     : [],
                        userRemoteConfigs                : [[credentialsId: GIT_CRED, url: GIT_PATH]]
                ])
            }
        }
        stage('Build') {
            steps {
                sh 'mvn install'
            }
        }

        stage('run') {
            steps {
                sh 'java -jar /var/lib/jenkins/workspace/create_pass_criteria/target/create_pass_criteria-8.0.125-SNAPSHOT.jar'
            }
        }

    }

    post {
        success {
            updateGitlabCommitStatus name: 'Build', state: 'success'
            emailext(
                    to: EMAIL_ADDR,
                    subject: "Success Pipeline: ${currentBuild.fullDisplayName}",
                    body: "Pipeline URL: ${env.BUILD_URL}",
                    mimeType: 'text/html'
            )
        }

        failure {
            updateGitlabCommitStatus name: 'Build', state: 'failed'
            emailext(
                    to: EMAIL_ADDR,
                    subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                    body: "Pipeline URL: ${env.BUILD_URL}",
                    mimeType: 'text/html'
            )
        }
    }
} // pipeline

i don't know if it is correct but this is what i need to add to my Jenkins file?:

    node{
        //to create json declare a sequence of maps/arrays in groovy
        //here is the data according to your sample
        def data = [
            attachments:[
                [
                    mxIp : params.MX_IP,
                    mxPassword : params.MX_PASSWORD,
                    policyName : params.POLICY_NAME,
                ]
            ]
        ]
        writeJSON(file: 'parameters.json', json: data)
}

if yes, at which part does it is has to be?

CodePudding user response:

You could put this code in a script block like this:

stage('run') {
    steps {
        script {
            def data = [
                attachments:[
                    [
                        mxIp : params.MX_IP,
                        mxPassword : params.MX_PASSWORD,
                        policyName : params.POLICY_NAME,
                    ]
                ]
            ]
            writeJSON(file: 'parameters.json', json: data)
        }
        
        sh 'java -jar /var/lib/jenkins/workspace/create_pass_criteria/target/create_pass_criteria-8.0.125-SNAPSHOT.jar'
    }
}

In complex pipelines I try to create clean code by adhering to the single level of abstraction principle. In this case I would extract the script and sh steps into a separate function, which could then be called from the pipeline section as a single step:

stage('run') {
    steps {
        createPassCriteria()
    }
}

Define the function after the closing } of the pipeline section:

void createPassCriteria() {
    def data = [
        attachments:[
            [
                mxIp : params.MX_IP,
                mxPassword : params.MX_PASSWORD,
                policyName : params.POLICY_NAME,
            ]
        ]
    ]
    writeJSON(file: 'parameters.json', json: data)

    sh 'java -jar /var/lib/jenkins/workspace/create_pass_criteria/target/create_pass_criteria-8.0.125-SNAPSHOT.jar'
}
  • Related