Skip to content

Jenkins shared library to interact with Jira from pipeline

Notifications You must be signed in to change notification settings

apwide/apwide-jenkins-shared-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jenkins shared library for Jira and Apwide Golive

Your are at the right place if you use Jira + Apwide Golive + Jenkins and if you love automation!

You should use this open source Jenkins Shared Library to easily exchange information between Jenkins, Jira and Apwide Golive.

If you prefer examples over documentation, jump directly to the pipeline examples library and come back here later.

Pre-requisites

Get Started

  1. Import the Jenkins Shared Library
  2. Create your first Hello World pipeline:
Push environment's deployment information to Apwide Golive
steps {
    apwSendDeploymentInfo(
        jiraBaseUrl: 'http://admin:admin@mycompany.com/jira',
        environmentId: 23, // since Golive 9.+, id of environments can be found in UI or by API
        version: '0.0.1-SNAPSHOT'
    )
}

In this example script, we have set:

  • jiraBaseUrl, user and password to connect to Jira with Apwide Golive (keep reading to learn how to get rid of these ugly hard coded values...)
  • deployed version of the environment with id=28 to "0.0.1-SNAPSHOT"

N.B. Build number and the history of Jira tickets found in the latest commits are also automatically pushed to Golive by the new apwSendDeploymentInfo step

A bit cleaner

You can use Jenkins credentials instead of hard coding user/password in your pipeline. Usage of predefined global variables also makes your pipeline more readable:

environment {
    APW_JIRA_BASE_URL = 'http://mycompany.com/jira'
    APW_JIRA_CREDENTIALS_ID = 'jira-credentials'
    APW_ENVIRONMENT_ID = 28
}
steps {
    apwSendDeploymentInfo version: '0.0.1-SNAPSHOT'
    apwSetEnvironmentStatus status: 'Up'
}

Much more concise, isn't it ?

Using Jenkins variable is very powerful. Learn more how use them at different levels:

More powerful

You just need a single step to check the url of all your Apwide environments:

environment {
    APW_JIRA_BASE_URL = 'http://mycompany.com/jira'
    APW_JIRA_CREDENTIALS_ID = 'jira-credentials'
    APW_UNAVAILABLE_STATUS = 'Down'
    APW_AVAILABLE_STATUS = 'Up'
}
steps {
    apwCheckEnvironmentsStatus
}

This single step will automatically call the url of each environment and set its status to "Up" (valid Http response) or "Down" (Http error). Quite powerful, isn't it ? ;-)

Direct calls to Jira and Apwide Golive Rest API

You can also make direct calls to any endpoints of Jira and Apwide Golive REST API using this more generic step:

steps {
    apwCallJira httpMode: 'GET', path: '/rest/api/2/project/10000'
    apwCallJira httpMode: 'POST', path: '/rest/api/2/versions', body:[:]
}

Want to talk to Golive cloud

Thanks to specific environment variables, this shared library is able to talk to Golive Cloud. To do that:

environment {
    APW_GOLIVE_CLOUD_CREDENTIALS_ID = 'golive-cloud-credentials'
    APW_UNAVAILABLE_STATUS = 'Down'
    APW_AVAILABLE_STATUS = 'Up'
}
steps {
    apwCheckEnvironmentsStatus
}

For this we've just:

  • On Golive, generated a new API token from the integrations page
  • Stored this token as a simple secret text in our jenkins credentials store
  • Filled the environment variable APW_GOLIVE_CLOUD_CREDENTIALS_ID with the credentials id used to store the token.

Note: if a Golive cloud credentials id is found by a pipeline, the steps will try to execute REST API calls on Golive cloud.

Want to talk to Jira cloud too

You can access your specific Jira cloud instance REST API with the corresponding environment variables:

environment {
    APW_JIRA_CLOUD_CREDENTIALS_ID = "jira-cloud-credentials"
    APW_JIRA_CLOUD_BASE_URL = 'https://mycompany.atlassian.net'
}
steps {
    apwCallJira httpMode: 'GET', path: '/rest/api/2/project'
}

