Thor
Thor

Reputation: 191

Job getting succeeded even when there is an error in gitlab-ci

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?

Upvotes: 1

Views: 3115

Answers (1)

Thor
Thor

Reputation: 191

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

Upvotes: 3

Related Questions