Initial commit of the scripts used in our Jenkins pipelines.
[arvados-dev.git] / jenkins / run-deploy.sh
1 #!/bin/bash
2
3 IDENTIFIER=$1
4 DEPLOY_REPO=$2
5
6 if [[ "$IDENTIFIER" == '' ]]; then
7   echo "Syntax: $0 <identifier> <deploy_repo_name>"
8   exit 1
9 fi
10
11 if [[ "$DEPLOY_REPO" == '' ]]; then
12   echo "Syntax: $0 <identifier> <deploy_repo_name>"
13   exit 1
14 fi
15
16 EXITCODE=0
17
18 COLUMNS=80
19
20 title () {
21   printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
22 }
23
24 # We only install capistrano in dev mode
25 export RAILS_ENV=development
26
27 source /etc/profile.d/rvm.sh
28 echo $WORKSPACE
29
30 # Weirdly, jenkins/rvm ties itself in a knot.
31 rvm use default
32
33 # Just say what version of ruby we're running
34 ruby --version
35
36 function ensure_symlink() {
37   if [[ ! -L $WORKSPACE/$1 ]]; then
38     ln -s $WORKSPACE/$DEPLOY_REPO/$1 $WORKSPACE/$1
39   fi
40 }
41
42 # Check out/update the $DEPLOY_REPO repository
43 if [[ ! -d $DEPLOY_REPO ]]; then
44   mkdir $DEPLOY_REPO
45   git clone git@git.curoverse.com:$DEPLOY_REPO.git
46 else
47   cd $DEPLOY_REPO
48   git pull
49 fi
50
51 # Make sure the necessary symlinks are in place
52 cd "$WORKSPACE"
53 ensure_symlink "apps/workbench/Capfile.workbench.$IDENTIFIER"
54 ensure_symlink "apps/workbench/config/deploy.common.rb"
55 ensure_symlink "apps/workbench/config/deploy.curoverse.rb"
56 ensure_symlink "apps/workbench/config/deploy.workbench.$IDENTIFIER.rb"
57
58 ensure_symlink "services/api/Capfile.$IDENTIFIER"
59 ensure_symlink "services/api/config/deploy.common.rb"
60 ensure_symlink "services/api/config/deploy.$IDENTIFIER.rb"
61
62 # Deploy API server
63 title "Deploying API server"
64 cd "$WORKSPACE"
65 cd services/api
66
67 bundle install --deployment
68
69 # make sure we do not print the output of config:check
70 sed -i'' -e "s/RAILS_ENV=production #{rake} config:check/RAILS_ENV=production QUIET=true #{rake} config:check/" $WORKSPACE/$DEPLOY_REPO/services/api/config/deploy.common.rb
71
72 bundle exec cap deploy -f Capfile.$IDENTIFIER
73
74 ECODE=$?
75
76 # restore unaltered deploy.common.rb
77 cd $WORKSPACE/$DEPLOY_REPO
78 git checkout services/api/config/deploy.common.rb
79
80 if [[ "$ECODE" != "0" ]]; then
81   title "!!!!!! DEPLOYING API SERVER FAILED !!!!!!"
82   EXITCODE=$(($EXITCODE + $ECODE))
83   exit $EXITCODE
84 fi
85
86 title "Deploying API server complete"
87
88 # Deploy Workbench
89 title "Deploying workbench"
90 cd "$WORKSPACE"
91 cd apps/workbench
92 bundle install --deployment
93
94 # make sure we do not print the output of config:check
95 sed -i'' -e "s/RAILS_ENV=production #{rake} config:check/RAILS_ENV=production QUIET=true #{rake} config:check/" $WORKSPACE/$DEPLOY_REPO/apps/workbench/config/deploy.common.rb
96
97 bundle exec cap deploy -f Capfile.workbench.$IDENTIFIER
98
99 ECODE=$?
100
101 # restore unaltered deploy.common.rb
102 cd $WORKSPACE/$DEPLOY_REPO
103 git checkout apps/workbench/config/deploy.common.rb
104
105 if [[ "$ECODE" != "0" ]]; then
106   title "!!!!!! DEPLOYING WORKBENCH FAILED !!!!!!"
107   EXITCODE=$(($EXITCODE + $ECODE))
108   exit $EXITCODE
109 fi
110
111 title "Deploying workbench complete"
112
113 exit $EXITCODE