代码之家  ›  专栏  ›  技术社区  ›  baptiste

无法从gitlab ci推送。yml公司

  •  40
  • baptiste  · 技术社区  · 7 年前

    我们和同事们一起开发了一个C++库,它每天都变得越来越重要。我们已经通过 gitlab-ci.yml 让我们:

    • &生成;在调试模式下测试
    • 使用Valgrind执行安全检查,如内存泄漏,并检查我们的库中是否有任何我们不想要的清晰符号
    • 生成文档

    所有让我们选择GitLab的东西!

    我们希望对我们的整个库进行分析,并在一个单独的项目中推动基准测试。我们已经使用 SSH密钥

    我们尝试了这样的脚本:

    test_ci_push:
      tags:
        - linux
        - shell
        - light
      stage: profiling
      allow_failure: false
      only:
        - new-benchmark-stage
      script:
        - git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
        - cd benchmarks
        - touch test.dat
        - echo "This is a test" > test.dat
        - git config --global user.name "${GITLAB_USER_NAME}"
        - git config --global user.email "${GITLAB_USER_EMAIL}"
        - git add --all
        - git commit -m "GitLab Runner Push"
        - git push http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
        - cd ..
    

    我们还尝试了一种基本的 git push origin master 推送更新的文件,但每次都得到相同的答案:

    remote: You are not allowed to upload code for this project.
    fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.home/developers/benchmarks.git/': The requested URL returned error: 403
    

    site

    4 回复  |  直到 7 年前
        1
  •  61
  •   Clive Makamara    5 年前

    gitlab ci令牌更像github中的部署密钥。com,因此它只有对存储库的读取权限。要真正推送,您需要生成个人访问令牌并使用它。

    首先,您需要生成令牌,如图所示 here in the gitlab documentation

    最终你的gitlab ci。yml应该是这样的:

    test_ci_push:
      tags:
        - linux
        - shell
        - light
      stage: profiling
      allow_failure: false
      only:
        - new-benchmark-stage
      script:
        - git clone http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git &> /dev/null
        - cd benchmarks
        - echo "This is a test" > test.dat
        - git config --global user.name "${GITLAB_USER_NAME}"
        - git config --global user.email "${GITLAB_USER_EMAIL}"
        - git add --all
        - git commit -m "GitLab Runner Push"
        - git push http://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.mycompany.home/developers/benchmarks.git HEAD:master
        - cd ..
    
        2
  •  30
  •   Melroy van den Berg    3 年前

    虽然前面的答案或多或少是好的,但也有一些重要的哥迪亚。

      before_script:
        - git config --global user.name "${GITLAB_USER_NAME}"
        - git config --global user.email "${GITLAB_USER_EMAIL}"
      script:
        - <do things>
        - git push "https://${GITLAB_USER_LOGIN}:${CI_GIT_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_TAG}"
    

    首先,我们只需要将用户名/电子邮件设置为please git。

    最后,推送https是“好的”,但由于我们不使用存储的ssh密钥,我们应该避免任何可能泄露令牌的事情。首先,虽然gitlab不会在这个命令中打印令牌,但git会很高兴地通知我们,新的上游设置为https://username:thetokeninplaintexthere@url 因此,您的令牌是纯文本的,所以不要使用-u设置上游。

        3
  •  11
  •   Melroy van den Berg    3 年前

    您还可以提供用户和密码(具有写访问权限的用户)作为秘密变量并使用它们。

    例子:

    before_script:
     - git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
     - git config --global user.email 'myuser@mydomain.com'
     - git config --global user.name 'MyUser'
    

    GIT_CI_USER GIT_CI_PASS

    通过这种配置,您可以正常使用git。我使用这种方法在发布后推送标签(使用Axion release Gradle Pling)- http://axion-release-plugin.readthedocs.io/en/latest/index.html

    发布作业示例:

    release:
      stage: release
      script:
        - git branch
        - gradle release -Prelease.disableChecks -Prelease.pushTagsOnly
        - git push --tags
      only:
       - master
    
        4
  •  0
  •   Melroy van den Berg    3 年前

    我正在使用以下GitLab作业:

    repo_pull_sync:
      image: danger89/repo_mirror_pull:latest
      rules:
        - if: '$CI_PIPELINE_SOURCE == "schedule"'
        - if: $REMOTE_URL
        - if: $REMOTE_BRANCH
        - if: $ACCESS_TOKEN
      before_script:
        - git config --global user.name "${GITLAB_USER_NAME}"
        - git config --global user.email "${GITLAB_USER_EMAIL}"
      script:
        - git checkout $CI_DEFAULT_BRANCH
        - git pull
        - git remote remove upstream || true
        - git remote add upstream $REMOTE_URL
        - git fetch upstream
        - git merge upstream/$REMOTE_BRANCH
        - git push "https://${GITLAB_USER_LOGIN}:${ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_DEFAULT_BRANCH}"
    

    danger89/repo_mirror_pull 基于alpine的docker图像, check this GitHub repository for more info

    这个GitLab作业从预定义的远程存储库+分支(参见下面的变量)中提取上游更改,并在CI/CD中本地合并它们,然后再次将它们推送到GitLab中。

    (这在GitLab CE上不是免费的,GitLab中只支持推送镜像)。

    • 通过:CI/CD创建新计划->时间表->新时间表。具有以下3个变量:
    • 保存管道计划

    https://github.com/danger89/repo_pull_sync_docker_image

    关于这个问题,请参阅 git push 命令,它允许您使用GitLab(项目)访问令牌将更改推回到GitLab。