从 CI 推送到存储库

时间:2021-01-14 14:30:33

标签: bash powershell gitlab-ci gitlab-ci-runner

我对 CI 相当陌生,只是想从一个自动变更日志系统开始。有一段时间我取得了非常好的进展,直到我想将更改推送回存储库。

我尝试了各种示例 like most of this,但仍然很难做到正确。

这是我上次使用的 yaml 文件(我用 等替换了我们的 URL 等):

stages:
  - test

test_a:
  stage: test
  before_script:
    ##
    # Set ssh-agent to start manual
    ##
    - Get-Service -Name ssh-agent | Set-Service -StartupType Manual
    
    ##
    # Start SSH-Agent and check if it is running
    ##
    - Start-Service ssh-agent
    - Get-Service ssh-agent
    ##
    ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    ## We're using Replace 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
    ##
    - echo $SSH_KEY_RUNNER.Replace(';',"`r`n") | ssh-add -

    ##
    ## Create the SSH directory and give it the right permissions
    ##
    - if(!(Test-Path -path ~/.ssh)){ mkdir -p ~/.ssh} else {echo ".ssh directory is not created!"}
    
    ##
    ## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
    ## with your own domain name. You can copy and repeat that command if you have
    ## more than one server to connect to.
    ##
    # - Add-Content ~/.ssh/known_hosts (ssh-keyscan gitlab.com) 

    ##
    ## Alternatively, assuming you created the SSH_SERVER_HOSTKEYS variable
    ## previously, uncomment the following two lines instead.
    ##
    - ((Remove-Item ~/.ssh/known_hosts) -and (echo "File known_hosts was removed!")) -or (echo "File known_hosts does not exist!")
    - Add-Content ~/.ssh/known_hosts "$SSH_SERVER_HOSTKEYS"

    ##
    ## Optionally, if you will be using any Git commands, set the user name and
    ## and email.
    ##
    - git config --global user.email "<OUR_GIT_MAIL>"
    - git config --global user.name "Gitlab.Runner"

  script:
    ##
    ## Message for the actual Script
    ##
    - echo "This Job runs if any file is changed, except CHANGELOG.md"

    ##
    ## run the run_update_changelog_script.ps1
    ##
    - powershell.exe -File ".\.gitlab-ci-scripte\run_update_changelog_script.ps1" -SHA $CI_COMMIT_SHA -SHORT_SHA $CI_COMMIT_SHORT_SHA -TITLE $CI_COMMIT_TITLE -URL $CI_PROJECT_URL -DESCRIPTION $CI_COMMIT_DESCRIPTION -USER_ID $GITLAB_USER_ID -USER_NAME $GITLAB_USER_LOGIN
    
    ##
    ## show changes in the changelog
    ##
    - Get-Content CHANGELOG.md
    
    ##
    ## do the git stuff
    ##
    - git status
    - git add CHANGELOG.md
    - (git commit -m "Update CHANGELOG.md") -or (echo "No changes, nothing to commit!")
    - git status
    - git remote show origin
    - git remote set-url --push origin git@<OUR_URL>:$CI_PROJECT_PATH.git
    - git remote show origin
    - git push --follow-tags origin HEAD:$CI_COMMIT_REF_NAME
  tags:
    - windows
  except:
    changes:
      - "CHANGELOG.md"

我不完全确定是什么导致了以下错误。但是为了获得完整的信息,我将私钥作为变量放在名为 SSH_KEY_RUNNER 的 REPO 设置中,将公钥作为部署密钥:

$ git push --follow-tags origin HEAD:$CI_COMMIT_REF_NAME
Warning: Permanently added the ECDSA host key for IP address 'X.X.X.X' to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
git@<OUR_URL>: Permission denied (publickey,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

我知道我的 and-or-command 没有按预期工作,但目前它们并不重要。

最后一条信息:gitlab-runner 在 Powershell 上运行,您可能已经知道了。部署密钥设置为写入访问权限。

附加测试

我切换到 Linux 运行器而不是 Windows 运行器来测试我是否得到了更令人满意的结果。 yaml 文件几乎相同,我只是切换到 linux 命令而不是 powershell。它主要是从 gitlab 网站复制和粘贴,并进行了一些较小的更改(不同的 var 名称等)

结果不同但几乎相同:

$ git push --follow-tags origin HEAD:$CI_COMMIT_REF_NAME
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure that you have the correct access rights
and the repository exists.

0 个答案:

没有答案