PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label jenkins-pipeline. Show all posts
Showing posts with label jenkins-pipeline. Show all posts

Tuesday, December 13, 2022

[FIXED] How to use parameter to skip a stage of Jenkins declarative pipeline?

 December 13, 2022     jenkins-pipeline, syntax     No comments   

Issue

I'm trying to create a Jenkinsfile that runs a release build step only if a boolean parameter isRelease is true, otherwise skip this step.

My Jenkinsfile looks like this (extract):

pipeline {
    agent { label 'build' }
    tools {
        jdk 'OpenJDK 11'
    }
    parameters {
        booleanParam( name: 'isRelease', description: 'Run a release build?', defaultValue: false)
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean compile'
           }
        }
        stage('Test') {
           steps {
                sh 'mvn test'
           }
        }
        stage('Release') {
            when {
                beforeAgent true
                expression { return isRelease }
            }
            steps {
                sh 'echo "######### Seems to a release!"'
            }
        }
    }       
}

However, I don't seem to understand how to use the parameters variable properly. What happens is that the release step is always executed.

I changed expression { return isRelease } to expression { return "${params.isRelease}" } which did not make a difference. Changing it to expression { return ${params.isRelease} } causes the step to fail with java.lang.NoSuchMethodError: No such DSL method '$' found among steps.

What's the right way to use a parameter to skip a step?


Solution

You were closest on your first attempt. The latter two failed because:

  • Converting a Boolean to a String would always return truthiness because a non-empty String is truthy.
  • That is invalid Jenkins Pipeline or Groovy syntax.

The issue here with the first attempt is that you need to access isRelease from within the params object.

when {
  beforeAgent true
  expression { params.isRelease }
}

The when directive documentation actually has a very similar example in the expression subsection for reference.



Answered By - Matt Schuchard
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, September 28, 2022

[FIXED] How can I use Jenkins to deploy a project that depends on another project on GIT?

 September 28, 2022     continuous-deployment, continuous-integration, jenkins, jenkins-pipeline, jenkins-workflow     No comments   

Issue

I am absolutly new in Jenkins and I have the following problem.

I have 2 jobs representing 2 different projects which are related to each other.

In particular I have the following situation:

I have a main project named main-project: this is a Java EE project. Then I have a Flex project named client-project that represent the web client of the previous project.

Both these project are stored in a specific GIT repository (so I have a specific GIT repository for the main-project Java EE project and another specific repository for the client-project).

The 2 projects (on my local machine) are related in the following way:

The main-project contains the .swf file that represent the compiled version of the client-project.

Something like this:

\main-project\src\main\webapp\Main.swf

So as you can see into the \src\main\webapp** directory of my **main-project is manually putted the Main.swf file that represent the compilation of the Flex client-project.

Ok, so my problem is: I have 2 Jenkins jobs related to these project.

1) main-project-job: that is the Jenkins job that compile and deploy on the srver the main-project project.

2) client-project-job: this job I think should do nothing (it only retrieve the latest compiled version of the client-project project.

I have to automate the building process in this way:

After that a dveloper push on GIT a new version of the main-project project the main-project-job compile and deploy it on server. When this job ends start the client-project-job that replace the Main.swf file into the **\DEPLOYED-PROJECT\src\main\webapp** path on my deployed project on the server.

How can I do something like this using Jenkins? Is it a neat solution to keep synchronized the latest version of the main-project and of the client-project?


Solution

Here a few leads that you might want to explore.

1. Deploy main-project-job with the embedded swf

As your main-project seems to depend heavily on your swf file (it should probably not be deployed without the swf file), can't you just get and put the swf file as part of the compiled main-project artifact ? It would seem logical to mee that your deployed main-project already contains the swf file at the time it is deployed.

With Jenkins pipelines, something like this would be pretty easy to do :

stage("Build main project") {
    // your build step
}

stage("Copy client project to main webapp") {
    git 'your-git/client-project'
    sh "cp client-project/generated-swf /main-project/src/main/webapp/Main.swf"
}

stage("Deploy main project") {
    // your deploy step
}

2. Copy swf file as part of the main job

If you cannot integrate the swf file directly to the main-project before deploy, you could still use the same job to do that, again if the main project heavily depends on the swf file, you probably want to copy it as part of the job. In this case, only the copy step will change and you will need to use some scp command to copy the swf file to the deployed app, on the distant server.

Something along these lines :

stage("Build main project") {
    // your build step
}

stage("Copy client project to main webapp") {
    git 'your-git/client-project'
    sh "scp client-project/generated-swf user@deployServer:/main-project/src/main/webapp/Main.swf"
}

stage("Deploy main project") {
    // your deploy step
}

3. Copy swf file as part of a separate job

If you are sure that you want to make the copy action as part of another job (let's say, if you sometimes needs to manually trigger the job to copy the modified swf file in the running webapp, on the server), you can just trigger another job from the main-project-job :

main-project-job

stage("Build main project") {
    // your build step
}

stage("Deploy main project") {
    // your deploy step
}

stage("Copy client project to main webapp") {
    build job: copy-client-project
}

copy-client-project

stage("Copy client project to main webapp") {
    git 'your-git/client-project'
    sh "scp client-project/generated-swf user@deployServer:/main-project/src/main/webapp/Main.swf"
}

One last detail: if you do decide to use a separate job for copy, I would recommend not calling the job "client-project-job" which often implies a standard build/deploy job, instead I would name it "copy-client-files" or something similar.



Answered By - Pom12
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, September 27, 2022

[FIXED] How to trigger a jenkins pipeline from multiple repository

 September 27, 2022     continuous-deployment, continuous-integration, git, jenkins, jenkins-pipeline     No comments   

Issue

I am relatively new to jenkins and I am working on a big project that pulls from multiple repo to build. I wrote a declarative pipeline with shell commands that I use that pulls from the required repos and build the project and everything are working but I want to connect this pipeline to all these repos so every time that there is a new commit or merge request, that triggers jenkins and starts this pipeline and then based on the build result I tag the git. I know how to do this for one repo but I don't know how to do it for multiple repo.


Solution

Because of our network infrastructure I couldn't use webhook to trigger jenkins but the solution that I am using now is I created a runner for each repo and I wrote a curl command to trigger jenkins with so each time that there is a new commit to each of those repos the runner starts a new job, executes that curl command and triggers my jenkins job.

This is the curl command that I am using in case someone needed it:

curl -i -X POST --user [JENKINS_USERNAME]:[JENKINS_PASSWORD] 'http://[JENKINS_IP]:[JENKINS_PORT]/job/[JENKINS_JOB_NAME]/build?token=[TOKEN_GENERATED_INSIDE_JENKINS]&cause=[ADDITIONAL_INFORMATION_THAT_YOU_WANT_TO_PRINT]'


Answered By - AVarf
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to update the content of an existing yaml file with Pipeline Utility Steps plugin

 September 27, 2022     build, continuous-deployment, continuous-integration, jenkins, jenkins-pipeline     No comments   

Issue

In my jenkins pipeline I am reading data stored in yaml file using Pipeline Utility Steps plugin

I can read data from file, now I want to update the value and write it back to the file, like this:

pipeline {
agent any

stages {

    stage('JOb B ....'){
        steps{
            script{
               def datas = readYaml file:"${WORKSPACE}/Version.yml"
               echo datas.MAJOR_VERSION //output is 111

               datas = ['MAJOR_VERSION': '222']
               writeYaml file:"${WORKSPACE}/Version.yml", data: datas
            }
        }//steps
    }//stage

}//stages

}//pipeline

But I am getting error - Version.yml already exist:

java.nio.file.FileAlreadyExistsException: /var/lib/jenkins/workspace/t-cicd-swarm-example_hdxts-job-B/Version.yml already exist.
at org.jenkinsci.plugins.pipeline.utility.steps.conf.WriteYamlStep$Execution.run(WriteYamlStep.java:175)
at org.jenkinsci.plugins.pipeline.utility.steps.conf.WriteYamlStep$Execution.run(WriteYamlStep.java:159)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

It seems it can only write a new file and it cannot overwrite the existing file. How to update the content of an existing yaml file from my script shown above?


Solution

It looks like you need to delete or rename the original file before you overwrite it because the writeYaml method doesn't have an overwrite flag.

sh '''
  if [ -e Version.yaml ]; then
    rm -f Version.yaml
  fi
'''


Answered By - Rich Duncan
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, September 26, 2022

[FIXED] How can I create parallel stages in Jenkins scripted pipeline?

 September 26, 2022     continuous-deployment, continuous-integration, groovy, jenkins-groovy, jenkins-pipeline     No comments   

Issue

I am trying to implement parallelization in my Jenkins pipeline code where I can run two stages in parallel. I know this is possible in declarative pipeline, but I am using scripted pipeline.

I've attempted to implement this by doing something like this:

parallel(
    stage('StageA') {
        echo "This is branch a"
    },
    stage('StageB') {
        echo "This is branch b"
    }
  )

When I run this and look at this in Blue ocean, the stages do not run in parallel, but instead, StageB is executed after StageA. Is it possible to have parallel stages in scripted jenkins pipeline? If so, how?


Solution

Try this syntax for scripted pipeline:

            parallel(
                    "StageA": {
                        echo "This is branch a"
                    },
                    "StageB": {
                        echo "This is branch b"
                    }
            )

It should look like this in Blue Ocean, this is what you expect right?

Parallel blue ocean

If you want to see the stages (and console output) in the classic view, you can use stage like this:

 parallel(
                        "StageA": {
                            stage("stage A") {
                                echo "This is branch a"
                            }
                        },
                        "StageB": {
                            stage("stage B") {
                                echo "This is branch b"
                            }
                        }
                )


Answered By - Unforgettable631
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] What is the best way to configure one Jenkinsfile for many repos?

 September 26, 2022     continuous-deployment, continuous-integration, jenkins, jenkins-pipeline, multibranch-pipeline     No comments   

Issue

Short explanation:

  • There are many repos in our Git
  • Each repo has it's own Jenkinsfile who has it's own separate Job at Jenkins
  • All Jenkins files are doing 99% the same thing!

What we want to achieve at the moment:

  • Build one Jenkinsfile for all repo
  • Maintain branches in between repos
  • Delete if we can the current Jenkins files of each repo and use the only generic new file.
  • Have the versatile to use and manipulate parameters so Jenkins file won't be affected by any other repo config

Solutions for now:

  1. Remove all Jenkins files in all repos
  2. Re-configure the Jenkinsfile PATH in Jenkins gui website to direct to our new file
  3. Put a config .yaml file in each repo who will contains all the relevant information of each repo (like key-value)
  4. So, when each repo will be triggered, our new Jenkinsfile will load the config file and use the parameters to proceed all the stages related to the config file.

I would be happy to hear ideas / examples / snippets from you guys! It will highly help me!

Regards

Niv


Solution

(Answering due to lack of reputation for comment. Please excuse)

Hi @n1vgabay If you are using Bitbucket for your SCM, then you may try these:

  1. create a Organization Folder/Bitbucket team Project inside Jenkins (From Jenkins --> New Item--> Organization Folder or Bitbucket team/Project)
  2. Update the config to filter all the repos (or regex them) under the Project inside your SCM. This will create all the repos as individual MB pipelines with all the branches under them as individual jobs. Also with Bitbucket Server Integration plugin, it automatically creates Webhooks for all the repos to trigger the jobs accordingly upon the events (Push, Commit, PR opened etc)
  3. Using Remote JenkinsFile provider plugin, you may choose to place your Jenkinsfile elsewhere in another Proj/repo and call them from this config.
  4. This Jenkinsfile can have all the steps you need and will run the same for all the branches which run as individual MB jobs.

More details on the same can be obtained from here.

Now if you want to use individual jenkinsfiles, then you might have to come up with having Jenkinsfiles specific to each repo which might make it complicated and your Jenkinsfile at the root folder level will have to call the Jenkinsfile present in your repo/branch level across all the repos and branches.

Hopefully this helps! :)



Answered By - Param J
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing