diff --git a/Jenkinsfile b/Jenkinsfile index 9ffce8a..e5cdefa 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,36 +1,32 @@ -node { - def app - - stage('Clone repository') { - /* Let's make sure we have the repository cloned to our workspace */ - - checkout scm +pipeline { + agent any + environment { + HARBOR_CREDENTIALS = 'jenkins-harbor' + HARBOR_URL = 'https://harbor.lan' + IMAGE_NAME = 'rsyncd' + IMAGE_TAG = 'latest' } - - stage('Build image') { - /* This builds the actual image; synonymous to - * docker build on the command line */ - - sh 'docker build -t andreas/rsyncd:latest' - } - - stage('Test image') { - /* Ideally, we would run a test framework against our image. - * For this example, we're using a Volkswagen-type approach ;-) */ - - app.inside { - sh 'echo "Tests passed"' + stages { + stage('Build and Push Docker Image') { + steps { + script { + withCredentials([usernamePassword(credentialsId: HARBOR_CREDENTIALS, usernameVariable: 'HARBOR_USERNAME', +passwordVariable: 'HARBOR_PASSWORD')]) { + sh "docker login -u ${HARBOR_USERNAME} -p ${HARBOR_PASSWORD} ${HARBOR_URL}" + + sh "docker build -t ${HARBOR_USERNAME}/${IMAGE_NAME}:${IMAGE_TAG} ." + + sh "docker tag ${HARBOR_USERNAME}/${IMAGE_NAME}:${IMAGE_TAG} ${HARBOR_URL}/${HARBOR_USERNAME}/${IMAGE_NAME}:${IMAGE_TAG}" + + sh "docker push ${HARBOR_URL}/${HARBOR_USERNAME}/${IMAGE_NAME}:${IMAGE_TAG}" + } + } + } } } - - stage('Push image') { - /* Finally, we'll push the image with two tags: - * First, the incremental build number from Jenkins - * Second, the 'latest' tag. - * Pushing multiple tags is cheap, as all the layers are reused. */ - docker.withRegistry('https://harbor.lan', 'jenkins-harbor') { - app.push("${env.BUILD_NUMBER}") - app.push("latest") + post { + always { + echo 'Pipeline finished' } } -} +} \ No newline at end of file