在Jenkins作业中使用带有私有gitlab repo的ansible-galaxy的凭据

oyt4ldly  于 2022-11-01  发布在  Jenkins
关注(0)|答案(2)|浏览(143)

我有一组角色,我需要与ansible-galaxy一起安装。

- src: 'https://gitlab.private/role-openstack-net.git'
  scm: 'git'
  version: '1.0.0'
  name: 'role-openstack-net'

- src: 'https://gitlab.private/role-openstack-subnet.git'
  scm: 'git'
  version: '1.0.0'
  name: 'role-openstack-subnet'

真实的上,我有大约20个角色。
所有的角色都是私有的,所以当我运行:

ansible-galaxy install -f -c -r galaxy.yml

它要求我提供每个角色的用户/通行证,这有点麻烦
手动执行以下操作:

git config --global credential.helper store

我输入凭据一次,它就会记住所有凭据
但是我该怎么做Jenkins的工作呢?
我看到这里有一种摆放令牌的方式:
https://github.com/ansible/ansible/pull/34621
但似乎不起作用。
你知道吗?

6pp0gazn

6pp0gazn1#

当前不支持在运行时将凭据参数传递到ansible-galaxy。
可以将凭据添加到requirements.yml中,但通常将凭据添加到代码中并不理想,因为其他人有一天可能会利用它们。
解决方案是在运行时更新requirements.yml
通过查看您的个人资料并更新设置来创建Gitlab个人访问令牌:https://private.gitlab/profile/personal_access_tokens
使用您选择的密码管理器在运行时使用令牌设置变量PAT_TOKEN。
在Jenkins脚本中,使用sed更新ansible-galaxy install之前的requirements.yml

sed -i "s#https://gitlab.private/#https://oauth2:$PAT_TOKEN@gitlab.private/#g requirements.yml

如果您使用的是Gitlab-ci而不是Jenkins,则可以使用现有的ci标记:

sed -i "s#https://gitlab.private/#https://gitlab-ci-token:$CI_JOB_TOKEN@gitlab.private/#g requirements.yml
mfpqipee

mfpqipee2#

如果你已经安装了git,你可以在gitlab中使用这个:

git config --global credential.helper store
echo "https://gitlab-ci-token:${CI_JOB_TOKEN}@$mygitlab.com" >>  ~/.git-credentials
chmod 600 ~/.git-credentials

CI_JOB_TOKEN是运行程序用于提取代码的标记。您可以使用自己的标记(安全性较低)

相关问题