[英]Change directory in Jenkins using shell script in YML pipeline
我在 Jenkins 中編寫了一個 YML 管道。 我使用 BitBucket 中的 ssh 克隆了代碼。 代碼目錄位於 jenkins_pipeline 目錄中。 我必須使用 YML 腳本運行一些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"
我不想在每個命令中都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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.