Search code examples
unixjenkinssedansiblejenkins-pipeline

sed: -e expression #1, char 20: unterminated `s' command in Jenkins Pipeline


I have my docker images upload in the nexus. I need to pull and replace the latest image to the Ansible playbook.

Screenshot of Nexus Docker Registry

Below is my code and ansible file

def dockerTag(){
    env.Docker_tag = sh (script: "git rev-parse --short HEAD | tr -d '\n'", returnStdout: true)
}

pipeline{
    agent any

    stages{
        stage('Git checkout'){
            steps { 
                git 'https://github.com/xxxxx/'
            }
        }    
        
        stage('Maven Build'){
         steps {
 withCredentials([usernamePassword(credentialsId: 'mvn-credentials', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
 sh "/opt/maven/bin/mvn -s /opt/maven/conf/settings.xml package  -Dproject.username=${USERNAME} -Dproject.password=${PASSWORD}"
      }
    }
 }
    
stage('Build Docker Image'){
  steps{
      dockerTag()
  sh "docker build . -t 10.220.110.10:8083/halosys:${dockerTag()}"
  }
}    

stage('Push Docker Image to Nexus'){
  steps{
    withCredentials([string(credentialsId: 'pwd', variable: 'nexus3')]) {
    sh "docker login -u admin -p ${nexus3} 10.220.110.10:8083"
    sh "docker push 10.220.110.10:8083/halosys:${dockerTag()}"
      } 
    }
 }    

   
   stage('Running Ansible playbook'){
     steps{
         dockerTag()
         
         sh '''
            echo $Docker_tag
            sed -i "s/docker_tag/$Docker_tag/g" nexus-image-pull.yml
           ''' 
     
     }  
       
   }
   
   

   }
}

nexus-image-pull.yml Playbook code
---
- hosts: webservers
  become: True
  tasks:
    - name: pull an image
      docker_image:
      name: 10.220.110.10:8083/halosys:docker_tag
      source: pull
...

When i am trying to fetch the latest images from Nexus and substituting the value to yml file. I am getting error like

sed -i 's/docker_tag/01255bc
/g' nexus-image-pull.yml
sed: -e expression #1, char 20: unterminated `s' command

Please help to fix this issue


Solution

  • Can you try it like this: sed -e \'s!docker_tag!\'"$Docker_tag"\'!g\'

    Here I am using ! as separator in sed and \ is used to escape '

    In bash, the command would be like: sed -e 's!docker_tag!'"$Docker_tag"'!g' but since you're using : sh ''' <somecode_here> ''', in which ' is used already, we have to escape ' in sed


    Update:

    I suspected when you wrote: sed -i 's/docker_tag/01255bc /g' nexus-image-pull.yml sed: -e expression #1, char 20: unterminated `s' command about the space coming between 01255bc and /.

    It must have been like below in console log of pipeline:

    sed -i 's/docker_tag/01255bc 
    /g' nexus-image-pull.yml 
    sed: -e expression #1, char 20: unterminated `s' command
    

    Therefore, I tried to run your pipeline and I think the issue that you're facing is due to newline char \n at the end of git commit id.

    However, I modified the script according to a working script that I created for something else and it's running for me. My version of your Script:

    def dockerTag(){
        env.Docker_tag = sh (script: "git rev-parse --short HEAD | tr -d '\n'", returnStdout: true)
    }
    
    pipeline{
        agent any
    
    stages {
    
     stage('Running Ansible playbook'){
         steps{
             dockerTag()
             
             sh '''
               echo $Docker_tag
       
              sed -i "s/docker_tag/$Docker_tag/g" nexus-image-pull.yml
              '''
             
         }  
           
       }
     }
    }