Gitlab-ci和deploy.sh

时间:2018-08-28 14:19:04

标签: git ubuntu gitlab gitlab-ci

我正在尝试使用Gitlab CI提供的持续集成系统来构建技能,并在git push local之后自动部署我的仓库。

但是这几周来我找不到我决定使用的解决方案。

文件:

  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:myPassword@gitlab.com/My/privaterepo.git"

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

interact

我的问题是我经常会遇到这样的错误:

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

当我从gitlab-ci.yml输入sh文件时,我还有其他错误:

- 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

当我在计算机的终端中运行Expect ./deploy.sh时,部署工作正常。

我还尝试在before_script中安装Expect:

- apt-get update expect -y

但是我有一个问题要选择一个包“ tzdata”来选择我的国家。但我不能干预脚本。

我的目标是我本地的每一次git push,gitlab都会在我的preprod和prod网站上启动git pull和代码更新(然后我打算在“另一个任务)。

您是否有解决方案来帮助我解决此问题,因为我认为这不需要很多我不理解的事情?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是我可以使用的文件,没有在没有密码的情况下将密钥放在远程服务器上,在服务器上没有文件authorized_keys的情况。不要忘记将其放在gitlab中。

现在可以使用了。

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 "username@mail.com"
  - 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

谢谢。