#!/usr/bin/env groovy // Upload a file to Artifactory. Shows the HTTP code and fails on >= 400. // Usage: publishArtifact(file: 'app.tar.gz', path: 'demo/1.0.0') def call(Map config = [:]) { def file = config.file ?: error('publishArtifact: "file" is required') def path = config.path ?: error('publishArtifact: "path" is required') def repo = config.repo ?: 'lab-generic-local' def base = config.base ?: 'http://10.0.1.4:8082/artifactory' container('build') { stage('Publish to Artifactory') { withCredentials([string(credentialsId: 'artifactory-token', variable: 'ART_TOKEN')]) { sh """ HTTP=\$(curl -s -o /tmp/resp.txt -w '%{http_code}' \ -H "Authorization: Bearer \${ART_TOKEN}" \ -T ${file} \ ${base}/${repo}/${path}/${file}) echo "Artifactory responded HTTP \${HTTP}" cat /tmp/resp.txt [ "\${HTTP}" -lt 400 ] || exit 1 echo "" echo "Published: ${base}/${repo}/${path}/${file}" """ } } } }