I have a pipeline and I'm building my image through a docker container and it output the image tag, I want to pass that image tag to next stage, when I echo it in the next stage it prints out. but when I use it in a shell it goes empty. here is my pipeline
pipeline {
agent any
stages {
stage('Cloning Git') {
steps {
git( url: 'https://xxx#bitbucket.org/xxx/xxx.git',
credentialsId: 'xxx',
branch: 'master')
}
}
stage('Building Image') {
steps{
script {
env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true
}
}
}
stage('Integration'){
steps{
script{
echo "passed: ${env.IMAGE_TAG}"
sh """
helm upgrade exampleservice charts/exampleservice --set image.tag=${env.IMAGE_TAG}
"""
sh "sleep 5"
}
}
}
}
}
pipeline output
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integration)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
passed:
b79c3bf-b6eec4f
[Pipeline] sh
[test101] Running shell script
+ helm upgrade exampleservice charts/exampleservice --set image.tag=
getting empty image tag
You should override this by using the 'env'.
Replace your code with this one:
pipeline {
agent any
stages {
stage('Cloning Git') {
steps {
git( url: 'https://xxx#bitbucket.org/xxx/xxx.git',
credentialsId: 'xxx',
branch: 'master')
}
}
stage('Building Image') {
steps{
script {
env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true
}
}
}
stage('Integration'){
steps{
script{
echo "passed: ${env.IMAGE_TAG}"
sh """
helm upgrade exampleservice charts/exampleservice\
--set image.tag="${env.IMAGE_TAG}"
"""
sh "sleep 5"
}
}
}
}
}
Related
everyone.
There is Jenkins running on my ubunut host.
Trying to execute the creation of the "aws ECR" in Jenkins file.
but,Jenkins error ImportError: No module named boto3error
my jenkins file
node {
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("xxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/xxx")
}
stage('Create ECR') {
steps {
'''
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
'''
}
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://xxxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:xxx-aws-ecr') {
app.push("xxx")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
Thanks you!!
Jenkins output error..
[Checks API] No suitable checks publisher found.
java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps
My jenkinsfile.
node {
stage('Clone repository') {
checkout scm
}
stage('Build packer') {
steps {
dir('packer') {
sh 'git clone https://github.com/changhyuni/packer'
sh 'packer build ec2.json'
}
}
}
stage('Build image') {
app = docker.build("475667265637.dkr.ecr.ap-northeast-2.amazonaws.com/chang")
}
stage('Create ECR') {
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://475667265637.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:chang-aws-ecr') {
app.push("chang")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
steps is a directive from declarative syntax https://www.jenkins.io/doc/book/pipeline/syntax/#steps
Your example is scripted syntax https://www.jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline
I managed to setup a jenkins on kubernetes and gitbucket on kubernetes. Now I am trying out to create my own first docker file for uploading on dockerhub. Unfortunately it fails while uploading to docker. Build is successfully, but I cant manage how to upload it to dockerhub (private repository).
Jenkinsfile
def label = "${BUILD_TAG}"
podTemplate(label: label, containers: [
containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true)
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
node(label) {
def app
def myRepo = checkout scm
def gitCommit = myRepo.GIT_COMMIT
def gitBranch = myRepo.GIT_BRANCH
def shortGitCommit = "${gitCommit[0..10]}"
def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
stage('Decommission Infrastructure') {
container('kubectl') {
echo "Decmomission..."
}
}
stage('Build application') {
container('docker') {
app = docker.build("fasautomation/recon", ".")
}
}
stage('Run unit tests') {
container('docker') {
app.inside {
sh 'echo "Test passed"'
}
}
}
stage('Docker publish') {
container('docker') {
docker.withRegistry('https://registry.hub.docker.com', '<<jenkins store-credentials>>') {
echo "Pushing 1..."
// Push tagged version
app.push("${env.BUILD_NUMBER}")
echo "Pushing 2..."
// Push latest-tagged version
app.push("latest")
echo "Pushed!"
}
}
}
stage('Deployment') {
container('docker') {
// Deploy to Kubernetes
echo 'Deploying'
}
}
stage('Provision Infrastructure') {
container('kubectl') {
echo 'Provision...'
}
}
}
}
Jenkins Logs
[...]
[Pipeline] stage (hide)
[Pipeline] { (Docker publish)
[Pipeline] container
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
Executing sh script inside container docker of pod jenkins-recon-master-116-0ksw8-f7779
Executing command: "docker" "login" "-u" "*****" "-p" ******** "https://index.docker.io/v1/"
exit
<<endless loading symbol>>
Does anyone has a clue how to debug here? Credentials work. Not sure why there is the exit in the log without the logs for pushing afterwards... :-(
I am trying to set environment variable in Jenkinsfile following way,
pipeline {
agent { label 'slave1' }
stages {
stage ('Build') {
steps {
script {
BUILD_VERSION = sh (
script: 'python get_firmware_version.py',
returnStdout: true
).trim()
}
echo "${BUILD_VERSION}"
withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
dir('firmware/') {
echo "${BUILD_VERSION}"
sh '''
echo "Building"
echo "${BUILD_VERSION}"
echo "${env.BUILD_VERSION}"
'''
}
}
}
}
}
post {
failure {
script {
echo "Pipeline Failed"
}
}
}
}
But its failing with following error Bad substitution
[Pipeline] echo
0_2_0
[Pipeline] sh
+ echo Building
Building
/home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware#tmp/durable-54e04481/script.sh: 3: /home/jenkins/jenkins_slave/workspace/Firmware/Branch/firmware#tmp/durable-54e04481/script.sh: Bad substitution
Why I can't set ENV Var and use it in sh step ?
This is Jenkins thing I think. When you use the sh block with '; it will not have access to things like environment variables. Try using the " instead. That should work
sh """
echo "Building"
echo "${env.BUILD_VERSION}"
echo "${env}"
"""
Jenkins should recognise the shell block and escape the " within the """ automatically.
pipeline {
agent { label 'slave1' }
stages {
stage ('Build') {
steps {
script {
BUILD_VERSION = sh (
script: 'python get_firmware_version.py',
returnStdout: true
).trim()
}
echo "${BUILD_VERSION}"
withCredentials([file(credentialsId: 'image-sign', variable: 'IMAGE_SIGN')]) {
dir('firmware/') {
echo "${BUILD_VERSION}"
sh """
echo "Building"
echo "${BUILD_VERSION}"
echo "${env.BUILD_VERSION}"
"""
}
}
}
}
}
post {
failure {
script {
echo "Pipeline Failed"
}
}
}
}
My test case
pipeline {
agent {
node {
label 'devops-jenkins-slave'
}
}
options {
timestamps()
}
stages {
stage('Setup'){
steps {
dir("${WORKSPACE}/"){
script {
BUILD_VERSION = "1"
}
sh """
echo "${BUILD_VERSION}"
"""
}
}
}
}
post {
always {
dir("${WORKSPACE}/"){
deleteDir()
}
}
}
}
Result
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on devops-jenkins-slave in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Setup)
[Pipeline] dir
22:09:55 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
22:09:56 [Test] Running shell script
22:09:56 + echo 1
22:09:56 1
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] dir
22:09:56 Running in /home/jenkins/workspace/Samples/Test
[Pipeline] {
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
/var/coverityCompile.groovy
#!groovy
def java(String DIR) {
compile_dir="${WORKSPACE}/${DIR}"
echo "complie_dir:${compile_dir}"
sh "echo complie_dir:${compile_dir}"
sh '''
echo complie_dir:${compile_dir}
'''
}
/src/org/coverity/JenkinsFile
#!groovy
#Library('shared-library') _
pipeline {
agent { label 'dev_ci_env_dm' }
stages {
stage('coverity'){
steps{
script{
coverityCompile.java("DM")
}
}
}
}
}
The Jenkins log will be:
complie_dir:/home/ci/workspace/test_coverity/DM
[Pipeline] sh
[test_coverity] Running shell script
+ echo complie_dir:/home/ci/workspace/test_coverity/DM
complie_dir:/home/ci/workspace/test_coverity/DM
complie_dir:
when I use echo "complie_dir:${compile_dir}"
and sh "echo complie_dir:${compile_dir}"
the result will be printed as expected. But, when I use
sh '''
echo complie_dir:${compile_dir}
'''
The result of ${compile_dir} will be null.
Now, I want to execute multiline of shell,so is there any other way to replace sh ''' '''?