Jenkins Pipeline Groovy script. Script not found in workspace - jenkins-pipeline

The Jenkins is installed at Ubuntu. The Jenkins workspace is at /var/lib/jenkins/workspace. The github repo is at https://github.com/garyyang6/scripts. There are three scripts in the repo, build.sh, deploy.sh and quality.sh. The pipeline groovy script is as follow. After I ran the groovy script in Jenkins, it complains build.sh: not found.
At /var/lib/jenkins/workspace/Scripted_Pipeline_GitHub, I found the scripts, build.sh deploy.sh quality.sh. The folder /var/lib/jenkins/workspace/Scripted_Pipeline_GitHub#tmp is empty.
pipeline {
agent any
stages {
stage('Git-Checkout') {
steps {
echo 'Checking out from Git Repo';
git 'https://github.com/garyyang6/scripts.git'
}
}
stage('Build') {
steps {
echo "Building the checked out project";
sh 'build.sh'
}
}
stage('Unit-Test') {
steps {
echo "Running JUnit Tests";
}
}
stage('Quality-Gate') {
steps {
echo "Verifying Quality Gates";
sh 'quality.sh'
}
}
stage('Deploy') {
steps {
echo "Deploying to Stage Environment for more tests";
sh 'deploy.sh'
}
}
}
}
Erors:
Running on Jenkins in /var/lib/jenkins/workspace/Scripted_Pipeline_GitHub
/var/lib/jenkins/workspace/Scripted_Pipeline_GitHub#tmp/durable-9a8651c4/script.sh: 1: build.sh: not found

Related

Jenkins on Localhost with Gradle Wrapper (Access Denied) Error

Following is my Machine details
Specification
Version
OS
Windows-11
Java
11
Gradle
7.3
I had setup of shell on Jenkins as well.
And Jenkins code snippet is as follows:
#!/usr/bin/env groovy
/* groovylint-disable CompileStatic, DuplicateStringLiteral, UnnecessaryGString, VariableName */
import java.text.SimpleDateFormat
pipeline {
agent any
stages {
stage('build-and-package') {
steps {
script {
sh "git update-index --chmod=+x gradlew"
sh "chmod +x gradlew"
sh "./gradlew clean assemble --stacktrace"
//sh "gradle clean build"
sh "ls -lrt"
sh "ls -lrt ./build/libs/"
}
}
}
stage('docker-build-and-push') {
steps {
//Code Commented
}
}
stage('helm-deploy') {
steps {
script {
//Code Commented
}
}
}
}
}
I have tried to provide full access rights to Jenkins workspace
Error Screen shot

JenkinsFile Execution for Dynamic Command via Pipeline

pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh migrateOut --argFile sourcearg.txt --encassAsPolicyDependency --dest OTK.xml --folderName /OTK'
}
}
}
}
}
I would like to call this part of command "migrateOut --argFile sourcearg.txt --encassAsPolicyDependency --dest OTK.xml --folderName /OTK" from file placed in jenkins workspace.
TIA

Require Jenkins Pipeline script for Maven build and Maven Test from Pom.xml?

I am new to Jenkins and I have created the Maven+TestNG project in eclipse I which I have executed the sample test case to print hello world from POM.xml,which is running fine. I have pushed the project in GIT as well Now I need to execute this from Jenkins using Pipeline script for CI/CD.
I have created below Pipleline script in Jenkins pipeline tab but I am getting error which executing the script. Can you please guide me or provide me the tutorial link where I can get Jenkins pipeline example.
The script is as follow:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jitsolution19/RestAssured_Framework.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'Maven'
}
stage('Build') {
// Run the maven build
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
stage('Test') {
//Perform test
echo 'Execute the script'
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore test"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore test/)
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
}
}

Does shell scripts in Jenkins pipeline run asynchronously?

