@@ -259,16 +259,17 @@ function mount_file() {
259259    mounts+=(" -v" " ${file_on_host} :${file_in_container} :${mount_option} " 
260260}
261261
262- #  Handle tokens.
263- set  +u
264- echo  " " >  ${TOKEN_FILE} 
265- {
266-     echo  " GITHUB_TOKEN=${GITHUB_TOKEN} " >>  ${TOKEN_FILE} 
267-     echo  " MCN_GITLAB_TOKEN=${MCN_GITLAB_TOKEN} " >>  ${TOKEN_FILE} 
268-     echo  " MCN_SELF_HOSTED_GITLAB_TOKEN=${MCN_SELF_HOSTED_GITLAB_TOKEN} " 
269- } >>  ${TOKEN_FILE} 
270- mount_file " macaron_env_file" ${TOKEN_FILE}  ${MACARON_WORKSPACE} /${TOKEN_FILE}  " rw,Z" 
271- set  -u
262+ #  Clean up the token file and EXIT this bash script with the given status code.
263+ # 
264+ #  Arguments:
265+ #    $1: The eventual exit status code.
266+ #    $2: The path to the token file.
267+ function  clean_up_exit()  {
268+     status_code=$1 
269+     token_file_path=$2 
270+     rm -f " $token_file_path " 
271+     exit  " $status_code " 
272+ }
272273
273274#  Parse main arguments.
274275while  [[ $#  -gt  0 ]];  do 
@@ -598,6 +599,18 @@ else
598599    fi 
599600fi 
600601
602+ #  Make sure commands that need to be cleaned up exist within `set +e` so that when any of them returns a non-zero
603+ #  status code, we don't exit right away and still run the token file cleaning up command.
604+ set  +e
605+ 
606+ #  Handle tokens.
607+ {
608+     echo  " GITHUB_TOKEN=${GITHUB_TOKEN} " 
609+     echo  " MCN_GITLAB_TOKEN=${MCN_GITLAB_TOKEN} " 
610+     echo  " MCN_SELF_HOSTED_GITLAB_TOKEN=${MCN_SELF_HOSTED_GITLAB_TOKEN} " 
611+ } >  ${TOKEN_FILE} 
612+ mount_file " macaron_env_file" ${TOKEN_FILE}  ${MACARON_WORKSPACE} /${TOKEN_FILE}  " rw,Z" 
613+ 
601614#  Force docker to use linux/amd64 platform in order to make docker use emulation on ARM host platforms.
602615docker run \
603616    --platform=linux/amd64 \
@@ -612,4 +625,4 @@ docker run \
612625    " ${entrypoint[@]} " 
613626    " ${macaron_args[@]} " 
614627
615- rm -f  " $TOKEN_FILE " 
628+ clean_up_exit  " $? " " $TOKEN_FILE " 
0 commit comments