Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Wednesday, 27 October 2021

Trigger Jenkins on a Merge Request from Gitlab

 



Imagine you're using Gitlab to host your source code and want to trigger a Jenkins pipeline/job when you open a merge request on specific branches. In this guide we'll explain all that you need to do that.

Before starting, we recommend you to read our guide on the basic connection between Gitlab and Jenkins, explaining all the prerequisites you need to setup to go on (user credentials and permissions).


Now, let's pretend we want to trigger the pipeline on a MR from feature/* to develop.

1) Install the "Generic Webhook trigger" plugin on Jenkins

2) In your pipeline configuration section, enable the plugin:


3) In Post Content Parameters section, you have to add these variables:

Variable

Expression

branch

$.object_attributes.source_branch

merge_status

$.object_attributes.state

body

$

For example, for the branch variable, you should have something like this:


4) In Token section, insert a token of your choice.

5) In Optional filter section, insert the following:



6) Go to Gitlab, inside your Repository -> Settings -> Integrations

In the URL section insert: https://<jenkins_url>/generic-webhook-trigger/invoke?token=<your_token>

and below, enable the flag:  "Merge request events"


Done!

You can test if your job/pipeline starts clicking on Test button, and choosing Merge Request events.



Monday, 25 October 2021

Get last commit changed files in Jenkins Pipeline




To get all the modified files when starting a Jenkins Pipeline, you've just have to do 2 steps.


1. Add to the end of your scripted pipeline the following function:

 @NonCPS
def getChangedFilesList() {
    changedFiles = []

for (changeLogSet in currentBuild.changeSets) {

for (entry in changeLogSet.getItems()) { //for each commit in detected changes

for (file in entry.getAffectedFiles()) {

if (!changedFiles.contains(file.getPath())) {
// do not add duplicates

changedFiles.add(file.getPath())

}
}
}
}

return changedFiles
}


2. Call the function in a stage, after cloning the repo of your source code, to get the list of changed files (with their relative paths):

stage('Check environment') {
    steps {
        script {
            modified_files = getChangedFilesList()
            if ("myfile.txt" in modified_files) {
                echo "The file has been changed"
            } else {
                echo "The file has not been changed"
}
}
}
}

That's all!

Friday, 23 October 2020

Start a pipeline from another in Jenkins


Suppose you want to start automatically a Jenkins Pipeline from another.
Besides, suppose you want to pass some variables from the first to the second one.
How can you do it?

Example


You have Pipeline 1 and Pipeline 2, and want to start Pipeline 2 from Pipeline 1





Pipeline 1

def params=[]

pipeline {
    agent any

    stages {
        stage('Start pipeline2') {

            script {
                
                params.add(string(name: "field1", value: "value1")
                params.add(string(name: "field2", value: "value2")

                build(job: "path/to/pipeline2", parameters: params)
            }
}
    }


Pipeline 2

pipeline {
    agent any

    stages {
        stage('Read parameters') {

            script {
                
                for (p in params) {
                    print p.key
                    print p.value
            }
            }
}
    }
}


Friday, 16 October 2020

Validate a Jenkinsfile before running the Pipeline

 


Jenkins can validate a Declarative Pipeline from the command line before actually running it. This can be done using a Jenkins CLI command or by making an HTTP POST request with appropriate parameters.

Example

Suppose you have a simple Jenkinsfile-test like this:

pipeline {
    agent any
    stages {
        stage('List files') {
            steps {
            sh 'ls -al'
            }
        }
    }
}

And you want to check if the sintax is correct before using it.

Validation

All you have to do is to execute this command:
curl -s -X POST -F "jenkinsfile=<Jenkinsfile-test" \
https://user:password@jenkins.example.com/pipeline-model-converter/validate
Note that user and password are the credentials you usually use to authenticate in Jenkins web console.

Output

✅If it's all ok, you should get:
Jenkinsfile successfully validated.

❌If there are some errors, then you should get an error message.
For example, let's remove the steps section in the stage.

Trying to validate again Jenkinsfile-test, we get:

Errors encountered validating Jenkinsfile:
WorkflowScript: 13: Unknown stage section "sh". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 13, column 9.
           stage('List files') {
           ^

WorkflowScript: 13: Expected one of "steps", "stages", or "parallel" for stage "List files" @ line 13, column 9.
           stage('List files') {
           ^


Monday, 20 July 2020

Automatically trigger Jenkins Pipeline on Gitlab push



Imagine you have your git repository hosted on Gitlab, and want to automatically trigger a Jenkins pipeline to build your code when you push on a given branch, for example "develop".

Here are the steps to follow.


1) Download Gitlab plugin for Jenkins.



2)Create a new pipeline on Jenkins.

In this example we named the pipeline "my-pipeline"

When configuring the pipeline, set a webhook on Jenkins as shown here:

































3) Set Jenkins Integration in Gitlab.

Go in your project repository, and configure the integration like here:





























As you can see, the Secret token is the same shown on Jenkins Build Trigger.


4) Create a Jenkins user on Gitlab to clone the repository.

Create the user and then add it as a Developer in your project.






5) Create a Credential set for Jenkins user on Jenkins server.

In Jenkins, go to Credentials, and add a new Credential like this:














6) Add a clone step to your pipeline using the credential just created.

stage('Clone sources') {
  steps {
    git credentialsId: 'gitlab-jenkins-user', branch: 'develop', url: 'https://my.gitlab.org/test.git'
  }        
}


Wednesday, 8 July 2020

Save artifacts in Jenkins Pipeline

Advocacy and Outreach

Built artifacts from Jenkins can be downloaded for local analysis and investigation, in case of tests failures. This can be achieved thanks to Jenkins’s built-in support for storing "artifacts", that is files generated during the execution of the Pipeline.


This is easily done with the archiveArtifacts step and a regex expression.

dir/**/*.*    -> archive all the files recursively under dir/

**/*.*                 -> archive all the files in the workspace

**/*.xml      -> archive all xml files in your workspace

dir/**/*.xml     -> archive all the xml files recursively under dir/



For example, if in your build a .dev file is created, then you can archive it doing:


 stage('Archive Artifacts'){

    archiveArtifacts artifacts: '**/*.dev'

 }



Then you can download it directly from Jenkins Web Console:



If you want to check the files on Jenkins server filesystem, then you can locate the artifacts in:
$JENKINS_HOME/jobs/<job_name>/builds/<build_number>/archive