title: Gitlab CI check if directory exists before pulling origin
body: <p>I'm trying to deploy my flask application to AWS EC2 instance using gitlab ci runner.</p>
  <blockquote>
  <p>.gitlab.ci.yml</p>
  </blockquote>
  <pre><code>stages:
    - test
    - deploy
  
  test_app:
    image: python:latest
    stage: test
    before_script:
    - python -V
    - pip install virtualenv
    - virtualenv env
    - source env/bin/activate
    - pip install flask
    script:
    - cd flask-ci-cd
    - python test.py
  
  prod-deploy:
    stage: deploy
    only:
      - master # Run this job only on  changes for stage branch
  
    before_script:
      - mkdir -p ~/.ssh
      - echo -e &quot;$RSA_KEY&quot; &gt; ~/.ssh/id_rsa
      - chmod 600 ~/.ssh/id_rsa
      - '[[ -f /.dockerenv ]] &amp;&amp; echo -e &quot;Host *\n\tStrictHostKeyChecking no\n\n&quot; &gt; ~/.ssh/config'
  
    script:
      - bash .gitlab-deploy-prod.sh
  
    environment:
      name: deploy
  </code></pre>
  <blockquote>
  <p>.gitlab-deploy-prod.sh</p>
  </blockquote>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  # Get servers list
  set -f
  
  # access server terminal
  shell=&quot;ssh -o StrictHostKeyChecking=no ${SERVER_URL}&quot;
  git_token=$DEPLOY_TOKEN
  
  echo &quot;Deploy project on server ${SERVER_URL}&quot;
  if [ ${shell} -d &quot;/flask-ci-cd&quot; ] # check if directory exists
  then
     eval &quot;${shell} cd flask-ci-cd &amp;&amp; git clone https://sbhusal123:${git_token}@gitlab.com/sbhusal123/flask-ci-cd.git master &amp;&amp; cd flask-ci-cd&quot;
  else
      eval &quot;${shell} git pull https://sbhusal123:${git_token}@gitlab.com/sbhusal123/flask-ci-cd.git master &amp;&amp; cd flask-ci-cd &amp;&amp; cd flask-ci-cd&quot;
  fi
  </code></pre>
  <blockquote>
  <p>Error: .gitlab-deploy-prod.sh: line 7: -o: command not found</p>
  </blockquote>
  <p>How can i check if directory existing??</p>
  <blockquote>
  <p>What i've tried.</p>
  </blockquote>
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  
  # Get servers list
  set -f
  
  # access server terminal
  shell=&quot;ssh -o StrictHostKeyChecking=no ${SERVER_URL}&quot;
  git_token=$DEPLOY_TOKEN
  
  eval &quot;${shell}&quot; # i thought gitlab would provide me with shell access
  echo &quot;Deploy project on server ${SERVER_URL}&quot;
  
  if [-d &quot;/flask-ci-cd&quot; ] # check if directory exists
  then
     eval &quot;cd flask-ci-cd &amp;&amp; git clone https://sbhusal123:${git_token}@gitlab.com/sbhusal123/flask-ci-cd.git master &amp;&amp; cd flask-ci-cd&quot;
  else
      eval &quot;git pull https://sbhusal123:${git_token}@gitlab.com/sbhusal123/flask-ci-cd.git master &amp;&amp; cd flask-ci-cd &amp;&amp; cd flask-ci-cd&quot;
  fi
  </code></pre>
  <p>I've tried to log into the ssh shell before executing the scripts inside if else. But it doesn't works the way intended.</p>