0

I have a pipeline which pushes the packages to a server. In the logs of that script, I can see that even when it has some errors into it(and when I check into that server, the file doesn't appear there, which means there is definetly an error), the job still gets succeeded.

Below is the part of the logs:

ECHO sudo aptly repo add stable abc.deb
Loading packages...
[!] Unable to process abc.deb: stat abc.deb: no such file or directory
[!] Some files were skipped due to errors:
  abc.deb
ECHO sudo aptly snapshot create abc-stable_2023.01.02-09.23.36 from repo stable
Snapshot abc-stable_2023.01.02-09.23.36 successfully created.
You can run 'aptly publish snapshot abc-stable_2023.01.02-09.23.36' to publish snapshot as Debian repository.
ECHO sudo aptly publish -passphrase=12345 switch xenial abc-stable_2023.01.02-09.23.36
ERROR: some files failed to be added
Loading packages...
Generating metadata files and linking package files...
Finalizing metadata files...
gpg: WARNING: unsafe permissions on configuration file `/home/.gnupg/gpg.conf'
gpg: WARNING: unsafe enclosing directory permissions on configuration file `/home/.gnupg/gpg.conf'
Signing file 'Release' with gpg, please enter your passphrase when prompted:
Clearsigning file 'Release' with gpg, please enter your passphrase when prompted:
gpg: WARNING: unsafe permissions on configuration file `/home/.gnupg/gpg.conf'
gpg: WARNING: unsafe enclosing directory permissions on configuration file `/home/.gnupg/gpg.conf'
Cleaning up prefix "." components main...
Publish for snapshot ./xenial [amd64] publishes {main: [abc-stable_2023.01.02-09.23.36]: Snapshot from local repo [stable]: Repository} has been successfully switched to new snapshot.
Cleaning up project directory and file based variables
00:00
Job succeeded

Any idea how to fix this? Like it should fail if there is an error! Can anyone also please explain why it has this behaviour whereas it's the default thing that gitlab pipeline shows error whenever one job fails.

Edit 1: here is the job which is causing the issue

deploy-on:
  stage: deploy
  image: ubuntu:20.04
  before_script:
    - apt-get update
    - apt-get install sshpass -y
    - apt-get install aptly -y
    - apt-get install sudo -y
  script:
    - pOSOP=publisher
    - unstableOrStable=stable
    - chmod +x ./pushToServer.sh
    - ./publishToServer.sh

here is the pushToServer.sh

#!/bin/bash
cat build.env
DebFileNameW=$(cat build.env | grep DebFileNameW | cut -d = -f2)
echo "DebFileNameW=" $DebFileNameW


sshpass -p pass ssh -oStrictHostKeyChecking=no $pOSOP '
echo "ECHO mkdir -p /home/packages/"
mkdir -p /home/packages/
exit
'
sshpass -p pass scp -oStrictHostKeyChecking=no build/$DebFileNameW.deb $pOSOP:/home/packages/


echo "making time"
file_name=$DebFileNameW
current_time=$(date "+%Y.%m.%d-%H.%M.%S")

sshpass -p pass ssh -t -oStrictHostKeyChecking=no $pOSOP '
echo "doing cd"
cd /home/packages

echo "ECHO sudo aptly repo add '$unstableOrStable' '$file_name'.deb"
sudo aptly repo add '$unstableOrStable' '$file_name'.deb
'

Edit 2: At the second last line in pushToServer.sh file, i.e., after line sudo aptly repo add '$unstableOrStable' '$file_name'.deb and before last line which is ', I added these two ways get this done, but still it is not working:

Way 1:

if [[ ! $? -eq 0 ]]; then
    print_error "The last operation failed."

    exit 1
fi

Ways 2:

retVal=$?
echo "ECHOO exit status" $retVal
if [ $retVal -ne 0 ]; then 
   echo "<meaningful message>"
   exit $retVal 
fi

With both the ways it is not working. And the same error.

Output:

ECHO sudo aptly repo add stable abc.deb
Loading packages...
[!] Unable to process abc.deb: stat abc.deb: no such file or directory
ERROR: some files failed to be added
[!] Some files were skipped due to errors:
  abc.deb
ECHO sudo aptly snapshot create abc-stable_2023.01.05-05.59.44 from repo stable
Snapshot abc-stable_2023.01.05-05.59.44 successfully created.
You can run 'aptly publish snapshot abc-stable_2023.01.05-05.59.44' to publish snapshot as Debian repository.
ECHO sudo aptly publish -passphrase=12345 switch xenial abc-stable_2023.01.05-05.59.44
Loading packages...
Generating metadata files and linking package files...
Finalizing metadata files...
Signing file 'Release' with gpg, please enter your passphrase when prompted:
gpg: WARNING: unsafe permissions on configuration file `/home/publisher/.gnupg/gpg.conf'
gpg: WARNING: unsafe enclosing directory permissions on configuration file `/home/publisher/.gnupg/gpg.conf'
gpg: WARNING: unsafe permissions on configuration file `/home/publisher/.gnupg/gpg.conf'
gpg: WARNING: unsafe enclosing directory permissions on configuration file `/home/publisher/.gnupg/gpg.conf'
Clearsigning file 'Release' with gpg, please enter your passphrase when prompted:
Cleaning up prefix "." components main...
Publish for snapshot ./xenial [amd64] publishes {main: [abc-stable_2023.01.05-05.59.44]: Snapshot from local repo [stable]: Repository} has been successfully switched to new snapshot.
ECHOO exit status 0
Cleaning up project directory and file based variables
00:00
Job succeeded

Please note: I have done echo "ECHOO exit status" $retVal statement, and it shows exit status 0, which means $? doesn't have the right value itself. I have expecting $retVal which is '$?', to be 1 or something other than 0(Success) to get it worked.

Any Pointers?

Thor
  • 113
  • 10
  • Can you show us the content of your .gitlab-ci.yml file? – danielnelz Jan 02 '23 at 10:38
  • sure. I have edited the question. Please checl – Thor Jan 02 '23 at 11:22
  • 1
    the job fail or pass depends on the exit code of the script. if the returned exit code after script is 0, the runner will consider the job as successful. try to debug script to see what will be the exit code of the script when it fails. – Harish Barma Jan 02 '23 at 15:19
  • @HarishBarma So you mean to say that I need to check that if there is an error then return exit 1 else exit 0? Can you please specify how can I check this thing if there is an error while running the script? – Thor Jan 03 '23 at 07:54
  • retVal=$? if [ $retVal -ne 0 ]; then echo "" exit $retVal fi – Harish Barma Jan 04 '23 at 14:18
  • @HarishBarma I tried your way (and before that I also tried one more which is quite similar just condition is opposit), and it doesn't seems to be working. I have explained the scenario and cause in Edit 2. Can you please go through it and provide me some more inputs like what can be the possible solution to fix it? – Thor Jan 05 '23 at 06:26

1 Answers1

2

So I have been trying with multiple different ways mentioned in comment replies and my Edits. Nothing worked. So I was able to solve it this way as mentioned here.

I just put these lines at the starting of my script after #!/bin/bash

#!/bin/bash

set -e 
set -o pipefail
Thor
  • 113
  • 10
  • Yeah, that's probably the right way. Your shell script needs to fail overall when one of its commands fails, and setting `-e` and `pipefail` is a good way to accomplish that IMO. You might want to change `set -e` to `set -o errexit` for clarity, though. – Ken Williams Feb 14 '23 at 21:09
  • What differnce would it make if I change "set -e" to "set -o errexit"? – Thor Feb 17 '23 at 10:45
  • They do the same thing, it's just more descriptive to use the long name `-o errexit` instead of `-e`. – Ken Williams Feb 19 '23 at 01:54