I have a pipeline for building my android application. In some stages, I have shell scripts. when I run the stages one by one(by commenting others) everything works fine, but when I run them all together I see strange behavior.
It seems that the shell scripts are running in parallel !!
here is my jenkinsfile :
pipeline{
agent any
stages{
stage("Clean"){
agent{
node{
label 'master'
customWorkspace getMainDirectory()
}
}
steps{
sh """#!/bin/bash
rm -rf Corona
rm -rf native-corona-android
cd ..
cp -a TemplateWorkspace/. ${getCoronaBranch()}-${getNativeBrach()}
"""
}
}
stage("pull native repo"){
agent{
node{
label 'master'
customWorkspace getNativeProjectPath()
}
}
steps{
echo "pulling native"
git(
url: nativeRepositoryAddress,
credentialsId: credentialsId,
branch: getNativeBrach()
)
echo "pulling done"
}
}
stage("pull corona repo"){
agent{
node{
label 'master'
customWorkspace getCoronaProjectPath()
}
}
steps{
echo "pulling corona"
git(
url: coronaRepositoryAddress,
credentialsId: credentialsId,
branch: getCoronaBranch()
)
echo "pulling done"
}
}
stage("build"){
environment {
docDir = getMainDirectory()
ANDROID_HOME = getAndroidSDKLocation()
}
agent{
node{
label 'master'
customWorkspace getNativeProjectPath()
}
}
steps{
sh """#!/bin/bash
./gradlew clean
./gradlew changeFiles --stacktrace --no-daemon
./gradlew assembleDebug --stacktrace --no-daemon
"""
}
}
stage("move build files"){
agent{
node{
label 'master'
customWorkspace getGradleBuildLocation()
}
}
steps{
sh """#!/bin/bash
yes | cp -rf * ../../../../JenkinsBuilds/${getOutputFolder()}/
"""
}
}
}
}
I just want to run steps synchronous(and of course shell scripts), What is my problem?
Here is what I see:
In "Clean" step the folders get deleted and a fresh copy of Template folders get copied to work directory. Steps "pull native repo" and "pull corona repo" do the jobs they should do. but in step "build" I can see that a part of "native-corona-android" files is gone and "gradlew" script is deleted. I have also seen situations where the whole "native-corona-android" folder gets deleted. Then I thought that the script in "Clean" step is called again.
Thanks
Unless you are using parallel directive all steps should be running synchronously.
What behavior do you observe? Which steps are running parallel?

Referencing variable in declarative jenkins pipeline

I am using the groovy below to call a bat command, that no matter how i reference the LOCAL_WORKSPACE within the bat command it does not evaluate it.
What am i missing?
Error
nuget restore $env.LOCAL_WORKSPACE
"Input file does not exist: $env.LOCAL_WORKSPACE"
Script
pipeline {
agent any
stages {
stage('Clone repo') {
steps {
deleteDir()
git branch: 'myBranch', changelog: false, credentialsId: 'myCreds', poll: false, url: 'http://myRepoURL'
}
}
stage ("Set any variables") {
steps{
script{
LOCAL_BUILD_PATH = "$env.WORKSPACE"
}
}
}
stage('Build It, yes we can') {
parallel {
stage("Build one") {
steps {
echo LOCAL_BUILD_PATH
bat 'nuget restore %LOCAL_WORKSPACE%'
}
}
}
}
}
}
You cannot set variables to share data between stages. Basically each script has its own namespace.
What you can do is use an environment directive as described in the pipeline syntax docs. Those constants are globally available, but they are constants, so you cannot change them in any stage.
You can calculate the values though. For example I use an sh step to get the current number of commits on master like this:
pipeline {
agent any
environment {
COMMITS_ON_MASTER = sh(script: "git rev-list HEAD --count", returnStdout: true).trim()
}
stages {
stage("Print commits") {
steps {
echo "There are ${env.COMMITS_ON_MASTER} commits on master"
}
}
}
}
You can use environment variables to store and access to/from stages. For example, if you define LOCAL_ENVR as Jenkins parameter, you can manipulate the variable from stages:
stage('Stage1') {
steps {
script{
env.LOCAL_ENVR = '2'
}
}
}
stage('Stage2') {
steps {
echo "${env.LOCAL_ENVR}"
}
}

Resources