通过CI runner将文件推送到gitlab-ci

Ven*_*kat 19 gitlab gitlab-ci gitlab-ci-runner

我正在使用gitlab CI runner来测试我的代码并生成一些文件.我只是想通过CI runner将生成的文件推送到gitlab存储库.有没有办法做到这一点?

jmu*_*ire 18

我通过这样做解决了这个问题:

  1. 使用api范围生成新的gitlab访问令牌: User Settings > Access Tokens
  2. 使用新令牌将受保护的CI变量添加到项目设置中: Your project > Settings > Secret variable

- > edit:如果要将git push推送到非受保护的分支,请不要将runner变量设置为protected

然后,您可以在gitlab-ci脚本中使用此标记而不是默认标记.例如 :

before_script:
  - git remote set-url origin https://USERNAME:${CI_PUSH_TOKEN}@gitlab.com/your-project.git
  - git config --global user.email 'your@email.com'
  - git config --global user.name 'yourname'

...

- git checkout -B branch
- change files
- git commit -m '[skip ci] commit from CI runner'
- git push --follow-tags origin branch
Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试过这个,它抛出:`致命:'https://[secure]@example.com/dka/duma.git/'的身份验证失败`,我是存储库的所有者,令牌是我的。 (2认同)
  • 1. 请使用`${CI_PROJECT_NAME}`代替`your-project`,使用`${GITLAB_USER_EMAIL}`代替`your@email.com`,使用`${GITLAB_USER_ID}`代替`yourname`。2. 请注意`CI_PUSH_TOKEN`是变量,必须包含API Token。3. 也许 Gitlab 用户界面自您的回答以来发生了变化,现在是:“您的项目 > 设置 > CI / CD > 环境变量”,您必须在其中设置“CI_PUSH_TOKEN”。如果你设置了 `protected` 属性,那么这个变量只会暴露给 `protected` 分支上的 gitlab 运行者(比如 `master 分支`,默认情况下是受保护的)。 (2认同)

Ven*_*kat 8

在gitlab中生成SSH密钥

- >配置文件设置 - > SSH密钥 - >生成它

生成SSH密钥存储后,在名为SSH的gitlab 变量中

- >项目设置 - >变量 - >添加变量

在.gitlab-ci.yml中添加以下行.

before_script:
   - mkdir -p ~/.ssh
   - echo "$SSH" | tr -d '\r' > ~/.ssh/id_rsa
   - chmod 600 ~/.ssh/id_rsa
   - ssh-keyscan -H 'Git_Domain' >> ~/.ssh/known_hosts 
Run Code Online (Sandbox Code Playgroud)

之后使用下面的js代码将文件推送到存储库.

var child_process = require("child_process");
child_process.execSync("git checkout -B 'Your_Branch'");
child_process.execSync("git remote set-url origin Your_Repository_Git_Url");
child_process.execSync("git config --global user.email 'Your_Email_ID'");
child_process.execSync("git config --global user.name 'Your_User_Name'");
for (var i=0;i<filesToBeAdded.length;i++) {
          child_process.execSync("git add "+filesToBeAdded[i]);
}   
var ciLog = child_process.execSync("git commit -m '[skip ci]Automated commit for CI'");
var pushLog = child_process.execSync("git push origin Your_Branch");
Run Code Online (Sandbox Code Playgroud)

[skip ci]在提交消息中最重要.否则它将启动CI过程的无限循环.


Prz*_*wak 5

您当然可以使用 SSH 密钥,但您也可以提供用户和密码(具有写入权限的用户)作为秘密变量并使用它们。

例子:

before_script:
 - git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS@gitlab.com/$CI_PROJECT_PATH.git
 - git config --global user.email 'myuser@mydomain.com'
 - git config --global user.name 'MyUser'
Run Code Online (Sandbox Code Playgroud)

您必须将GIT_CI_USER和定义GIT_CI_PASS为秘密变量(您始终可以为此目的创建专用用户)。

通过此配置,您可以正常使用 git。我正在使用这种方法在发布后推送标签(使用 Axion Release Gradle Pluing - 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
Run Code Online (Sandbox Code Playgroud)


Fai*_*iry -1

您正在寻找的功能称为工件。工件是成功构建时附加到构建的文件。

要启用 Artifact,请将其放入您的 .gitlab-ci.yml 中:

artifacts:
  paths:
    - dir/
    - singlefile
Run Code Online (Sandbox Code Playgroud)

这会将dir目录和文件上传singlefile回 GitLab。

  • 是的,它将开始无限循环。但是当我通过跑步者提交时,我使用“[skip ci]”关键字。通过这种方式我可以消除无限循环。 (11认同)
  • 但我真的想将文件作为源文件推送到存储库。 (3认同)
  • @VenkatGan 但为什么呢?如果您使用运行器将任何内容推送到存储库,您只需再次启动运行器即可。这将导致无限循环。 (2认同)