To do so, we had to:

  • generate a new API token for Jira
  • Store the user email-address and the corresponding token as username and password secret in our jenkins credentials store
  • Filled the environment variable APW_JIRA_CLOUD_CREDENTIALS_ID with the credentials id used to store the username and token.

Note: if a Jira cloud credentials id AND a Jira cloud base url is found by a pipeline, the steps will try to execute REST API calls on Jira cloud.

More Examples

Browse our "examples" folder to get inspired and to reuse portion of scripts to write your own pipelines. They start from the easiest to the most advanced ones. A quick overview:

Environment Monitoring

Deployment tracking

  • CI Deployment workflow: how to automatically push deployment information (including the list of deployed Jira tickets!) to Golive and Jira
  • Deployment workflow: push build and deployment information to Jira and Apwide Golive

Self-Service Environments

Environment Discovery

  • Send Environment Info: discover and push environments, applications and/or categories from your pipelines.

Predefined Global Variables

To avoid duplication in your pipelines, Jenkins global variables can be set and overriden at different levels.

Here are the available predefined global variables:

Jira global variables

  • APW_JIRA_BASE_URL : Jira server/datacenter base url. (e.g. http://localhost:8080 or if you use a context http://localhost:2990/jira). Replace jiraBaseUrl parameter.
  • APW_JIRA_CREDENTIALS_ID : Id of the Jenkins credentials to use to call Jira Rest API. Replace jiraCredentialsId parameter. If not provided the shared library will look for the credentials id 'jira-credentials'
  • APW_JIRA_PROJECT : id of key of a given jira project that will be used by steps using a Jira project (ex: creation of Jira versions)
  • APW_JIRA_CLOUD_BASE_URL : For Jira cloud, the URL of the specific instance(e.g. https://mycompany.atlassian.net). Replace jiraCloudBaseUrl parameter.
  • APW_JIRA_CLOUD_CREDENTIALS_ID : For Jira cloud, the Jenkins credentials id (username/password type) to use to call Jira Cloud REST API. Replace jiraCloudCredentialsId parameter.

Note that you can also override the global variables using inline properties at step level like in this example:

environment {
    APW_JIRA_BASE_URL = 'http://mycompany.com/jira'
    APW_JIRA_CREDENTIALS_ID = 'jira-credentials'
}
def project = apwCallJira(
    jiraBaseUrl: 'http://localhost:2990/jira',
    jiraCredentialsId: 'localhost-jira-admin',
    httpMode: 'GET',
    path: '/rest/api/2/project/10000'
)

This allows you to easily deal with multiple Jira and Apwide Golive servers if required.

Apwide Golive global variables

  • APW_ENVIRONMENT_ID : Id of the Apwide Golive Environment (used when updating environment details, attributes, deployment,...). Replace environmentId parameter
  • APW_APPLICATION : Environment application name used in Apwide Golive (e.g. 'eCommerce'). Replace application parameter.
  • APW_CATEGORY : Environment category name used in Apwide Golive (e.g. 'Dev', 'Demo', 'Staging'...). Replace category parameter
  • APW_UNAVAILABLE_STATUS : Status name when environment is considered as not available by environment status check. Replace unavailableStatus parameter
  • APW_AVAILABLE_STATUS : Status name when environment is considered as available by environment check status. Replace availableStatus parameter
  • APW_GOLIVE_CLOUD_CREDENTIALS_ID : If shared lib used to talk to Golive Cloud, id of the Jenkins credentials to use to call Golive Cloud REST API. Replace goliveCloudCredentialsId parameter

Browse documentation in Jenkins UI

You can browse the list of step parameters and global variables of the shared lib in Parameters Global Variable Reference. This documentation will be visible from the pipeline editor only after having successfully ran the job once.

Provided Pipeline Steps

Call any REST endpoint

Manage Environment

Search and List Environments

Manage Application

Manage Category

Monitor Environments

Jira Project

Manage Jira Versions & Releases

References