How to configure RedeployPublisher in jenkinsfile - jenkins

This is my jobs "RedeployPublisher" configuration in the config.xml. ("Deploy artifacts to the maven repository")
<publishers>
<hudson.maven.RedeployPublisher>
<id>snapshots</id>
<url>http://repo.internal.com/content/repositories/snapshots/</url>
<uniqueVersion>true</uniqueVersion>
<evenIfUnstable>false</evenIfUnstable>
</hudson.maven.RedeployPublisher>
</publishers>
but, now I need to cinfigure this in "scripted pipline" jenkinsfile.
Thank you!

Related

Jenkins pipeline script to publish into Jfrogartifactory

I am trying to write a pipeline script to publish *.war/*.jar file to JFrogArtifactory. I don't find any syntax for the same.
Anyone can help me out on the same.
please help me with a sample script.
JFrog has a dedicated GitHub repository with many examples for such cases.
There are Jenkins Pipelines examples there.
First, you must install Artifactory Plugin and config it in Jenkins server.
Refer: https://www.jfrog.com/confluence/display/JFROG/Configuring+Jenkins+Artifactory+Plug-in
And then try add below script to Jenkinsfile:
script {
def server = Artifactory.server '<artifactory id>'
def uploadSpec = '''{
"files": [{
"pattern": "<name of war or jar file>",
"target": "<artifactory repo>/path-to/war-or-jar/file/in-Artifactory"
}]
}'''
server.upload(uploadSpec)
}
Don't forget replace <artifactory id> <name of war or jar file> and <artifactory repo>/path-to/war-or-jar/file/in-Artifactory
More information: https://www.jfrog.com/confluence/display/JFROG/Declarative+Pipeline+Syntax
The scripted pipeline syntax for deploying war files to JFrog artifactory is :
env.ARTIFACTORY = 'True'
if(env.ARTIFACTORY == 'True')
{
stage('Deploying to Artifactory')
{
FAILED_STAGE = env.STAGE_NAME
bat 'mvn deploy'
}
}
Note :
1.) 'bat' command is for Windows batch file. If you're using Linux, replace 'bat' with 'sh'
2.) env.ARTIFACTORY is used to give you control over whether or not you want to execute this particular stage in your pipeline job. if you don't want this stage to execute, simply set env.ARTIFACTORY = 'False'
3.) Also note, you've to configure JFrog in : Manage Jenkins -> Configure system -> JFrog Platform Instances
4.) Include JFrog in your pom.xml file under distributionManagement tag.

Generate pom file for non-Maven artifact in Jenkins

I have a tar.gz artifact that gets uploaded to Artifactory using Jenkins. Since this artifact is not based on a Maven project, there is no pom file and thus no maven-metadata.xml being generated in Artifactory. The latter being very useful for versioning, etc.
Is there any way to have Jenkins generate a pom file for a non-Maven project?
With a manual deploy on artifactory you can generate a pom.xml ,unfortunately this option is not available on the jenkins artifactory's plugin, but you can maybe create you own shell script , like :
#!/bin/bash
groupId=com.mycompany
artifactId=myproject
version=1.0.0
echo '<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>'$groupId'</groupId>
<artifactId>'$artifactId'</artifactId>
<version>'$version'</version>
<description>Artifactory auto generated POM</description>
</project>' >> pom.xml
Anyway depending on your build tool and how you create the tar.gz package you can may be find more solutions ..
Regards

environmentVariables() not working for Jenkins Job DSL and Pipeline Jobs

We use Jenkins Job DSL for our CI setup. Since we are using a special command only available in the traditional Jenkinsfile syntax, we need to use a pipeline job.
Inside of the pipeline job we check out our project from Git. We are using the pipeline job for multiple projects, so we want to inject the git url into the pipeline script.
This is a short version of our script generating the pipeline job:
def createPipelineJob(def jobName, def gitUrl) {
pipelineJob(jobName) {
environmentVariables(GIT_URL: gitUrl)
definition {
cps {
script('''
node {
sh 'env | sort'
}
''')
sandbox(true)
}
}
}
}
This creates the following XML config:
<flow-definition>
<actions/>
<description/>
<keepDependencies>false</keepDependencies>
<properties>
<EnvInjectJobProperty>
<info>
<propertiesContent>GIT_URL=my-git.url</propertiesContent>
<loadFilesFromMaster>false</loadFilesFromMaster>
</info>
<on>true</on>
<keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
<keepBuildVariables>true</keepBuildVariables>
<overrideBuildParameters>false</overrideBuildParameters>
<contributors/>
</EnvInjectJobProperty>
</properties>
<triggers/>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition">
<script>
node { sh 'env | sort' }
</script>
<sandbox>true</sandbox>
</definition>
</flow-definition>
But if i run this, the GIT_URL environment variable is not listed (other environment variables are). But if i instead create the pipeline job manually with this setup, the GIT_URL environment variable is printed just fine. Creating the job manually pretty much creates the same xml configuration:
<flow-definition plugin="workflow-job#2.15">
<actions>
<io.jenkins.blueocean.service.embedded.BlueOceanUrlAction plugin="blueocean-rest-impl#1.3.1">
<blueOceanUrlObject class="io.jenkins.blueocean.service.embedded.BlueOceanUrlObjectImpl">
<mappedUrl>blue/organizations/jenkins/test-jobname</mappedUrl>
<modelObject class="flow-definition" reference="../../../.."/>
</blueOceanUrlObject>
</io.jenkins.blueocean.service.embedded.BlueOceanUrlAction>
</actions>
<description/>
<keepDependencies>false</keepDependencies>
<properties>
<com.sonyericsson.rebuild.RebuildSettings plugin="rebuild#1.27">
<autoRebuild>false</autoRebuild>
<rebuildDisabled>false</rebuildDisabled>
</com.sonyericsson.rebuild.RebuildSettings>
<EnvInjectJobProperty plugin="envinject#2.1.5">
<info>
<propertiesContent>GIT_URL=my-git.url</propertiesContent>
<secureGroovyScript plugin="script-security#1.35">
<script/>
<sandbox>false</sandbox>
</secureGroovyScript>
<loadFilesFromMaster>false</loadFilesFromMaster>
</info>
<on>true</on>
<keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
<keepBuildVariables>true</keepBuildVariables>
<overrideBuildParameters>false</overrideBuildParameters>
</EnvInjectJobProperty>
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
<triggers/>
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps#2.41">
<script>
node { sh 'env | sort' }
</script>
<sandbox>true</sandbox>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>
We are pretty lost because we are new to jenkins and this problem is holding us for days now.
Edit:
The job is generated on the jenkins master node but executed on a slave node
Jenkins 2.37.3
Environment Injector Plugin 2.1.5
Pipeline 2.5
This is more of a comment than an answer, but I modified and tested your DSL code and it works fine.
I created a DSL job using the script:
def createPipelineJob(def jobName, def gitUrl) {
pipelineJob(jobName) {
environmentVariables(GIT_URL: gitUrl)
definition {
cps {
script('''
node {
sh "echo $GIT_URL"
}
''')
sandbox(true)
}
}
}
}
createPipelineJob('new-job-2','my-git.url')
The resulting pipeline job has the same XML as the one you posted (minus the shell script), and building the pipeline job prints the value of GIT_URL.
[new-job-1] Running shell script
+ echo my-git.url
my-git.url
My recommendation:
If the short version you posted (or maybe try mine) doesn't work, I would try to see if upgrading Jenkins or the plugins makes any difference.
If the short version you posted or mine does work, maybe post the full version, perhaps there's an error there.
As it turns out the Environment Injector Plugin was not installed successfully, therefore the script did not run properly. So all i had to do was to restart Jenkins and everything worked just fine. Special thanks to Javier Garcés ensuring me that my script was indeed correct.

Token replacement in a Jenkins Config file

I have a deployment pipeline job thats in need of a deployment template file. There are some secure passwords in that file that I want to keep secure.
So I added a Config file provider plugin (v 2.13) and had placeholders in it that corresponded to global passwords. This unfortunately is not working. Just to test I had a Jenkinsfile like below
node {
checkout scm
withEnv(['INSTANCE=Something']) {
configFileProvider(
[configFile(fileId: 'prescribe', variable: 'DEPLOY_FILE')]) {
sh "echo $env.INSTANCE"
sh "cat ${env.DEPLOY_FILE}"
}
}
}
And the file with id 'prescribe' as
${branch}
${ENV, var=INSTANCE}
${ENV.INSTANCE}
${ENV,INSTANCE}
${env, var=INSTANCE}
And I tried keeping INSTANCE as also a global password, global variable.
However none of the tokens are replaced.
Any ideas what I'm doing wrong.
The only way that i get it working, was using the parameters of the job configuration.
And in the file i use this interpolation
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="overrideDefaultEndpoint" value="true" />
<add key="endpoint" value="${ENDPOINT}"/>
where ENDPOINT is the name of the job parameter.
Problem here is that Token Macro only accepts predefined env vars.
Options:
Job parameters, as stated by #Taringalio in another answer
Try and provide env vars via EnvInject plugin
Please see the related issue in the jenkins tracker https://issues.jenkins-ci.org/browse/JENKINS-39998

Jenkins's maven release plugin - How to set parent project version?

I have a multi-modules project that I want to release with Jenkins. I use Maven 3.3.1, Jenkins 1.651.3 and maven-release-plugin 0.14.0
I create one job for the parent project and one job for each sub project.
Here is the parent configuration :
pom.xml :
<groupId>parent.group.id</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<scm>
<url>http://mygitrepo/parent-project.git</url>
<connection>scm:git:git://mygitrepo/parent-project.git</connection>
<developerConnection>scm:git:http://mygitrepo/parent-project.git</developerConnection>
</scm>
Jenkins config :
When I perform maven release for the parent project, it works.
Now I do the same thing for a sub project.
pom.xml :
<parent>
<groupId>parent.group.id</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<scm>
<url>http://mygitrepo/sub-project.git</url>
<connection>scm:git:git://mygitrepo/sub-project.git</connection>
<developerConnection>scm:git:http://mygitrepo/sub-project.git</developerConnection>
</scm>
With the same Jenkins config. I got this error :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project sub-project: Can't release project due to non released dependencies :
[ERROR] parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT
The plugin doesn't seem to replace the parent version by the release version.
I read in the maven release plugin's documentation that I can use "-Dproject.dev" and "-Dproject.rel" to specify the parent version to use.
So I tried this :
-Dproject.dev.parent.group.id:parent-artifact=1.0.1-SNAPSHO
-Dproject.rel.parent.group.id:parent-artifact=1.0.0
release:clean release:prepare release:perform -X
-Dproject.dev.parent.group.id:parent-artifact:pom=1.0.1-SNAPSHOT
-Dproject.rel.parent.group.id:parent-artifact:pom=1.0.0
release:clean release:prepare release:perform -X
-Dproject.dev.parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT=1.0.1-SNAPSHOT
-Dproject.rel.parent.group.id:parent-artifact:pom:1.0.0-SNAPSHOT=1.0.0
release:clean release:prepare release:perform -X
None of this solve the problem.
How can I configure Jenkins plugin to set the parent-project version ?
As an alternative you could use the Versions Maven Plugin to set the parent version in an own build step:
mvn versions:update-parent
or to set a specific version
mvn versions:update-parent "-DparentVersion=[1.2.0]"

Resources