github.com/jfrog/jfrog-cli-core/v2@v2.52.0/general/cisetup/jenkinsfilegenerator.go (about)

     1  package cisetup
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     8  	"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
     9  )
    10  
    11  const JenkinsfileName = "Jenkinsfile"
    12  const m2HomeSet = `
    13  	  // The M2_HOME environment variable should be set to the local maven installation path.
    14  	  M2_HOME = ""`
    15  const jenkinsfileTemplate = `pipeline {
    16  	agent any
    17  	environment {
    18  		%s
    19  
    20  	  JFROG_CLI_BUILD_NAME = "${JOB_NAME}"
    21  	  JFROG_CLI_BUILD_NUMBER = "${BUILD_NUMBER}"
    22  
    23  	  // Sets the CI server build URL in the build-info.
    24  	  JFROG_CLI_BUILD_URL = "https://<my-jenkins-domain>/<my-job-uri>/${BUILD_NUMBER}/console"
    25  	  
    26  	}
    27  	stages {
    28  		stage ('Clone') {
    29  			steps {
    30  				// If cloning the code requires credentials, follow these steps:
    31  				// 1. Uncomment the ending of the below 'git' step.
    32  				// 2. Create the 'git_cred_id' credentials as described here - https://www.jenkins.io/doc/book/using/using-credentials/
    33  				git branch: %q, url: %q //, credentialsId: 'git_cred_id'
    34  			}
    35  		}
    36     
    37  		stage ('Config') {
    38  			steps {
    39  				// Download JFrog CLI.
    40  				sh 'curl -fL https://getcli.jfrog.io | sh && chmod +x jfrog'
    41  
    42  				// Configure JFrog CLI 
    43  				withCredentials([string(credentialsId: 'rt-password', variable: 'RT_PASSWORD')]) {
    44  					sh '''./jfrog c add %s --url %s --user ${RT_USERNAME} --password ${RT_PASSWORD}
    45  					./%s
    46  					'''
    47  				}
    48  			}
    49  		}
    50     
    51  		stage ('Build') {
    52  			steps {
    53  				dir('%s') {
    54  					sh '''./%s'''
    55  				}
    56  			}
    57  		}
    58  	}
    59  	   
    60  	post {
    61  		success {
    62  			script {
    63  				env.JFROG_BUILD_STATUS="PASS"
    64  			}
    65  		}
    66  		 
    67  		failure {
    68  			script {
    69  				env.JFROG_BUILD_STATUS="FAIL"
    70  			}
    71  		}
    72  		 
    73  		cleanup {
    74  			// Collect and store environment variables in the build-info
    75  			sh './jfrog rt bce'
    76  			// Collect and store VCS details in the build-info
    77  			sh './jfrog rt bag'
    78  			// Publish the build-info to Artifactory
    79  			sh './jfrog rt bp'
    80  			sh './jfrog c remove %s --quiet'
    81  		}
    82  	}
    83    }`
    84  
    85  type JenkinsfileGenerator struct {
    86  	SetupData *CiSetupData
    87  }
    88  
    89  func (jg *JenkinsfileGenerator) Generate() (jenkinsfileBytes []byte, jenkinsfileName string, err error) {
    90  	serviceDetails, err := config.GetSpecificConfig(ConfigServerId, false, false)
    91  	if err != nil {
    92  		return nil, "", err
    93  	}
    94  	buildToolsConfigCommands := strings.Join(getTechConfigsCommands(ConfigServerId, false, jg.SetupData), cmdAndOperator)
    95  	buildCommand, err := convertBuildCmd(jg.SetupData)
    96  	if err != nil {
    97  		return nil, "", err
    98  	}
    99  	var envSet string
   100  	// Set the M2_HOME env variable if maven is used.
   101  	if jg.SetupData.BuiltTechnology.Type == coreutils.Maven {
   102  		envSet = m2HomeSet
   103  	}
   104  	return []byte(fmt.Sprintf(jenkinsfileTemplate, envSet, jg.SetupData.GitBranch, jg.SetupData.VcsCredentials.Url, ConfigServerId, serviceDetails.Url, buildToolsConfigCommands, jg.SetupData.RepositoryName, buildCommand, ConfigServerId)), JenkinsfileName, nil
   105  }