Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gitlab ci和deploy.sh_Git_Ubuntu_Gitlab_Gitlab Ci - Fatal编程技术网

Gitlab ci和deploy.sh

Gitlab ci和deploy.sh,git,ubuntu,gitlab,gitlab-ci,Git,Ubuntu,Gitlab,Gitlab Ci,我正在尝试使用Gitlab CI提供的持续集成系统来构建技能,并在git推送我的本地应用程序后自动部署我的repo 但在这几周里,我无法找到我决定使用的解决方案 档案: ./.gitlab-ci.yml /deploy.sh gitlab ci.yml image: ubuntu:latest before_script: - apt-get install -y - apt-get update -y stages: - deploy deploy_staging: st

我正在尝试使用Gitlab CI提供的持续集成系统来构建技能,并在git推送我的本地应用程序后自动部署我的repo

但在这几周里,我无法找到我决定使用的解决方案

档案:

  • ./.gitlab-ci.yml
  • /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_脚本中安装expect:

    - apt-get update expect -y
    
    但我有一个问题,那就是“tzdata”套餐如何选择我的国家。但我不能干预剧本

    我的目标是让我本地的每一次git推送,gitlab it在我的Prepod和prod站点上启动一次git推送和代码更新(我打算在另一个任务中用“when:manual”来阻止)

    你有没有办法帮我解决这个问题,因为我认为这不需要很多我不懂的东西


    谢谢大家!

    这是我的文件,我没有在没有密码的情况下将密钥放在我的远程服务器上,也没有将文件授权密钥放在我的服务器上。别忘了把它放在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
    

    谢谢。

    快速回答:使用docker,CI任务没有最好的伴侣,而且,您将为更大的事情准备代码和基础设施(易于部署在云端、本地服务器上,特别是kubernetes群集上)@rekiem87我也有同样的问题,希望使用docker,但我还没有找到一个能给出一个很好的例子的指南。你能提供一个链接或者一个基本例子来回答这个问题吗?谢谢!:)我认为
    变量中不能有破折号。例如,
    SSH-USER
    应该是
    SSH\u USER
    ,否则我会得到一个错误。另外,这样运行会产生另一个错误:
    /bin/sh:eval:line 88:apt get:not found错误:作业失败:退出代码127
    ,因为没有设置docker映像。