1

Is there any way to send back exit signal from the bash script to the GitLab pipeline script?

What I'm trying to do simple looks like:

install:
  stage: install
  
  script:
    - |
       echo "Dummy dry-run installation"
       chmod +x ./install-dry-run.sh
       ./install-dry-run.sh $SA_EMAIL $PROJECT_ID
  rules:
    - when: never

and bash script:

gcloud compute ssh ${instance_name} \
--quiet --tunnel-through-iap --zone=us-east4-c --ssh-key-expire-after=30s \
--project=${PROJECT_ID} \
--impersonate-service-account=${SA_EMAIL} \
--command="\
  if [ -f "/path/to/my/file.txt" ]; then echo 'Process successful'; else echo 'Process failed'; exit 1 > /dev/null; fi \
  " \
  -- -n

but that will not fail when the file is not present.

PS. Proposed solution didn't worked:

Process failed
$ if [ $? -ne 0 ]; then # collapsed multi-line command
Installation successful
JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • it's better to use the `exit` command with a specific exit code to indicate success or failure! – Freeman Aug 29 '23 at 16:51

2 Answers2

0

change the last line of your code to exit with code 1 when the file is not present, then in your GitLab pipeline, check the exit code by using the $? variable and if the exit code is non-zero, it means the script failed!

so in your bash script do like this:

if [ -f "/path/to/my/file.txt" ]; then
  echo 'Process successful'
else
  echo 'Process failed'
  exit 1
fi

and in your GitLab script do like this

install:
  stage: install
  script:
    - |
       echo "Dummy dry-run installation"
       chmod +x ./install-dry-run.sh
       ./install-dry-run.sh $SA_EMAIL $PROJECT_ID
       EXIT_CODE=$?
       if [ $EXIT_CODE -ne 0 ]; then
         echo "Installation failed"
         # Handle failure
         exit $EXIT_CODE
       else
         echo "Installation successful"
         # Handle success
       fi
  rules:
    - when: never

update
as you see the output variable captures the output of the gcloud command and then, using the [[ $output == *"failure"* ]] condition, it checks if the output contains the word failure. If it does, it indicates a failure and the script exits with code 1. Otherwise, it considers the installation successful!

output=$(gcloud compute ssh ${instance_name} \
--quiet --tunnel-through-iap --zone=us-east4-c --ssh-key-expire-after=30s \
--project=${PROJECT_ID} \
--impersonate-service-account=${SA_EMAIL} \
--command="\
  if [ -f "/path/to/my/file.txt" ]; then echo 'success'; else echo 'failure'; fi; \
" \
-- -n)

if [[ $output == *"failure"* ]]; then
   echo "Installation failed"
   exit 1
else 
   echo "Installation successful"
fi
Freeman
  • 9,464
  • 7
  • 35
  • 58
0

Working but kind of "dirty" solution for install-dry-run.sh :

gcloud compute ssh ${instance_name} \
--quiet --tunnel-through-iap --zone=us-east4-c --ssh-key-expire-after=30s \
--project=${PROJECT_ID} \
--impersonate-service-account=${SA_EMAIL} \
--command="\
  if [ -f "/path/to/my/file.txt" ]; then echo 'success'; else echo 'failure'; fi; \
  " \
-- -n > status.txt

if grep -q failure status.txt; then
   echo "Installation failed"
   exit 1
else 
   echo "Installation successful"
fi    
JackTheKnife
  • 3,795
  • 8
  • 57
  • 117