2
votes

I'm trying to use the continuous integration system that Gitlab CI offers to build skills and automatically deploy my repo following a git push my local.

But here are weeks that I can not find the solutions that I decided to use.

Files :

  1. ./.gitlab-ci.yml
  2. ./deploy.sh

gitlab-ci.yml

image: ubuntu:latest

before_script:
  - apt-get install -y
  - apt-get update -y

stages:
  - deploy

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging server"
    - expect ./deploy.sh
  environment:
    name: staging
    url: my.site.com
  only:
  - master

deploy.sh

#!/usr/bin/expect -f

spawn ssh username@host "cd www && git pull https://Username:[email protected]/My/privaterepo.git"

expect "password:"
send "myPassword\n";

interact

My problem is that I often have an error like this :

- expect ./deploy.sh
/bin/bash: line 79: expect: command not found

I had other errors when I enter my sh file from gitlab-ci.yml :

- sh ./deploy.sh ( or bash ./deploy.sh )
./deploy.sh: 6: ./deploy.sh: spawn: not found
./deploy.sh: 8: ./deploy.sh: expect: not found
./deploy.sh: 9: ./deploy.sh: send: not found
./deploy.sh: 11: ./deploy.sh: interact: not found

and when in my terminal of my computer I run expect ./deploy.sh, the deployment is working properly.

i also tried in the before_script to install expect :

- apt-get update expect -y

But I had a question for one package "tzdata" to choose my country. but I can not intervene in the script.

My goal is that every git push of my local, that gitlab it launches a git pull and the update of the code on my site of preprod and prod then (that I intend to block then with a "when : manual "in another task).

Would you have a solution to help me solve this problem because I think it does not take a lot of things that I do not understand ?

Thank you !

1
Quick answer: Use docker, there is no best companion for CI tasks, also, you will be preparing your code and infrastructure for bigger things (easy to deploy on cloud, on local servers, and specially kubernetes clusters)rekiem87
@rekiem87 I have the same problem and want to use docker, but i have not found one single guide that gives a good example and walk-through. Can you please provide either a link or an answer to this question with a basic example? Thanks! :)Newskooler

1 Answers

0
votes

Here is my file that works, I did not put the key on my remote server without password and the file authorized_keys on my server. Do not forget to put it in gitlab.

Now it's working.

variables:
  USERNAME: "$USERNAME_GITLAB" # username
  PASSWORD: "$PASSWORD_GITLAB" # password
  SSH-USER: "$SSH-USER_GITLAB" # ssh-username
  SSH-HOST: "$SSH-HOST_GITLAB" # ssh-host
  SSH_PRIVATE_KEY: "$SSH_PRIVATE_KEY" # private key without password
  REPO: $REPO # gitlab.com/me/repo.git
  COMMANDS: > # commands in your server preprod
    cd www && 
    git pull

before_script:
  ##
  ## 1 Create an ssh key on the preprod server or prod without a password
  ## 2 Copy a pub key for ./ssh/authorized_keys
  ## 3 Copy the same pub key for gitlab ssh key of the profile
  ## 4 Copy the private key for gitlab> repo> params> ci / cd> env variables> $ SSH_PRIVATE_KEY
  ## 5 Try to improve the script
  ##
  ##
  ## Install ssh-agent if not already installed, it is required by Docker.
  ## (change apt-get to yum if you use an RPM-based image)
  ##
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y && apt-get install git -y )'

  ##
  ## Run ssh-agent (inside the build environment)
  ##
  - eval $(ssh-agent -s)

  ##
  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
  ## Private key from the server without password
  ##
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null ## /dev/null = trou noir

  ##
  ## Create the SSH directory and give it the right permissions
  ##
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  - ssh-keyscan charrier.alwaysdata.net >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts

  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

  ##
  ## Optionally, if you will be using any Git commands, set the user name and
  ## and email.
  ##
  - git config --global user.email "[email protected]"
  - git config --global user.name "$USERNAME"

deploy:
  #when: manual 
  script:
    #- ssh -o StrictHostKeyChecking=no $SSH-USER@$SSH-HOST "cd www && git clone https://$USERNAME:$PASSWORD@$REPO"
    - ssh -o StrictHostKeyChecking=no $SSH-USER@$SSH-HOST "$COMMANDS"

  only:
    - master

Thank you.