213 shaares
5 résultats
taggé
jenkins
In a Groovy class:
private final Script script;
public MyClass(Script script) {
this.script = script;
}
public myMethod() {
this.script.env.test = "prout"
this.script.println(this.script.env.test)
this.script.properties([
this.script.parameters([
this.script.string(defaultValue: "${MY_VALUE}", description: 'the value', name: "${the_name}", trim: true),
this.script.password(defaultValue: "${MY_PASSWORD}", description: 'the password', name: "${the_password}", trim: true)
])
])
}
In the pipeline:
stage('My Stage') {
steps {
script {
MyClass toto = MyClass(this);
toto.myMethod();
}
}
}
Le Jenkinsfile utilisé :
pipeline {
agent {
node { label 'With-Ansible' }
}
environment {
VM_CREDENTIALS = credentials('my-vm-credentials')
VM_USER = "${VM_CREDENTIALS_USR}"
VM_KEY = "${VM_CREDENTIALS_PSW}"
HOST_NAME = "my-vm-host"
}
stages {
stage('PING MACHINE') {
steps {
script {
println " ========== PING ${HOST_NAME} ========== "
sh "ansible all -i ${HOST_NAME}, -m ping -u ${VM_USER} --extra-vars=\"ansible_ssh_pass=${VM_KEY} ansible_connection=ssh ansible_port='22' ansible_ssh_common_args='-o StrictHostKeyChecking=no'\""
}
}
}
}
}
Il faut laisser la virgule :)
Anecdote : Pour appeler un job local, le step s'appelle "build", mais pour appeler un job distant c'est triggerRemoteJob... C'est quand même pas très homogène...
Ce job appelle un autre job en passant des paramètres :
pipeline {
stages {
...
stage('TRIGGER OTHER JOB') {
steps {
script {
println " ========== TRIGGER NEXT JOB ========== "
build job: 'other-job/master', parameters: [[$class: 'StringParameterValue', name: 'PROUT', value: "TRUC" ]]
}
}
}
}
}
Ce job récupère des paramètres :
pipeline {
parameters {
string(name: 'PROUT', defaultValue: 'VALUE', description: 'Received from previous job')
}
stages {
stage('HELLO WORLD') {
steps {
script {
println " ========== HELLO WORLD II WITH ${PROUT} ========== "
}
}
}
}
}
stage("Sonar Analysis") {
steps {
println " ========== START SONAR ANALYSIS ON SOURCES ========== "
// Prepare SonarQube scanner environment
withSonarQubeEnv(sonarqubeEnvName) {
sh "mvn -B -U -Dproject.version=${version} " +
(env.BRANCH_NAME != 'master' ? "-Dsonar.branch.name=${env.BRANCH_NAME} " : "") +
" -Dsonar.value.url=${sonarqubeserver}" +
" sonar:sonar"
}
}
}
Apparemment, faire simple c'est compliqué.
Comme j'ai pas envie de le faire plusieurs fois, je le met ici, en flat :
println " ========== START PREPARATION STEP ========== "
pom = readMavenPom file: 'pom.xml'
binaryName = pom.artifactId
version = pom.version
groupId = pom.groupId
artifactoryserver = Artifactory.newServer url: 'myURL/', credentialsId: artifactoryCredentials
artifactoryserver.bypassProxy = true
println " ========== START PREPARING RELEASING ========== "
version = version.replace("-SNAPSHOT", "")
println " release version is now ${version} "
println " ========== CLONE REPO ========== "
checkout scm
def sshFolderLocation = '/root/.ssh'
def sshBitbucketKeyTargetLocation = sshFolderLocation + '/id_rsa'
configFileProvider([configFile(fileId: 'bitbucket-ssh-key', targetLocation: sshBitbucketKeyTargetLocation)]) {
withCredentials([sshUserPrivateKey(credentialsId: gitCredentialsId, keyFileVariable: 'KEYFILEVARIABLE', passphraseVariable: '', usernameVariable: gitCredentialsId)]) {
sh "git config --global user.email \"jenkins@localhost\""
sh "git config --global user.name \"jenkins\""
sh "chmod 700 ${sshFolderLocation}"
sh "chmod 600 ${sshBitbucketKeyTargetLocation}"
def current_branch = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true)
sh "git pull origin ${current_branch}"
println " ========== BUILD PACKAGE ========== "
configFileProvider([configFile(fileId: 'MAVEN_SETTINGS_FILE', variable: 'MAVEN_SETTINGS')]) {
sh "mvn -B -U org.codehaus.mojo:versions-maven-plugin:set -DnewVersion=$version"
sh "mvn -B -U clean install -Dmaven.test.skip=true"
}
println " ========== START PUBLISHING IN DEV SPACE ========== "
publishBinaryInDevSpace(version, groupId, mavenlocaldev, binaryName, artifactoryserver, buildInfo)
sh "git add ."
sh "git commit -m \"Release version $version\""
sh "git tag $version"
def newSnapshotVersion = upgradeVersionToSnapshot(version)
configFileProvider([configFile(fileId: 'MAVEN_SETTINGS_FILE', variable: 'MAVEN_SETTINGS')]) {
sh "mvn -B -U org.codehaus.mojo:versions-maven-plugin:set -DnewVersion=$newSnapshotVersion"
}
sh "git add ."
sh "git commit -m \"Back to Snapshot version\""
sh "git push -u origin ${current_branch}"
sh "git push --tag"
println " ========== START PROMOTING STAGE ========== "
promoteArtifact(artifactoryserver, buildInfo, mavenlocaldev, mavenlocalrelease)
}
}
Avec les méthodes :
def upgradeVersionToSnapshot(currentVersion) {
println currentVersion
String[] numbersInVersion = currentVersion.split("\\.")
String minorVersion = numbersInVersion[2]
int newMinorNumber = Integer.parseInt(minorVersion) + 1
return numbersInVersion[0] + "." + numbersInVersion[1] + "." + newMinorNumber + "-SNAPSHOT"
}
def publishBinaryInDevSpace(mavenVersion, groupId, artifactoryDevSpaceName, binaryName, artifactoryserver, buildInfo) {
def packageName = groupId.replace(".", "/")
def uploadSpec = """{
"files": [{
"pattern": "target/${binaryName}-${mavenVersion}.jar",
"target":"${artifactoryDevSpaceName}/${packageName}/${binaryName}/${mavenVersion}/${binaryName}-${mavenVersion}.jar"
},{
"pattern": "pom.xml",
"target":"${artifactoryDevSpaceName}/${packageName}/${binaryName}/${mavenVersion}/${binaryName}-${mavenVersion}.pom"
}]
}"""
buildInfo = artifactoryserver.upload uploadSpec
artifactoryserver.publishBuildInfo buildInfo
}
def promoteArtifact(artifactoryserver, buildInfo, artifactoryDevSpaceName, artifactoryReleaseSpaceName) {
def promotionConfig = [
// Mandatory parameters
'buildName' : buildInfo.name,
'buildNumber' : buildInfo.number,
'targetRepo' : artifactoryReleaseSpaceName,
// Optional parameters
'comment' : 'XXXxxxXXX is promoted',
'sourceRepo' : artifactoryDevSpaceName,
'status' : 'Released',
'includeDependencies': false,
'copy' : true,
// 'failFast' is true by default.
// Set it to false, if you don't want the promotion to abort upon receiving the first error.
'failFast' : true
]
// Promote build
artifactoryserver.promote promotionConfig
}