英文:
Bash script for Gitlab CI
问题
我有一个bash脚本,它将查看在流水线上运行的作业列表,并搜索两个特定的作业。如果作业A匹配成功,我想运行脚本的一部分,在某些条件下重试作业B;如果找到作业B,执行脚本的另一部分,将重试作业A。
在我现在的解决方案中,在if语句之前进行了搜索,这意味着两个if条件都会满足,整个脚本都会运行。我不知道如何更改条件以避免这种情况... 我陷入了困境,所以任何建议都会很棒。
以下是脚本的草图:
keyWord_A="jobA"
keyword_B="jobB"
# 使用curl命令获取作业列表
# 使用jq搜索作业列表,查找
# keyWord_A 并将结果存储在match_A中
# keyword_B 并将结果存储在match_B中
if [[ keyWord_A == match_A ]] ; then
# 运行此代码
if [[ keyWord_B == match_B ]] ; then
# 运行此代码
如果您需要进一步的帮助,请告诉我。
英文:
I have a bash script that will look into the list of jobs running on the pipeline and search for 2 specific jobs.
If job A is a match, I want to run one part of the script that in some conditions will retry job B, if job B is found, do another part of the script that will retry job A.
In the solution I have now I do the search before I have the if statements, and that means both if conditions are met and the hole script will run. I don't know how to change my condition to avoid that... I am stuck so any suggestion would be awesome.
Here is a sketch of the script:
keyWord_A="jobA"
keyword_B="jobB"
# get the job_list with curl command
# search the job_list with jq for
# keyWord_A and store the result in match_A
# keyword_B and store the result in match_B
if [[ keyWord_A == match_A ]] ; then
# run this code
if [[ keyWord_B == match_B ]] ; then
# run this code
答案1
得分: 1
这个解决方案比我想象的要简单...我可以从CI中获取当前作业的名称。这样,我可以在我的if条件中使用当前作业的名称,并从脚本中获取所需的输出😊:
keyWord_A="jobA"
keyword_B="jobB"
if [[ keyWord_A == CI_job ]] ; then
# 运行这段代码
if [[ keyWord_B == CI_job ]] ; then
# 运行这段代码
英文:
The solution was simpler than I thought... I can get from the CI the name of the curent job. This way, I can use the name of the current job in my if conditions and get the desired output from the script😊:
keyWord_A="jobA"
keyword_B="jobB"
if [[ keyWord_A == CI_job ]] ; then
# run this code
if [[ keyWord_B == CI_job ]] ; then
# run this code
答案2
得分: 0
自从您知道如果同时找到工作A和B,您可以添加条件到您的测试中,或者使用一个简单的else if
来执行一个,而不是两个:
if [[ keyWord_A == match_A ]] ; then
# 执行这段代码
elif [[ keyWord_B == match_B ]] ; then
# 执行这段代码
fi
在这里,如果两个条件都满足,只有第一个if会被执行。不会同时执行两个。
英文:
Since you know if both job A and B are found, you can add conditions to your test, or use a simple else if
in order to execute one, or the others, but not both:
if [[ keyWord_A == match_A ]] ; then
# run this code
elif [[ keyWord_B == match_B ]] ; then
# run this code
fi
Here, if both conditions are met, only the first if will be executed. Not both.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论