Advertisement
Guest User

Untitled

a guest
Aug 29th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.39 KB | None | 0 0
  1. app = 'fsapi'
  2. slack_channel = '#ci-fsws'
  3. repo = 'fs-api.git'
  4. branch_to_build = 'master'
  5.  
  6. pipeline {
  7.   agent any
  8.  
  9.   options {
  10.     buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5'))
  11.   }
  12.  
  13.   triggers {
  14.     pollSCM('H/5 * * * *')
  15.   }
  16.  
  17.   environment {
  18.     MAVEN_HOME = "${tool 'Maven3'}"
  19.     PATH = "${MAVEN_HOME}/bin:${PATH}"
  20.   }
  21.  
  22.   stages {
  23.     stage('Checkout') {
  24.       steps {
  25.         git url: "ssh://git@bitbucket.unit.no:7999/fs/${repo}", branch: branch_to_build, credentialsId: "jenkins-unit-bitbucket"
  26.       }
  27.     }
  28.  
  29.     stage('Build') {
  30.       steps {
  31.         withMaven(maven: 'Maven3', mavenSettingsConfig: '17598675-d4d6-41a1-aca0-4803d7ba8a50') {
  32.           sh "mvn $MAVEN_CONFIG -U -X clean package -Dfs.test=true -Dlog4j.configuration=log4j_dev.properties"
  33.         }
  34.       }
  35.     }
  36.  
  37.     stage('Unit Tests') {
  38.       steps {
  39.         withMaven(maven: 'Maven3', mavenSettingsConfig: '17598675-d4d6-41a1-aca0-4803d7ba8a50') {
  40.           sh "mvn test -Dfs.test=true -Dlog4j.configuration=log4j_dev.properties"
  41.         }
  42.       }
  43.     }
  44.  
  45.     stage('Build Docker Image') {
  46.       steps {
  47.         withDockerRegistry([url: 'https://harbor.uio.no:443', credentialsId: 'harbor-ceres']) {
  48.           script {
  49.             def pom = readMavenPom file: 'pom.xml'
  50.             VERSION = pom.version
  51.           }
  52.           sh "docker build -f hosting/api/Dockerfile --pull -t ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} ."
  53.         }
  54.       }
  55.     }
  56.    
  57.     stage('Publish') {
  58.       steps {
  59.         withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'harbor-ceres', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
  60.           withDockerRegistry([url: 'https://harbor.uio.no:443', credentialsId: 'harbor-ceres']) {
  61.             script {
  62.               def pom = readMavenPom file: 'pom.xml'
  63.               VERSION = pom.version
  64.             }
  65.             sh "docker login -u \"${USERNAME}\" -p \"${PASSWORD}\" https://harbor.uio.no:443"
  66.             sh "docker tag ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} harbor.uio.no:443/ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP}"
  67.             sh "docker push harbor.uio.no:443/ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP}"
  68.             sh "docker tag ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} harbor.uio.no:443/ceres-fs/${app}:prod"
  69.             sh "docker push harbor.uio.no:443/ceres-fs/${app}:prod"
  70.           }
  71.         }
  72.       }
  73.     }
  74.   }
  75.  
  76.   post {
  77.     always {
  78.       deleteDir() /* clean up our workspace */
  79.  
  80.       /* Forcefully deletes all dangling images */
  81.       sh """#!/bin/bash
  82.         docker images --no-trunc --all --quiet --filter='dangling=true' | xargs --no-run-if-empty docker rmi -f
  83.         """
  84.  
  85.       /* Forcefully deletes all images older than 1 hour */
  86.       sh """#!/bin/bash
  87.         docker images | grep fsapi | grep -e "months\\|weeks\\|days\\|hours" | awk '{ print \$1 ":" \$2 }' | xargs --no-run-if-empty docker rmi -f
  88.         """
  89.     }
  90.  
  91.     success {
  92.         slackSend channel: slack_channel, color: '#00FF00', message: "Finished publising ${app}:${VERSION} docker image. Latest commit: ${env.GIT_COMMIT}"
  93.     }
  94.  
  95.     failure {
  96.         slackSend channel: slack_channel, color: '#FF0000', message: "Failed to publish ${app}:${VERSION} docker image. Latest commit: ${env.GIT_COMMIT}"
  97.     }
  98.  
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement