What is the equivalent of the command tools in Jenkins scirpted pipeline? - jenkins

How does Jenkins manage plugins? Do all nodes have a set of plugins installed according to the list specified in the master?
What is the equivalent of declarative pipeline's command tools in scripted pipeline? If there isn't one, how do we use the tools like Maven, NodeJS?

If there isn't one, how do we use the tools like Maven, NodeJS?
According to the node plugin doc at https://plugins.jenkins.io/nodejs/, you could do the following:
nodejs(nodeJSInstallationName: 'Node-name') {
sh 'npm install'
}
Same for the maven plugin:
withMaven(maven: 'Jenkins Maven') {
sh 'mvn install'
}

Related

How can I reference jenkinsfile declarative tools reference in my code

I have a section at the top of my jenkinsfile for tools that manages the installation. I want to see if I can reference this values later on in my declarative code.
pipeline {
agent { label 'buildfarm' }
options { buildDiscarder(logRotator(numToKeepStr: '15')) }
tools {
maven '3.6.2'
jdk 'jdk9'
}
When I generate code for the pipeline it given me code like this ...
withMaven( maven: 'Maven 3.5.0 Linux', globalMavenSettingsConfig:
'f0b3bb88-e3b7-4ba2-a1b5-8a5ab8ce02fc', mavenLocalRepo:
'.repository'){
sh "mvn clean install -U -Dmaven.test.skip=true"
}
This code resides in a library and I want the values in the tools section of the Jenkinsfile to be able to set the values for maven version in the library declarative code. Is this possible and how?

Is there any way to convert maven project to pipeline in an automated way

Having some maven projects. I want to change it to a scripted pipeline in jenkins
To automate the following example you can use the Jenkins API via the groovy script console or an groovy system script to create you jobs programmatically.
Example for scripted Pipeline:
node{
stage ('Build') {
git url: 'https://github.com/cyrille-leclerc/multi-module-maven-project'
withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'M3',
// Maven settings.xml file defined with the Jenkins Config File Provider Plugin
// Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
mavenSettingsConfig: 'my-maven-settings',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
}
}
You need the Pipeline Maven Plugin: https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin

Scripted Jenkins file for maven package

I am writing a Jenkinsfile for Maven package by using scripted syntax
node{
stage('Checkout')
git url:'<gir url>',branch: 'master
stage('build')
sh 'mvn clean package'
}
my problem is how to declare the maven version in above Jenkinsfile (in scripted syntax only)
I just completed a major Jenkins Pipeline/Maven implementation with the Pipeline Maven Integration plugin: https://plugins.jenkins.io/pipeline-maven.
It let's you leverage Maven Tool configuration when you run Maven commands in your pipeline.

How to enable Yarn for Jenkinsfile Pipeline syntax?

Trying to set up a simple unit test runner with Jenkins 2 using a Jenkinsfile and Pipeline declarative syntax. The below sample just about works, but I'd like to use yarn instead of npm.
Jenkinsfile
#!groovy
pipeline {
agent any
tools {nodejs 'node-8.10.0'} // previously configured via Manage Jenkins -> Global Tool Configuration
stages {
stage('Unit') {
steps {
checkout scm
sh 'node -v' // 8.10.0
sh 'npm -v' // 5.6.0
sh 'npm install' // <-- desired change: 'yarn install'
sh 'npm run test:unit' // <-- desired change: 'yarn test:unit'
}
}
}
}
Bonus question: is checkout scm really required? Adding it appears to cause it to run twice.
You can set yarn as a installable dependency in the node tool configuration:
After defining the NodeJS tool, you can declare which global packages you want to install.
You will find it in the Global Tool Configuration menu in the Manage Jenkins section.
Each time your pipeline's is builded, the tool will provide a NodeJS environment with yarn installed.
There is no yarn plugin for Jenkins as far as I know. So there is no yarn tool which can be easily used in a Pipeline and will take care of the yarn installation.
So here are some other possibilities:
You can install yarn locally on Jenkins and use sh 'yarn install' in the pipeline. See https://yarnpkg.com/en/docs/install#alternatives-stable for a list of possible options how to install it. Some steps can easily be scripted in a pipeline like the curl solution.
Or you can install yarn via the npm provided in the pipeline:
sh "npm install -g yarn"
sh "yarn install"
Or if you are using Java and Maven you can use the frontend-maven-plugin
to install yarn via Maven (which has a tool blog in pipelines) and then use the installed yarn by this plugin.
Build inside a docker container the node container has yarn already installed
pipeline {
agent {
docker { image 'node:8.11' }
}
stages {
stage('Test') {
steps {
sh 'yarn install'
}
}
}
}
And like you observed the checkout is redundant. A declarative pipeline will check out the code and the Pipeline script in a special pre step before your steps.

Auto generate build pipeline for gradle build using Jenkinsfile

I am trying to create a build pipeline based upon the Gradle tasks. I have viewed JenkinsFile configuration Pipeline-as-code-demo but I am unable to create a pipeline for gradle tasks. Please suggest me a possible way so that I can use the Jenkinsfile to automatically show the build pipeline just by reading the configurations from the Jenkinsfile.
Thankyou
In case your project uses Gradle Wrapper you can use the following snippet in your Jenkinsfile:
stage('Gradle Build') {
if (isUnix()) {
sh './gradlew clean build'
} else {
bat 'gradlew.bat clean build'
}
}
If you checkout to subdirectory sub-dir you might want to use
stage('Gradle Build') {
if (isUnix()) {
dir('sub-dir') {sh './gradlew clean build'}
} else {
dir('sub-dir') {bat 'gradlew.bat clean build'}
}
}
In case you're using Artifactory to resolve your build dependencies or to deploy your build artifacts, it is recommended to use the Pipeline DSL for Gradle build with Artifactory.
Here's an example taken from the Jenkins Pipeline Examples page:
node {
// Get Artifactory server instance, defined in the Artifactory Plugin administration page.
def server = Artifactory.server "SERVER_ID"
// Create an Artifactory Gradle instance.
def rtGradle = Artifactory.newGradleBuild()
stage 'Clone sources'
git url: 'https://github.com/jfrogdev/project-examples.git'
stage 'Artifactory configuration'
// Tool name from Jenkins configuration
rtGradle.tool = "Gradle-2.4"
// Set Artifactory repositories for dependencies resolution and artifacts deployment.
rtGradle.deployer repo:'ext-release-local', server: server
rtGradle.resolver repo:'remote-repos', server: server
stage 'Gradle build'
def buildInfo = rtGradle.run rootDir: "gradle-examples/4/gradle-example-ci-server/", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish'
stage 'Publish build info'
server.publishBuildInfo buildInfo
}
Otherwise, you can simply run the gradle command with the sh or bat Pipeline steps.
In jenkins you can creates a jenkins pipeline using a script which is written in Jenkinsfile.
We write a script using 'stages' and 'node' as building block, these building blocks allow you to specify instructions that should be executed as part of jenkins pipeline.
To execute gradle build using JenkinsFile first check for Operating system and call appropriate shell that can execute that gradle task, as below:
Jenkinsfile
stage 'build_Project'
node{
if(isUnix()){
sh 'gradle build --info'
}
else{
bat 'gradle build --info'
}
}
Above code snippet create a stage with name build_project and execute gradle build script of the current project.

Resources