bash-替换URL链接中的账户信息

4次阅读

共计 593 个字符,预计需要花费 2 分钟才能阅读完成。

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

如下图,

正文完
 0