2

I have a small shell script for deployment which I use in my bitbucket pipeline.

I successfully tested that bitbucket pipeline executes the script on my server with this step (I tried it by writing a one-line script which just inserts a file on the server -> it was created correctly after the pipe ran):

  - step:
     name: Deploy dev 
     trigger: manual
     script:
      - cat ./deploy.sh | ssh -T user@[myIp]

When I execute my deploy-script directly on the server after logging in via ssh it works great. But when the BitBucket Pipeline executes it, the last four commands are skipped for some reason.

This is the deploy.sh file :

#!/bin/bash
env="dev"
if [ $# -gt 0 ]; then
  env=$1
fi

basePath="/path/to/my/project/${env}"

echo "---------------------------------------- | DEPLOYING TO ${env}"
cd ${basePath}
git stash
git pull

echo "---------------------------------------- | BUILD ANGULAR"
cd ${basePath}/Web/project
ng build

echo "---------------------------------------- | BUILD API"
cd ${basePath}/API
./gradlew build

echo "---------------------------------------- | KILL API"
sh ${basePath}/killAllServers.sh
mv ${basePath}/killAllServers.sh ${basePath}/log/archive/`date +%Y-%m-%d-%H:%M:%S`-killed.log

echo "---------------------------------------- | CLEAR LOGS"
mv ${basePath}/log/java-spring.log ${basePath}/log/archive/`date +%Y-%m-%d-%H:%M:%S`-spring.log

echo "---------------------------------------- | STARTING API"
java -jar ${basePath}/path/to/my/build.jar --spring.profiles.active=dev >> ${basePath}/log/java-spring.log  & echo "kill $!  #started on `date +%Y-%m-%d-%H:%M:%S`" >> ${basePath}/killAllServers.sh

Now when I execute it on my server I get the following output (I emitted some outputs from angular and gradle to make it easier readable):

user@[myIp]:/path/to/my/project$ sh deploy.sh
---------------------------------------- | DEPLOYING TO dev
Saved working directory and index state ... /**Git output**/
---------------------------------------- | BUILD ANGULAR
Your global Angular CLI version /**Angular Output**/
---------------------------------------- | BUILD API

BUILD SUCCESSFUL in 6s
3 actionable tasks: 3 up-to-date
---------------------------------------- | KILL API
---------------------------------------- | CLEAR LOGS
---------------------------------------- | STARTING API

When I execute it via the BitBucket Pipeline, this is the output that I get:

+ cat ./deploy.sh | ssh -T user@[myIp]
Welcome to Ubuntu /** Ubuntu Output**/
---------------------------------------- | DEPLOYING TO dev
Saved working directory and index state /** Git output**/
---------------------------------------- | BUILD ANGULAR
Your global Angular CLI version /** Angular Output**/
---------------------------------------- | BUILD API
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :bootJar UP-TO-DATE
> Task :jar SKIPPED
> Task :assemble UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build UP-TO-DATE
BUILD SUCCESSFUL in 5s
3 actionable tasks: 3 up-to-date

I have no idea why the script doesn't execute the rest of the commands as well when executing via the pipeline.

Juliette
  • 966
  • 16
  • 35
  • i have no idea about `gradlew`, but i stumbled upon it in a few job-control related questions. so most likely your problem is mostly with `gradlew` itself (rather than "bitbucket" or "deployment".) – umläute Mar 30 '19 at 08:59
  • thanks for the tips, I updated the tags – Juliette Mar 30 '19 at 09:11
  • it seems to be the same problem as in this question: https://stackoverflow.com/questions/40972099/gradle-exit-in-a-shell-script apparently gradle sends an exit command that closes the script if it was executed with a pipe – Juliette Mar 30 '19 at 11:37

1 Answers1

1

I found the solution:

The problem seems to be that gradle build sends an exit command after it is finished. This doesn't cause a problem when the script that called gradle build was called directly. That is why it worked when I executed it directly on the server.

But when it was called with a pipe, the script stops as well. A workaround that I found was that it is possible to create another script called onlyGradle.sh that only contains the ./gradlew build command and is called with sh onlyGradle.sh in my deploy.sh Then the deploy.sh continues running after the gradle build -> even in bitbucket

Juliette
  • 966
  • 16
  • 35