bash-替换URL链接中的账户信息
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 如下图,