繁体   English   中英

Gitlab-ci和deploy.sh

[英]Gitlab-ci and deploy.sh

我正在尝试使用Gitlab CI提供的持续集成系统来构建技能并在git push my 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中安装期望:

- apt-get update expect -y

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

我的目标是我本地的每一次git push,gitlab都会在我的preprod和prod网站上启动git pull和代码更新(然后我打算在另一项任务中使用“ when:manual”阻止) 。

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

谢谢 !

这是我可以使用的文件,没有在没有密码的情况下将密钥放在远程服务器上,而在服务器上没有将文件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

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM