In my Jenkins build, when I push a local PR to Master, blackduck scanning in the Master branch often fails. And so I would like to introduce some code to get it to retry if it fails. I looked online and found a useful command && break syntax, but I’m not sure if I am using it correctly.
Here is the code:
#!/bin/bash
set -ex
. "${0%/*}/common.sh"
. activate "$PYENV_HOME"
for ((i=0;i<5;++i));
do
if [[ "$BRANCH_NAME" == "master" || "$BRANCH_NAME" == "main" ]]; then
DETECT_PROJECT_VERSION_NAME="$BRANCH_NAME"
DETECT_PROJECT_VERSION_PHASE="PRERELEASE"
bash <(curl -s https://artifactory.dev.com/artifactory/blackduck-detect/detect7.sh) \
--blackduck.url=https://com_name.app.blackduck.com \
--blackduck.api.token="${BLACKDUCK_API_TOKEN}" \
--detect.project.name="${DETECT_PROJECT_NAME}" \
--detect.project.version.name="${DETECT_PROJECT_VERSION_NAME}" \
--detect.project.version.phase="${DETECT_PROJECT_VERSION_PHASE}" \
--detect.code.location.name="${DETECT_PROJECT_NAME}_${BRANCH_NAME}" \
--detect.source.path="$(pwd)" \
--detect.excluded.directories=/tests/
&& break
fi
sleep 15
done
Another quick question if I may; after introducing the code, I would like to test this locally, so it possible to simply change the "$BRANCH_NAME" to my local PR branch’s name?
I found this link that was helpful: https://unix.stackexchange.com/questions/82598/how-do-i-write-a-retry-logic-in-script-to-keep-retrying-to-run-it-upto-5-times
But can I substitute my whole code for command?
>Solution :
Is my Bash command syntax of
command && breakcorrect?
Yes.
The exit status of if is 0, when there is no else block and the condition if false, or the exit status of the whole if block is equal to the exit status of the last command executed. So if the condition is false, then && break will break, if the condition is true, then bash <(curl will execute and will && break depending on the exit status.
I would refactor it to just bash <(curl..... && break. I would also just for i in $(seq 5) or for ((i=0;i<5;++i)) instead of while. I see no reason to do if [ "$BRANCH_NAME" == "master"... every loop. Also prefer if instead of &&. Check your scripts with shellcheck. Additionally, you can use $PWD instead of $(pwd).
#!/bin/bash
set -ex
. "${0%/*}/common.sh"
. activate "$PYENV_HOME"
if [[ "$BRANCH_NAME" == "master" || "$BRANCH_NAME" == "main" ]]; then
DETECT_PROJECT_VERSION_NAME="$BRANCH_NAME"
DETECT_PROJECT_VERSION_PHASE="PRERELEASE"
for ((i=0;i<5;++i)); do
if
bash <(curl -s https://artifactory.dev.com/artifactory/blackduck-detect/detect7.sh) \
--blackduck.url=https://com_name.app.blackduck.com \
--blackduck.api.token="${BLACKDUCK_API_TOKEN}" \
--detect.project.name="${DETECT_PROJECT_NAME}" \
--detect.project.version.name="${DETECT_PROJECT_VERSION_NAME}" \
--detect.project.version.phase="${DETECT_PROJECT_VERSION_PHASE}" \
--detect.code.location.name="${DETECT_PROJECT_NAME}_${BRANCH_NAME}" \
--detect.source.path="$PWD" \
--detect.excluded.directories=/tests/
then
break
fi
done
fi
so it possible to simply change the "$BRANCH_NAME" to my local PR branch’s name?
It’s just a variable, set it and export.
BRANCH_NAME=stuff ./yourscript
But can I substitute my whole code for command?
Generally yes. Research command exit status in bash