gpt4 book ai didi

Gitlab-ci 和 deploy.sh

转载 作者:行者123 更新时间:2023-12-04 18:33:50 25 4
gpt4 key购买 nike

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

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

文件:

  • ./.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

    部署.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 它都会启动一个 git pull 并更新我的 preprod 和 prod 站点上的代码(我打算在另一个任务中使用“何时:手动”来阻止) .

    你有一个解决方案来帮助我解决这个问题,因为我认为它不需要很多我不明白的东西吗?

    谢谢 !

    最佳答案

    这是我的有效文件,我没有在没有密码的情况下将 key 放在我的远程服务器上,并将文件 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

    谢谢你。

    关于Gitlab-ci 和 deploy.sh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060111/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com