Skip to content

Instantly share code, notes, and snippets.

@cvega
Last active April 19, 2020 04:28
Show Gist options
  • Save cvega/4b12e382a092aeb42a09a909372e8fa4 to your computer and use it in GitHub Desktop.
Save cvega/4b12e382a092aeb42a09a909372e8fa4 to your computer and use it in GitHub Desktop.
CLOUDBEES: Artifactory/Customtools Functions for post install configuration

Artifactory/Customtools Functions for post install configuration

This a MVP for a global library that will configure artifactory and customtools post install as a job.

This can live as a global variable in /vars of your global pipeline. If this file was config.groovy you can call these functions as config.artifactory()

See below how to wire the global lib into casc

CODE

import hudson.tools.*
import jenkins.model.*

import com.cloudbees.jenkins.plugins.customtools.*
import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersionConfig

import org.jfrog.* 
import org.jfrog.hudson.* 
import org.jfrog.hudson.util.Credentials;

// This uses an artifactory credential, I recommend bringing the credential in through the normal pipeline
// process "withCredentials" and substituting.

// constructor variables:
// serverId (string)
// artifactoryURL (string)
// deployerCredentialsConfig (CredentialsConfig)
// resolverCredentialsConfig (CredentialsConfig)
// timeout (int)
// bypassProxy (bool)
// connectionRetry (int)
// deploymentThreads(int)
def artifactory() {
    def inst = Jenkins.getInstance()
    def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
    def deployerCredentials = new CredentialsConfig("<username>", "<password>", "")
    def sinst = [new ArtifactoryServer( 
    "Artifactory", 
    "https://demo-test.com/artifactory", 
    deployerCredentials,deployerCredentials,
    300, 
    true ,1,1)]
    
    desc.setArtifactoryServers(sinst)
}

// zip extraction from url
def customtools_url () {
    def inst = Jenkins.getInstance()
    def desc = inst.getExtensionList(com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl.class)[0]
    def customTools = []

    List installers = new ArrayList();
    installers.add(new ZipExtractionInstaller("<label>", "<url>", "<dir>"))

    List<ToolProperty> properties = new ArrayList<ToolProperty>()
    properties.add(new InstallSourceProperty(installers))

    def installation = new CustomTool(
    "<toolname>",
    "",
    properties,
    "",
    null,
    ToolVersionConfig.DEFAULT,
    null
    )

    customTools.push(installation)

    desc.setInstallations((com.cloudbees.jenkins.plugins.customtools.CustomTool[]) customTools)
    desc.save()

}

// run bash
def customtools_sh () {
    def inst = Jenkins.getInstance()
    def desc = inst.getExtensionList(com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl.class)[0]
    def customTools = []

    List installers = new ArrayList();
    installers.add(new CommandInstaller("<label>", "<command>", "<dir>")

    List<ToolProperty> properties = new ArrayList<ToolProperty>()
    properties.add(new InstallSourceProperty(installers))

    def installation = new CustomTool(
    "<toolname>",
    "",
    properties,
    "",
    null,
    ToolVersionConfig.DEFAULT,
    null
    )

    customTools.push(installation)

    desc.setInstallations((com.cloudbees.jenkins.plugins.customtools.CustomTool[]) customTools)
    desc.save()

}

CASC GLOBAL LIB

Change the Github piece to Bitbucket branch Source

globalLibraries:
    libraries:
    - defaultVersion: "master"
      implicit: true
      name: "shlib"
      retriever:
        modernSCM:
          scm:
            github:
              configuredByUrl: true
              credentialsId: "github-user-token"
              id: "d4b4d948-b7ff-4b86-9909-a948a629a555"
              repoOwner: "initrode"
              repository: "shared-marker"
              repositoryUrl: "https://github.com/initrode/shared-marker.git"
              traits:
              - gitHubBranchDiscovery:
                  strategyId: 1
              - gitHubPullRequestDiscovery:
                  strategyId: 1
              - gitHubForkDiscovery:
                  strategyId: 1
                  trust: "gitHubTrustPermissions"

CASC PIPELINE/JOB

https://jenkinsci.github.io/job-dsl-plugin/

Create a pipeline job that can take advantage of the shared library. You can use a cron trigger to automatically run the job when the master comes up

https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob

Triggers:

properties([pipelineTriggers([cron('H 23 * * *')])])

or (declaritive):

triggers {
    cron('H */4 * * 1-5')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment