使用 YML 管道中的 shell 脚本更改 Jenkins 中的目录
Change directory in Jenkins using shell script in YML pipeline
我在 Jenkins 中编写了一个 YML 管道。我使用 BitBucket 中的 ssh 克隆了代码。代码目录在 jenkins_pipeline 目录中。我必须使用 YML 脚本 运行 几个 sh
命令。我想将脚本开头的目录更改为代码目录,这样我就不再需要在每个 sh
命令中更改它。我没有办法做到这一点。这是我的 YML 脚本。我试过使用 this 方法,但它总是说 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"
我不想在每个命令中都使用 cd
。有没有办法可以在开始时切换目录并一直保留到脚本结束?非常感谢
标记,因为评论没有正确的缩进:你能试试这个吗?
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'
文档 https://plugins.jenkins.io/pipeline-as-yaml/ 具体 Special Steps With Code Blocks
的部分对此进行了描述。
有些步骤有自己的代码块。例如:'withAnt, withEnv, withCredentials, dir' 或任何其他具有自己的代码块的自定义步骤定义。
这种步骤也可以定义为YAML。
我在 Jenkins 中编写了一个 YML 管道。我使用 BitBucket 中的 ssh 克隆了代码。代码目录在 jenkins_pipeline 目录中。我必须使用 YML 脚本 运行 几个 sh
命令。我想将脚本开头的目录更改为代码目录,这样我就不再需要在每个 sh
命令中更改它。我没有办法做到这一点。这是我的 YML 脚本。我试过使用 this 方法,但它总是说 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"
我不想在每个命令中都使用 cd
。有没有办法可以在开始时切换目录并一直保留到脚本结束?非常感谢
标记,因为评论没有正确的缩进:你能试试这个吗?
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'
文档 https://plugins.jenkins.io/pipeline-as-yaml/ 具体 Special Steps With Code Blocks
的部分对此进行了描述。
有些步骤有自己的代码块。例如:'withAnt, withEnv, withCredentials, dir' 或任何其他具有自己的代码块的自定义步骤定义。 这种步骤也可以定义为YAML。