Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app = 'fsapi'
- slack_channel = '#ci-fsws'
- repo = 'fs-api.git'
- branch_to_build = 'master'
- pipeline {
- agent any
- options {
- buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5'))
- }
- triggers {
- pollSCM('H/5 * * * *')
- }
- environment {
- MAVEN_HOME = "${tool 'Maven3'}"
- PATH = "${MAVEN_HOME}/bin:${PATH}"
- }
- stages {
- stage('Checkout') {
- steps {
- git url: "ssh://git@bitbucket.unit.no:7999/fs/${repo}", branch: branch_to_build, credentialsId: "jenkins-unit-bitbucket"
- }
- }
- stage('Build') {
- steps {
- withMaven(maven: 'Maven3', mavenSettingsConfig: '17598675-d4d6-41a1-aca0-4803d7ba8a50') {
- sh "mvn $MAVEN_CONFIG -U -X clean package -Dfs.test=true -Dlog4j.configuration=log4j_dev.properties"
- }
- }
- }
- stage('Unit Tests') {
- steps {
- withMaven(maven: 'Maven3', mavenSettingsConfig: '17598675-d4d6-41a1-aca0-4803d7ba8a50') {
- sh "mvn test -Dfs.test=true -Dlog4j.configuration=log4j_dev.properties"
- }
- }
- }
- stage('Build Docker Image') {
- steps {
- withDockerRegistry([url: 'https://harbor.uio.no:443', credentialsId: 'harbor-ceres']) {
- script {
- def pom = readMavenPom file: 'pom.xml'
- VERSION = pom.version
- }
- sh "docker build -f hosting/api/Dockerfile --pull -t ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} ."
- }
- }
- }
- stage('Publish') {
- steps {
- withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'harbor-ceres', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
- withDockerRegistry([url: 'https://harbor.uio.no:443', credentialsId: 'harbor-ceres']) {
- script {
- def pom = readMavenPom file: 'pom.xml'
- VERSION = pom.version
- }
- sh "docker login -u \"${USERNAME}\" -p \"${PASSWORD}\" https://harbor.uio.no:443"
- sh "docker tag ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} harbor.uio.no:443/ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP}"
- sh "docker push harbor.uio.no:443/ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP}"
- sh "docker tag ceres-fs/${app}:${VERSION}-${env.BUILD_TIMESTAMP} harbor.uio.no:443/ceres-fs/${app}:prod"
- sh "docker push harbor.uio.no:443/ceres-fs/${app}:prod"
- }
- }
- }
- }
- }
- post {
- always {
- deleteDir() /* clean up our workspace */
- /* Forcefully deletes all dangling images */
- sh """#!/bin/bash
- docker images --no-trunc --all --quiet --filter='dangling=true' | xargs --no-run-if-empty docker rmi -f
- """
- /* Forcefully deletes all images older than 1 hour */
- sh """#!/bin/bash
- docker images | grep fsapi | grep -e "months\\|weeks\\|days\\|hours" | awk '{ print \$1 ":" \$2 }' | xargs --no-run-if-empty docker rmi -f
- """
- }
- success {
- slackSend channel: slack_channel, color: '#00FF00', message: "Finished publising ${app}:${VERSION} docker image. Latest commit: ${env.GIT_COMMIT}"
- }
- failure {
- slackSend channel: slack_channel, color: '#FF0000', message: "Failed to publish ${app}:${VERSION} docker image. Latest commit: ${env.GIT_COMMIT}"
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement