【发布时间】:2020-08-26 23:28:55
【问题描述】:
我在 Jenkins 管道中有两个 groovy 函数,它们一起登录到 ECR 存储库并构建一个 docker 容器。它看起来像这样:
def login() {
sh "aws ecr get-login --registry-ids <id> --region <region> --no-include-email"
sh "aws ecr get-login --region <region> --no-include-email"
}
def build(project, tag) {
login()
sh "docker build -t ${project}:${tag} ."
}
但是,当我运行它时,我得到pull access denied,就好像我从未登录过一样。我推测这是因为 aws ecr 登录命令在它们自己的 shell 中运行,而构建命令在另一个 shell 中运行。理想情况下,我想利用这种功能分解和 groovy 的其他特性,但在一个进程/shell 中运行 shell 命令。这可能吗?我怎样才能做到这一点?
【问题讨论】:
-
为什么不将所有这些命令放在一个
sh中? -
基本上是因为如果我这样做,我将无法重用函数。例如,如果我有另一个,例如
push,理想情况下,我希望重复使用login函数,而不必在需要的地方复制和粘贴这些行。
标签: jenkins groovy jenkins-pipeline jenkins-groovy