项目作者: nemerosa

项目描述 :
Jenkins pipeline library using the Ontrack CLI
高级语言: Groovy
项目地址: git://github.com/nemerosa/ontrack-jenkins-cli-pipeline.git
创建时间: 2021-05-29T06:57:20Z
项目社区:https://github.com/nemerosa/ontrack-jenkins-cli-pipeline

开源协议:MIT License

下载


Ontrack CLI Jenkins Pipeline Library

Jenkins pipeline library using the Ontrack GraphQL API.

Installation

JCasC

JCasC (Jenkins Configuration as Code) is the recommended approach:

  1. globalLibraries:
  2. libraries:
  3. - name: "ontrack-jenkins-cli-pipeline"
  4. retriever:
  5. modernSCM:
  6. scm:
  7. github:
  8. configuredByUrl: true
  9. credentialsId: "<GitHub credentials>"
  10. repositoryUrl: "https://github.com/nemerosa/ontrack-jenkins-cli-pipeline"

UI

You can also use the Jenkins management UI to register this library:

Library UI

Setup

The steps provided by this library rely on environment variables and credentials.

Set the ONTRACK_URL Jenkins environment variable to the URL to Ontrack.

Either:

  • create an ONTRACK_TOKEN secret text credentials entry containing the authentication token used to connect to Ontrack
  • or reuse an existing credentials entry by referring to its ID in an ONTRACK_TOKEN_ID environment variable

By default, projects and branches managed by this pipeline steps will be linked to a GitHub configuration named github.com,
using GitHub as an issue tracker.

For other settings, use the following environment variables:

  • ONTRACK_SCM - defaults to github - type of SCM, like github or bitbucket-server.
  • ONTRACK_SCM_CONFIG - defaults to github.com - name of the SCM configuration in Ontrack.
  • ONTRACK_SCM_JIRA - using the defaults if not configured - identifier of the issue service configuration. For example, for
    Jira, it looks like jira//<name of the config>. So if your Jira configuration in Ontrack is named “MyJira”, the
    identifier will be jira//MyConfig.

Note that all these settings can be overridden at pipeline level. See ontrackCliSetup for more information.

Usage

In your Jenkinsfile, declare the version of the pipeline library you want to use and start using the steps
provided by the library. For example:

  1. @Library("ontrack-jenkins-cli-pipeline@main") _
  2. pipeline {
  3. agent any
  4. stages {
  5. stage("Setup") {
  6. steps {
  7. // Ontrack connection & branch setup
  8. ontrackCliSetup()
  9. }
  10. }
  11. stage("Preparation") {
  12. steps {
  13. // Computing a version in VERSION
  14. // Ontrack build entry creation
  15. ontrackCliBuild(release: env.VERSION)
  16. }
  17. post {
  18. always {
  19. // Validation run, including detection of the
  20. // status and run information
  21. ontrackCliValidate(stamp: 'STAMP')
  22. }
  23. }
  24. }
  25. }
  26. }

Logging

Most of the steps accept a logging parameter but logging can be configured globally using
the ONTRACK_LOGGING environment variable, set to false or true (false is the default).

Note that any logging parameter on the step will override the global settings.

Typically, the ONTRACK_LOGGING environment variable would be set at pipeline level:

  1. pipeline {
  2. environment {
  3. ONTRACK_LOGGING = true
  4. }
  5. }

Versioning

In this example, we use the main branch of the pipeline library but it’s better to stick to:

  • versioned branches, like v1
  • or explicit tags in order to ensure build reproducibility. See the list of available tags
    in GitHub.

Steps

General setup

  • ontrackCliSetup - general purpose setup task to set up Ontrack in your pipeline, from
    creating common environment variables to initializing project and branch in Ontrack for your pipeline.
  • ontrackCliSetupSonarQube - setup of SonarQube properties at project level

Creating Ontrack items

Setting properties on builds

Accessing Ontrack information

Auto versioning setup

Setup of notifications

Generic steps

  • ontrackCliGraphQL - performs a GraphQL call to Ontrack and returns the JSON response

Orchestration steps

These steps are very specific in the kind of scenario they are running and interact with more than Ontrack.

CLI steps

Those steps allow to download and the setup the Ontrack CLI.

Ignoring errors

You can run the Ontrack steps in a mode where any error is ignored.

Note that steps returning values will then return a null or empty value. This
may cause some issues in some pipelines relying on these values, so it’s not a perfect solution.

Set the ONTRACK_IGNORE_ERRORS global environment variable to true.

Failsafe

In case you want to utterly disable all the Ontrack steps, in all pipelines, to do something and in particular,
you want to remove any connection to the Ontrack server, you can set the ONTRACK_STOP global environment
variable to true.

Like when ignoring errors, the steps returning values will return a null or empty value. This
may cause some issues in some pipelines relying on these values, so it’s not a perfect solution.

Environment variables

Variable Default value Description
ONTRACK_LOGGING false Enabling by default logging for all Ontrack operations
ONTRACK_URL Required URL to the Ontrack server
ONTRACK_TOKEN Required or ONTRACK_TOKEN_ID (credentials entry) API token used to connect to Ontrack
ONTRACK_TOKEN_ID Required or ONTRACK_TOKEN ID of a text credentials entry containing API token used to connect to Ontrack
ONTRACK_STOP false See Failsafe
ONTRACK_IGNORE_ERRORS false See Ignoring errors
ONTRACK_USE_LABEL false See ontrackCliSetup
ONTRACK_SCM github See ontrackCliSetup
ONTRACK_SCM_CONFIG github.com See ontrackCliSetup
ONTRACK_SCM_ISSUES `` See ontrackCliSetup

Change log

4.9

Requires Ontrack >= 4.9

4.9.0

  • support for Ontrack subscriptions names

2.x

  • old versions not linked to a given version of Ontrack