Giter Site home page Giter Site logo

pepcitron / hesperides-jenkins-lib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sncf-connect-tech/hesperides-jenkins-lib

0.0 3.0 0.0 373 KB

Shared lib for Jenkins pipelines to interact with Hesperides

License: GNU General Public License v3.0

Groovy 100.00%

hesperides-jenkins-lib's Introduction

This is a Groovy shared lib for Jenkins 2 pipelines, but also useable as a Groovy command-line script.

It lets you interact with Hesperides to perform various tasks:

  • platform and module creation
  • module release
  • update of platform version / modules versions
  • update of properties per platform/instance of a platform/module of a platform from a JSON file

Summary

  1. Installation
  2. Usage
  3. Tests
  4. Development

Installation

In "Manage Jenkins > Configure System" aka /jenkins/configure, adds the git@... URL to this repo in Global Pipeline Libraries, and ticks the "load implicitly" checkbox. You can use either the master branch to use the latest version, or specify a tag to ensure more stability.

You will also need to install the http_request Jenkins plugin from its .hpi.

Usage

Jenkins pipeline

cf. Jenkinsfile & vars/*.txt documentation files for examples.

Note: to check your Jenkinsfiles syntax, use a linter ! cf. https://github.com/Lucas-C/pre-commit-hooks#other-useful-local-hooks

CLI script for the standard Groovy interpreter

@GrabResolver(name='nexus', root='http://nexus.mycompany.com/content/repositories/jenkins-ci/repo.jenkins-ci.org/public')
@Grab(group='com.cloudbees', module='groovy-cps', version='1.12')
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2')

import static groovy.json.JsonOutput.*
import com.vsct.dt.hesperides.jenkins.pipelines.Hesperides
import com.vsct.dt.hesperides.jenkins.pipelines.http.HTTPBuilderRequester

def cli = new CliBuilder()
cli.apiRootUrl(args:1, argName:'endpoint', 'Default: https://hesperides.mycompany.com')
cli.auth(args:1, required:true, argName:'auth', 'user:password')
cli.app(args:1, required:true, argName:'trigram', '')
cli.platform(args:1, required:true, argName:'instance', '')
def options = cli.parse(args)
if (options) {
    def hesperides = new Hesperides(apiRootUrl: options.apiRootUrl, auth: options.auth, httpRequester: new HTTPBuilderRequester())
    def platformInfo = hesperides.getPlatformInfo(app: options.app, platform: options.platform)
    System.out.println prettyPrint(toJson(platformInfo))
}

Tests

The tests require the $HESPERIDES_HOST environment variable to be set, including the protocol. An optional $HESPERIDES_PORT can also be specified, along with $HESPERIDES_AUTH as <USERNAME>:<PASSWORD>.

gradle test

To run a single test:

gradle -Dtest.single=HesperidesIntegrationSpec test

⚠️ WARNING: Integration tests perform modifications on the target Hesperides instance

The test report is generated in build/reports/tests/test/index.html.

With docker-compose

Integration tests use a dockerized Hesperides instance.

docker-compose build
docker-compose run gradle-test

If you want to only use Docker to launch an Hesperides instance:

docker-compose up -d hesperides
HESPERIDES_HOST=http://localhost
HESPERIDES_PORT=8080
gradle test

Development

HTTPRequester

Because we want this library to be usable both with the standard Groovy interpreter and the jenkins groovy-cps plugin, we faced the challenge of making HTTP requests in both contexts:

  • in Jenkins pipelines, the recommended solution is to use the non-builtin http_request plugin. Another, more hacky approach, would be to use the sh step + curl.

  • with the Groovy standard interpreter, groovyx.net.http.HTTPBuilder is a very common library to make HTTP calls

Both are based on org.apache.httpcomponents.httpclient.

In order to use either one dependeing on the execution context, we created the com.vsct.dt.hesperides.jenkins.pipelines.http package to abstract this into an HTTPRequester interface:

Release & upload to Nexus

  1. git tag & git push --tags
  2. modifify the build.gradle according to this tag
  3. set the NEXUS_URL / NEXUS_USER / NEXUS_PASSWORD environment variables
  4. gradle upload

Coding style

Use the CodeNarc Groovy linter:

gradle check
  • following Robert C. Martin "Clean Code" recommendations, we avoid methods with too many parameters. We use named-parameter with Map args, and validate necesseray parameters with _required.
  • do NOT @Grab in source files under src/, it makes the code non-testable

Known issues with Jenkins Pipeline

‼️ ⚠️ ‼️

Other known issuess

javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair exception when using http_request plugin : you need to run Jenkins with Java 8, a bug with Java 7 will prevent you from making HTTPS requests.

cf. http://vboard.vsct.fr/vblog/?p=561

Debugging Grapes resolution

Deleting an artifact in both Groovy & Maven caches, to test re-downloading (yes, Groovy will use Maven cache by default !) :

rm -r %HOME%\.groovy\grapes\com.cloudbees\groovy-cps %HOME%\.m2\repository\com\cloudbees\groovy-cps

Rerunning a script with increased logging:

set CLASSPATH=src
groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=3 vars/getHesperidesPlatformInfo.groovy --app CSC --platform USN1

Look for strings like "downloading https://jcenter.bintray.com/com/cloudbees/groovy-cps/1.12/groovy-cps-1.12.jar".

To disable default groovy resolvers (like jcenter), you need to create a ~/.groovy/grapeConfig.xml file based on the default one, then remove the resolver entries you don't want.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.