I need to write a pipeline script so that it fetches the repository I need and the other Stages work in the context of that repository. I also need all variables to be available, such as the hash of the last commit or the project name.
In fact I need to do the same thing that happens when we select th "Pipeline script from SCM" option in job settings.
But in my case I can't set "Pipeline script from SCM" because I need to write a custom pipeline different from the one in the Jenkinsfile of the repository I need, and I don't want to create another Jenkinsfile.
And I don't exactly get how can I get env.GIT_COMMIT in this way? Below is my pipeline, which works if I select "Pipeline script from SCM" and set the desired repository. How
@Library("cnv-jenkins-sharedlib") _
pipeline {
agent {
label 'cnv-jobs'
}
options {
timestamps ()
disableConcurrentBuilds()
}
tools {
maven 'Maven_3.9.2'
jdk 'JDK11'
}
parameters {
choice(name: 'StageName', choices: ['test', 'dev' ], description: 'Choise stage')
booleanParam(name: 'deploy', defaultValue: false, description: 'Deploy')
}
environment {
registryUrl = 'cprd-doc-reg01.example.com:10012'
DOCKER_CREDS = credentials('cnv_docker_reg_creds')
ENV = "${params.StageName}"
DEPLOY_BRANCH = "test"
PROJECT_NAME = "background-osago-tarifficaiton-service"
GIT_HASH = env.GIT_COMMIT.take(7)
}
stages {
stage('prepare Environment'){
steps {
GetBuildVersion()
}
}
stage('Build and Sonar Scanner') {
steps {
MvnBuild(registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}", PROFILE: "${ENV}stand")
}
}
stage('Build image'){
steps {
cnvDockerBuild(dockerFile: "src/main/docker/Dockerfile.jvm", registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}")
}
}
stage("Deploy to K8S") {
when { expression { params.deploy } }
steps {
CnvDeployK8S(imageTag: "build-${GIT_HASH}")
}
}
}
}
I find out the answer. Below you can see what I need: