在YML管道中使用shell脚本更改Jenkins中的目录
我在詹金斯写了一个YML管道。我使用ssh从BitBucket克隆了代码。代码目录位于jenkins_管道目录中。我必须使用YML脚本运行一些在YML管道中使用shell脚本更改Jenkins中的目录,shell,jenkins-pipeline,cd,Shell,Jenkins Pipeline,Cd,我在詹金斯写了一个YML管道。我使用ssh从BitBucket克隆了代码。代码目录位于jenkins_管道目录中。我必须使用YML脚本运行一些sh命令。我想将脚本开头的目录更改为代码目录,这样我就不再需要在每个sh命令中更改它。我没有办法做到这一点。这是我的YML脚本。我尝试过使用这个方法,但它总是说找不到目录中的script.sh文件。 YML脚本 pipeline: environment: agent: any: stages: - stage: Pul
sh
命令。我想将脚本开头的目录更改为代码目录,这样我就不再需要在每个sh
命令中更改它。我没有办法做到这一点。这是我的YML脚本。我尝试过使用这个方法,但它总是说找不到目录中的script.sh文件。YML脚本
pipeline:
environment:
agent:
any:
stages:
- stage: Pull Code and Merge master in UAT
steps:
script:
- sh 'git clone ssh://url-to-repo'
- sh 'source script.sh' #it gives the error
- sh 'pwd'
- sh 'git checkout release/uat'
- sh "git pull "
- sh 'git status'
- sh 'git merge -m "merging changes from main to UAT" origin/master'
- sh 'git push'
- stage: Content Replace
steps:
script:
- withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
script:
- "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/UAT_YamlPipeline_Ceye_Service/deploy.sh')])"
- stage: Publish Changes to uat branch
steps:
script:
- sh 'git status'
- sh 'git add --all'
- sh 'git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
- sh "git push --all"
pipeline:
environment:
agent:
any:
stages:
- stage: Pull Code and Merge master in UAT
steps:
script:
- sh ' cd ceye-service && git checkout release/uat'
- sh " cd ceye-service && git pull "
- sh ' cd ceye-service && git status'
- sh ' cd ceye-service && git merge -m "merging changes from main to UAT" origin/master'
- sh ' cd ceye-service && git push'
- stage: Content Replace
steps:
script:
- withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
script:
- "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/uat-ceye_service_code_change/ceye-service/deploy.sh')])"
- stage: Publish Changes to uat branch
steps:
script:
- sh ' cd ceye-service && git status'
- sh 'cd ceye-service && git add --all'
- sh 'cd ceye-service && git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
- sh "cd ceye-service && git push --all"
这是我当前的工作脚本
pipeline:
environment:
agent:
any:
stages:
- stage: Pull Code and Merge master in UAT
steps:
script:
- sh 'git clone ssh://url-to-repo'
- sh 'source script.sh' #it gives the error
- sh 'pwd'
- sh 'git checkout release/uat'
- sh "git pull "
- sh 'git status'
- sh 'git merge -m "merging changes from main to UAT" origin/master'
- sh 'git push'
- stage: Content Replace
steps:
script:
- withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
script:
- "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/UAT_YamlPipeline_Ceye_Service/deploy.sh')])"
- stage: Publish Changes to uat branch
steps:
script:
- sh 'git status'
- sh 'git add --all'
- sh 'git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
- sh "git push --all"
pipeline:
environment:
agent:
any:
stages:
- stage: Pull Code and Merge master in UAT
steps:
script:
- sh ' cd ceye-service && git checkout release/uat'
- sh " cd ceye-service && git pull "
- sh ' cd ceye-service && git status'
- sh ' cd ceye-service && git merge -m "merging changes from main to UAT" origin/master'
- sh ' cd ceye-service && git push'
- stage: Content Replace
steps:
script:
- withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
script:
- "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/uat-ceye_service_code_change/ceye-service/deploy.sh')])"
- stage: Publish Changes to uat branch
steps:
script:
- sh ' cd ceye-service && git status'
- sh 'cd ceye-service && git add --all'
- sh 'cd ceye-service && git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
- sh "cd ceye-service && git push --all"
我不想在每个命令中都使用
cd
。有没有办法在start中切换目录,直到脚本结束?非常感谢标记,因为注释没有适当的缩进:你能试试这个吗
pipeline:
environment:
agent:
any:
stages:
- stage: Pull Code and Merge master in UAT
steps:
script:
- dir: "'ceye-service'"
script:
- sh 'git checkout release/uat'
- sh "git pull"
- sh 'git status'
- sh 'git merge -m "merging changes from main to UAT" origin/master'
- sh 'git push'
特定文档带有代码块的特殊步骤部分描述了这一点
有些步骤有自己的代码块。例如:“withAnt、withEnv、withCredentials、dir”或具有自己代码块的任何其他自定义步骤定义。
这种步骤也可以定义为YAML。您使用什么样的插件来启用基于YAML的管道?我见过一些插件使用dir
命令作为一种包装(作为代码块的特殊步骤):steps:script:-dir:'ceye-service'脚本:-sh'git-status'
EDIT:删除了另一个sh命令在dir处键入错误,应该是dir:'ceye-service'”
@Membertable631我正在使用这个插件@Membertable631你能用格式化的方式写你的脚本吗?我对缩进感到困惑。请你把这个问题加到我的问题上好吗?谢谢你的努力。谢谢还有一个问题。我的yml脚本中有多个阶段,我可以在脚本开始时指定目录吗?您的意思是为多个阶段/步骤在顶部指定一次目录吗?我之前没有试过。如果您有权访问转换器https://jenkins-url/payConverter/
您可以将YAML示例粘贴到您的期望中,当它转换为管道声明性脚本时(并单击验证
),我想它应该会起作用。感谢您的指导。我会试试看。