Script ran from Gitlab CI-CD stopped without completing

I have set up gitlab ci-cd in which I call a script to perform some task on my server.

production:
  stage: production
  # when: manual
  script:
    - "which ssh-agent || ( apk update && apk add openssh-client )"
    - ssh [email protected]$DEPLOY_SERVER 'bash -s' < ./myscript.sh
  only:
    - master

Now inside myscript.sh, I checked if docker is installed or not as:

#!/bin/sh

set -e

which docker || ( sudo apt update -y && sudo apt install apt-transport-https ca-certificates curl software-properties-common -y && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" -y && sudo apt update -y && sudo apt-cache policy docker-ce && sudo apt install docker-ce )

sudo docker version

... continue performing the tasks

The problem is if docker is installed then the tasks from sudo docker version to the end of the script is executed.

But if docker is not installed, i.e which docker fails, then the required packages are installed but then the tasks below all fail to execute and gitlab-cicd show Job succeeded without executing other tasks below from sudo docker version to the end.

What is the reason?

Source: StackOverflow