Find and Replace does not seem to work in Jenkins Pipeline

55 Views Asked by At

I have the following code to search a string and replace it in all deployment.yaml files

it works locally but does not seem to work on Jenkins.

def files = findFiles(glob: "**/deployment.yaml")
files.each {
                file ->
                    status = sh(script: """sed -r -i 's,$project.*,$project:$tag,' ./$file.path &> tmp.txt""",
                            returnStatus: true)
                    sh "cat tmp.txt"
                    if (status != 0) {
                        error("Error: string replacement in yaml failed for the file $file.directory")                   
                    }
            }
   
}

The tmp.txt file is empty. In any which case the replacement should have worked. The problem is I am coupling this with git add and git commit after this stage and the git does not necessarily add all the changed yaml files.

I tried using lock (from lockable-resource plugin) before git add, because I thought may be ``git add` has a race condition that I am not aware of. But that did not help either.

1

There are 1 best solutions below

2
mbwmd On

You call sed ... -i ... which means to make the modificaation "inplace" (aka within the file). This does not produce output as sed does without -i, when it does output the changed file.