Search code examples
linuxbashgitlabcicd

Unable to do condition check inside gitlab cicd


I'm unable to test a condition in gitlab cicd. Here is the condition check I wanted to do.

count=docker ps -aq | wc -l && if [ "$count" -gt 0 ]; then echo "TESTING $count";fi

It works fine within bash shell but doesn't work inside gitlab-runner

deploy:
    stage: deploy
    before_script:
        - chmod 400 $SSH_KEY 
    script: 
        - ssh -o StrictHostKeyChecking=no -i $SSH_KEY [email protected]" 
            docker login -u $REGISTRY_USER -p $REGISTRY_PASS && 
            count=`docker ps -aq | wc -l` && if [  "$count" -gt 0 ]; then echo "TESTING $count";fi " 

I get the following error unary operator expected any idea why?

enter image description here


Solution

  • Figured it out.

    I had to escape the special characters when using ssh.

    deploy:
        stage: deploy
        before_script:
            - chmod 400 $SSH_KEY 
        script: 
            - ssh -o StrictHostKeyChecking=no -i $SSH_KEY [email protected]" 
                docker login -u $REGISTRY_USER -p $REGISTRY_PASS && 
                count=\`docker ps -aq | wc -l\` && if \[  "\$count" -gt 0 \]; then echo "TESTING \$count";fi "