Unable to Load Jenkins Shared Libraries - jenkins

I have 2 scripts 1 is the scripted pipeline and 1 is the declarative pipeline
Scripted Pipeline (script1):
node ('Slave'){
stage('Loading app Deployment File') {
def util = load './abcd/Jenkinsfile.groovy'
}
}
Declarative Pipeline (script2)/(Jenkinsfile.groovy):
#Library('shared-library#master') _
import com.xaa.utils.myUtils
pipeline {
agent any
stages {
stage ("Say Hello World") {
steps {
helloWolrd()
}
}
}
from 1 script I am calling another, when declarative pipeline trying to load the shared Libraries I am getting this error message
ERROR: Could not find any definition of libraries [shared-library#master]
org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
Script1.groovy: Loading libraries failed
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
Any suggestions are much appreciated.

You are missing the library configuration in the Jenkins. Follow this official documentation https://www.jenkins.io/doc/book/pipeline/shared-libraries/
if it is due to the bug in the loading library as mentioned in the link please use a dedicated stage to load the library.
pipeline {
agent any
stages {
stage('init') {
scripts {
library "shared-library#master"
}
}
stage('Build'){
steps {
echo "build stage"
}
}
}
}

Related

How to use file parameter in Jenkins declarative pipeline?

I am currently using the following code to upload file in Jenkins declarative pipeline and read the content from file. But the file is not stored in the Jenkins workspace or anywhere. So, whenever I run pipelines, it shows file not found error.
I tried other ways which are available on Internet but did not get output. Can anyone suggest a proper way to upload file in Jenkins and read the data from it?
pipeline {
agent any
parameters {
file(name: 'yamlFile', description: 'Upload file test')
}
stages {
stage ("Checkout demo repo") {
steps {
script{
echo "${WORKSPACE}"
def configVal = readYaml file: yamlFile
}
}
}
}
}
I never had luck using the default File Input with a declarative Pipeline. Instead, I used the File Parameters plugin. Here is an example.
pipeline {
agent any
parameters {
base64File 'yamlFile'
}
stages {
stage('Example') {
steps {
withFileParameter('yamlFile') {
def configVal = readYaml file: yamlFile
}
}
}
}
}

Looking for a jenkins pipeline build job syntax that really works

I'm trying to trigger a jenkins pipeline job from inside another jenkins pipeline job, with parameters.
I'm relatively newbie in java/groovy so I search the web for functional samples but all that I found are unusable for syntax or scripting reasons.
Some of my tests below:
How to trigger another Jenkins pipeline that needs a $BRANCH variable?
node() {
build job: 'INVENTORIES', propagate: true, wait: true
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'build' found among steps [ansiblePlaybook
Jenkins pipeline for building other jobs
node() {
stage('Desc1') {
steps {
dir('/var/lib/jenkins/workspace') {
build job: 'INVENTORIES', propagate: true, wait: true
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps [ansiblePlaybook,
node() {
stages {
stage ("build") { //an arbitrary stage name
steps {
build 'INVENTORIES' //this is where we specify which job to invoke.
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps [ansiblePlaybook,
I've tried plenty of samples (script block, step block, stage block...) but it never works, always throwing java exception like:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.CoreStep.delegate expects interface jenkins.tasks.SimpleBuildStep but received class ...
Before I jump from a bridge, could anyone here helps me?
Thanks in advance, I know swimming but it's a little cold
[SOLVED]
It were missing a Pipeline Plugin but the error messages wasn't clear enought and log content too poor to guess.
Thanks to #zett42 to have pointed me on the good search way.
Have a nice day.

how do i use a pipeline script file in Jenkins pipeline (Pipeline tab)

Beginner in Jenkins. I have set up Jenkins on Linux server and creating a pipeline. I see in a Pipeline Tab, where it asks pipeline script or pipeline script from SCM. i m not using any SCM. i have written the sample pipeline script
pipeline {
agent {
node {
label ''
}
}
stages {
stage ('check script')
{
steps
{
script
{
println " Checking the bounce scripts of application"
}
}
}
stage ('Bounce Application')
{
steps
{
script
{
println " Bouncing the application"
}
}
}
}
}
i wanted to put the above pipeline script in a file on a Jenkins server and use/ call it here. Do we have feasibility of this?
Thanks for your help in advance.

Code execution failure for post pipeline failure

I'm building a pipeline using scripted pipeline and as I can see from https://jenkins.io/doc/pipeline/tour/post/, for declarative pipeline, we can use post actions.
Similarly I tried in declarative pipeline -
node {
stage("Stage 1") {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 1"
}
}
stage("Stage 2") {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 2"
error "failed for some reason."
}
}
stage("Stage 3") {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 3"
}
}
post {
failure {
echo "Failed stage name: ${FAILED_STAGE}"
}
}
}
But I do not see failure block of post executed even if pipeline failed. Can you please help to understand if it has some different syntax for scripted pipeline?
You are using a scripted pipeline and not a declarative pipeline (see the difference here). The syntax is completely different so you can't use a post-stage (existing for declarative pipeline) in a scripted pipeline (like your pipeline).
You have two options here.
You convert your scripted pipeline to a declarative pipeline (see syntax here) and simply use the post-stage like you have it used in your questoin. (I would prefer this option)
or
Implement the post-stage for your scripted pipeline by yourself in a finally-block (see this SO question for help)

How To Deployit configuration in jenkins pipeline as code in jenkins

i found one link in google
https://docs.xebialabs.com/xl-deploy/concept/jenkins-xl-deploy-plugin.html
here the following steps are present but it is throwing
No dsl method xldCreatePackage
node {
stage('Checkout') {
git url: '<git_project_url>'
}
stage('Package') {
xldCreatePackage artifactsPath: 'build/libs', manifestPath: 'deployit-manifest.xml', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Publish') {
xldPublishPackage serverCredentials: '<user_name>', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Deploy') {
xldDeploy serverCredentials: '<user_name>', environmentId: 'Environments/Dev', packageId: 'Applications/<project_name>/$BUILD_NUMBER.0'
}
}
You need to install the XL Deploy plugin into your Jenkins installation.
Right now you have the Pipeline plugin installed which gives you the Jenkinsfile & pipeline capability only. The XebiaLabs Jenkins plugin will take advantage of that but you need to plugin to give you the functionality you want.

Resources