Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Monday, 28 August 2017

Filter Git events within a Jenkinsfile

Assuming you're using Git webhooks to trigger your Jenkins builds, let's say you want to trigger a different set of pipeline stages depending on whether the Git event is a push vs. a pull request. I.e for a push you may only wish to run static analysis and unit tests, whereas for a pull request you may want to add Selenium tests.

The following Jenkinsfile snippet will achieve this
// Import JSON Slurper
import groovy.json.JsonSlurperClassic

// Get the Git payload
def payload = new JsonSlurperClassic().parseText(env.payload)


// Now define a method to filter the event type
private String getEventType ( payload ){

   if( payload.pull_request  && payload.action.toLowerCase().contains("opened") ){
      return "pull_req"
   }

   else if( payload.ref && payload.head_commit){
      if( payload.ref.split('/')[1].toLowerCase().contains('head') ){
         return "push"
      }

      else if( payload.ref.split('/')[1].toLowerCase().contains('tag') ){
         return "tag"
      }
   }
}


// Now decide what action to take 
def eventType = getEventType( payload )
switch (eventType) {
  case "push":
    ............
    do something 
    ............
    break;

  case "pull_req":
    ............
    do something else 
    ............
    break;

  default:
      println "Git event ignored";
      currentBuild.displayName = "Git event ignored"
      
      // Tidy up the Jenkins GUI by deleting the ignored build
      def buildNumber = env.BUILD_ID
      node {
            sh "curl -X POST http://<username>:<apikey>@<jenkins host>:443/job/execute-pipeline/${buildNumber}/doDelete"
      }




 



Tuesday, 13 June 2017

Delete jenkins builds using curl


To delete a single job: 
curl -X POST http://jenkins-user:jenkins-token@hostname:port/job/execute-pipeline/26/doDelete

To delete a range of jobs: 
curl -X POST http://jenkins-user:jenkins-token@hostname:port/job/execute-pipeline/[20-30]/doDelete

This is handy for deleting a build arising from a git event you’ve chosen to ignore in your Jenkinsfile:
def buildId = env.BUILD_ID
node {
    sh "curl -X POST http://my_user:my_token@my_host:8080/job/execute-pipeline/${buildId}/doDelete"
}



Friday, 9 June 2017

storing git credentials

If you need to store git credentials so that you don't have to keep entering them when cloning a git repo over http(s)

Add the following to ~/.gitconfig
[user]
        email = bobmclarke@gmail.com
        name = Bob Clarke
[credential]
        helper = store

Then create ~/.git-credentials with the following contents
https://your-username:your-password@github.com

Now authentication to Git will work without prompting for credentials.

Another way of generating these files is to run:
git config --global user.email "bobmclarke@gmail.com"
git config --global user.name "Bob Clarke"
git config --global credential.helper store

Then push to the repo at which point you'll be prompted for your username and password. After you enter them for the first time a ~/.git-credentials file will be generated as above.

This can be useful if the developers in your team are using git as an npm repo for internal modules with something in the package.json like:
"some-npm-package-name": "git+https://github.com/bobclarke/mymodule#v1.0"

The downside is that your Git credentials will be stored in PLAIN TEXT so be aware.

Last thing to note, if you're doing this on an internal Github you may need to ignore ssl certs as follows:
git config --global http.sslVerify false