Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

./deploy.sh not working on gitlab ci

My problem is the bash script I created got this error "/bin/sh: eval: line 88: ./deploy.sh: not found" on gitlab. Below is my sample script .gitlab-ci.yml.

I suspect that gitlab ci is not supporting bash script.

image: docker:latest

variables:
  IMAGE_NAME: registry.gitlab.com/$PROJECT_OWNER/$PROJECT_NAME
  DOCKER_DRIVER: overlay

services:
  - docker:dind

stages:
  - deploy

before_script:
 - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
 - docker pull $IMAGE_NAME:$CI_BUILD_REF_NAME || true

production-deploy:
  stage: deploy
  only:
    - master@$PROJECT_OWNER/$PROJECT_NAME
  script:
    - echo "$PRODUCTION_DOCKER_FILE" > Dockerfile
    - docker build --cache-from $IMAGE_NAME:$CI_BUILD_REF_NAME -t $IMAGE_NAME:$CI_BUILD_REF_NAME .
    - docker push $IMAGE_NAME:$CI_BUILD_REF_NAME
    - echo "$PEM_FILE" > deploy.pem
    - echo "$PRODUCTION_DEPLOY" > deploy.sh
    - chmod 600 deploy.pem
    - chmod 700 deploy.sh
    - ./deploy.sh
  environment:
    name: production
    url: https://www.example.com

And this also my deploy.sh.

#!/bin/bash

ssh -o StrictHostKeyChecking=no -i deploy.pem ec2-user@targetIPAddress << 'ENDSSH'
 // command goes here
ENDSSH

All I want is to execute deploy.sh after docker push but unfortunately got this error about /bin/bash thingy.

I really need your help guys. I will be thankful if you can solve my problem about gitlab ci bash script got error "/bin/sh: eval: line 88: ./deploy.sh: not found".

like image 214
redwolfgang20 Avatar asked Apr 08 '18 20:04

redwolfgang20


People also ask

Why did my pipeline fail GitLab?

A team's build environment or servers could have problems, causing a failed pipeline. That's a key reason why GitLab believes ephemeral builds are important, Brendan says. Ephemeral builds are reproducible meaning they're going to not be impacted because the server went down.

Can we deploy using GitLab?

GitLab CI/CD can automatically build, test, deploy, and monitor your applications by using Auto DevOps.


3 Answers

This is probably related to the fact you are using Docker-in-Docker (docker:dind). Your deploy.sh is requesting /bin/bash as the script executor which is NOT present in that image.

You can test this locally on your computer with Docker:

docker run --rm -it docker:dind bash

It will report an error. So rewrite the first line of deploy.sh to

#!/bin/sh

After fixing that you will run into the problem that the previous answer is addressing: ssh is not installed either. You will need to fix that too!

like image 181
Marco van Neerbos Avatar answered Sep 19 '22 14:09

Marco van Neerbos


docker:latest is based on alpine linux which is very minimalistic and does not have a lot installed by default. For example, ssh is not available out of the box, so if you want to use ssh commands you need to install it first. In your before_script, add:

- apk update && apk add openssh
like image 24
1615903 Avatar answered Sep 17 '22 14:09

1615903


Thanks. This worked for me by adding bash

before_script:
  - apk update && apk add bash

Let me know if that still doesn't work for you.

like image 24
Mradula Ghatiya Avatar answered Sep 19 '22 14:09

Mradula Ghatiya