bash 中将 git 仓库链接中的用户名密码信息替换为另一个账号的用户名和密码信息,可以使用sed -r
命令。
适用场景:
gitlab-ci.yml 配置分支推送,将环境变量中的 CI_REPOSITORY_URL
替换为想要的结果。例如,
script: - git fetch - git checkout test - git pull origin test - git merge - - git checkout - - git push $(echo "$CI_REPOSITORY_URL" | sed -r "s/:\/\/.+:.+@/:\/\/$GITLAB_PUSH_ACCOUNT@/g") test
实用范围:linux
核心内容:
$ CI_REPOSITORY_URL="http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@somegit.example.com/group/project.git"$ GITLAB_PUSH_ACCOUNT="thisaccount:thispassword"$ echo "$CI_REPOSITORY_URL" | sed -r "s/:\/\/.+:.+@/:\/\/$GITLAB_PUSH_ACCOUNT@/g"
输出的结果为
http://thisaccount:thispassword@somegit.example.com/group/project.git
如下图,