Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active October 7, 2020 00:36
Show Gist options
  • Select an option

  • Save turboBasic/25d38643d6e868ed777c42cfd408565b to your computer and use it in GitHub Desktop.

Select an option

Save turboBasic/25d38643d6e868ed777c42cfd408565b to your computer and use it in GitHub Desktop.
Jenkins DSL script to build job from latest Gerrit patchset #jenkins #gerrit

Jenkins DSL script to build job from latest Gerrit patchset

Job configuration

Parameters

CHANGE_NUMBER (String)

Prepare an environment for the run

Properties

These properties would be provided to DSL script as default config values

JOB_UI_DIR=Playground/mao
DEFAULT_EMAIL=noone@example.com
REPLY_TO=noone@example.com
SHOULD_SEND_EMAIL=
DEFAULT_BRANCH=ci-debug/001
DEFAULT_TAG=ci-debug/v1.10.1
DEFAULT_HASH=cdac769efe0566e13946d0ed5b8bb50c3c39b3e8

Groovy script

  assert CHANGE_NUMBER

  String branchPrefix = {
      String last2Digits = ("  " + CHANGE_NUMBER)[-2..-1].trim()
      "changes/${last2Digits}/${CHANGE_NUMBER}"
  } ()

  def config = [   
      development: [
          REFSPEC: "+refs/${branchPrefix}/*:refs/remotes/origin/${branchPrefix}/*",
          BRANCH: "${branchPrefix}/1",
      ],
      production: [
          REFSPEC: '+refs/heads/*:refs/remotes/origin/*',
          BRANCH: 'develop',
      ],
  ].get(ENVIRONMENT)

  return config

Source code management

Git

Repositories

Refspec

${REFSPEC}

Branches to build

${BRANCH}

Build

Execute shell

if [ "${ENVIRONMENT}" = development ]
then
    PATCHSET_NUMBER="$(
        git branch -a \
        | 	grep -o "/${CHANGE_NUMBER}/.*" \
        | 	cut  -d/ -f3 \
        | 	sort -nr \
        | 	head -1
    )"
    BRANCH="${BRANCH%%/1}/${PATCHSET_NUMBER}"
    git checkout ${BRANCH}
fi

cat <<EOF >build.properties 
    BRANCH=${BRANCH}
    PATCHSET_NUMBER=${PATCHSET_NUMBER}
EOF

Inject environment variables from file

build.properties

Process Job DSL scripts

Path to your script(s) in Source control
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment