Skip to content

Instantly share code, notes, and snippets.

@paul-lupu
Created July 14, 2020 17:22
Show Gist options
  • Save paul-lupu/a8be789e6d7d65996207b8e00fa7140c to your computer and use it in GitHub Desktop.
Save paul-lupu/a8be789e6d7d65996207b8e00fa7140c to your computer and use it in GitHub Desktop.
pipeline {
agent any
options {
preserveStashes(buildCount: 10)
}
stages {
stage('Build') {
steps {
echo 'Building..'
sh """
mkdir $BUILD_NUMBER
touch $BUILD_NUMBER/version.txt
echo $BUILD_NUMBER > $BUILD_NUMBER/version.txt
"""
stash name: "${currentBuild.number}", includes: "$BUILD_NUMBER/*"
}
}
stage('Test') {
steps {
unstash "${currentBuild.number}"
sh "cat $BUILD_NUMBER/version.txt"
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
@kwerle
Copy link

kwerle commented Jul 14, 2020

def IMAGE_NAME                             // defined in build section
def COMMITTER_EMAIL
def REGISTRY_URL = 'my reg' // host used for docker registry
def ACTUAL_REGISTRY_HOST = 'my host'   // ssh target for the actual registry
def SPEC_FILES_LIST_LIST

// https://github.com/jenkinsci/github-plugin
// state: ['SUCCESS', 'FAILURE', 'PENDING']
void setBuildStatus(String message, String state) {
  step([
      $class: "GitHubCommitStatusSetter",
      reposSource: [$class: "ManuallyEnteredRepositorySource", url: 'ssh://git@github.com/cdd/vault.git'],
      contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
      errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
      statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
  ]);
}

pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
    timeout(time: 20, unit: 'MINUTES') 
  }

  stages {
    stage('Init') {
      steps {
        script {
          IMAGE_NAME = 'cdd:${GIT_COMMIT}'
          GIT_COMMIT_MESSAGE = sh (
            script: 'git log -1 --pretty=%B',
            returnStdout: true
          ).trim()
          COMMITTER_EMAIL = sh (
            script: 'git --no-pager show -s --format=\'%ae\'',
            returnStdout: true
          ).trim()
          if (GIT_COMMIT_MESSAGE ==~ /(i?).*(WIP).*/) {
            currentBuild.result = 'ABORTED'
            error 'Skipping build because of commit message'
          }
          if (BRANCH_NAME ==~ /(i?).*(ZZZ|WIP).*/) {
            currentBuild.result = 'ABORTED'
            error 'Skipping build because of branch name'
          }
        }
      }
    }
    stage('Build') {
      steps {
        script {
          docker.withRegistry(REGISTRY_URL) {
def IMAGE_NAME                             // defined in build section
def COMMITTER_EMAIL
def REGISTRY_URL = 'http://localhost:5002' // host used for docker registry
def ACTUAL_REGISTRY_HOST = 'smelt.local'   // ssh target for the actual registry
def SPEC_FILES_LIST_LIST

// https://github.com/jenkinsci/github-plugin
// state: ['SUCCESS', 'FAILURE', 'PENDING']
void setBuildStatus(String message, String state) {
  step([
      $class: "GitHubCommitStatusSetter",
      reposSource: [$class: "ManuallyEnteredRepositorySource", url: 'ssh://git@github.com/cdd/vault.git'],
      contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
      errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
      statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
  ]);
}

pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
    timeout(time: 20, unit: 'MINUTES') 
  }

  stages {
    stage('Init') {
      steps {
        script {
          IMAGE_NAME = 'cdd:${GIT_COMMIT}'
          GIT_COMMIT_MESSAGE = sh (
            script: 'git log -1 --pretty=%B',
            returnStdout: true
          ).trim()
          COMMITTER_EMAIL = sh (
            script: 'git --no-pager show -s --format=\'%ae\'',
            returnStdout: true
          ).trim()
          if (GIT_COMMIT_MESSAGE ==~ /(i?).*(WIP).*/) {
            currentBuild.result = 'ABORTED'
            error 'Skipping build because of commit message'
          }
          if (BRANCH_NAME ==~ /(i?).*(ZZZ|WIP).*/) {
            currentBuild.result = 'ABORTED'
            error 'Skipping build because of branch name'
          }
        }
      }
    }
    stage('Build') {
      steps {
        script {
          docker.withRegistry(REGISTRY_URL) {
            // stuff...
            latest_image = docker.build('project name', 'Dockerfile path') // This should pull the latest image before we build - useful for cached layers
            latest_image.push() // Push the latest - which may get overwritten by another build at nearly the same time
            image = docker.build(IMAGE_NAME, 'Dockerfile path') // Push the image for *this* build
            image.push()
          }
        }
      }
    }
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment