首页 > 解决方案 > How do I replace a specific line in a file, using jenkins file build step?

问题描述

I am running a Jenkinsfile that needs to include a step, where it goes in to an existing file in a Github repo and updates a line with a version number variable, that is stored a Jenkins credential called ${version}. So that when the build is done, the variable version is added in to the compilede go program.

e.g

The Jenkins Step will need to go in to the version.go file, go to the fmt.Println() line and add in ${version}.

stage('Update Version number in version.go') {
                
                sh 'echo "${version}" > Version.go'    
                }    

File and line to be updated. Example of what I need the println line to look like.

Version.go

var versionCmd = &cobra.Command{
    Use:   "version",
    Short: "Print the version number of CLI",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println( **${version}** )
    },
}

I've been able to test adding ${version} to an existing file. My main confusion is how to specify a location in an existing file.

Thanks

标签: jenkinsgroovyjenkins-pipeline

解决方案


推荐阅读