Gitlab-ci和deploy.sh

问题描述 投票:1回答:1

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

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

档案:

  1. ./.git lab-此.阴谋论
  2. ./deploy.是

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.是

#!/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

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

- 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它会启动git pull并在我的preprod和prod网站上更新代码然后(我打算在另一个任务中使用“when:manual”来阻止) 。

你有解决方案可以帮助我解决这个问题,因为我觉得它不需要很多我不理解的事情吗?

谢谢 !

git ubuntu gitlab gitlab-ci
1个回答
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 "[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

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.