50

如果 AWS ECR 存储库不存在,我该如何创建它?

4

8 回答 8

70

如果 repo 不存在(或 describe 命令因任何其他原因失败),则创建一个衬垫:

aws ecr describe-repositories --repository-names ${REPO_NAME} || aws ecr create-repository --repository-name ${REPO_NAME}
于 2019-05-16T14:27:42.153 回答
14

AWS 仅在存储库不存在时才创建它。
|| true如果存在相同的存储库,您可以简单地忽略错误和失败:

aws ecr create-repository --repository-name <repo_name> || true
于 2019-12-11T10:57:07.193 回答
6

到目前为止,几乎所有的答案都是调用describe-repositories,如果出现错误,他们认为 repo 不存在。这是错误的,因为还会出现其他类型的错误(没有互联网连接,没有权限(AccessDeniedException),错误的仓库名称,......)。

这意味着如果describe-repositories调用以错误结束,那么我们需要检查错误是否为RepositoryNotFoundException. 只有在这种情况下,我们才应该调用create-repository.

这就是 bash 代码的样子:

output=$(aws ecr describe-repositories --repository-names ${REPO_NAME} 2>&1)

if [ $? -ne 0 ]; then
  if echo ${output} | grep -q RepositoryNotFoundException; then
    aws ecr create-repository --repository-name ${REPO_NAME}
  else
    >&2 echo ${output}
  fi
fi

逐行解释:

output=$(aws ecr describe-repositories --repository-names ${REPO_NAME} 2>&1)- 这会调用describe-repositories并将输出存储到名为 的变量output中。

if [ $? -ne 0 ]; then- 此行检查最后一个命令 ( aws ecs describe-repositories ...) 是否不成功。如果退出代码 ( $?) 不是 0 ( -ne 0),那么我们需要检查错误是什么。如果成功,则无事可做(成功意味着回购已经存在)。

if echo ${output} | grep -q RepositoryNotFoundException; then- 在这一行中,我们正在检查是否因为 repo 不存在而出现错误。如果是,那么我们需要创建 repo:

aws ecr create-repository --repository-name ${REPO_NAME}- 创建回购,我们知道它不存在。

else- else 情况意味着describe-repositories由于其他原因引发错误然后不存在 repo。

>&2 echo ${output}- 在这种情况下,我们不应该尝试创建 repo,而只是在 stderr ( >&2)上输出错误

于 2020-11-06T21:28:32.510 回答
5

You can do this, but you need to check if the repo exists first. I hacked this bash script together and it does what I need:

#!/bin/bash

aws ecr describe-repositories --repository-names $1 2>&1 > /dev/null
status=$?
if [[ ! "${status}" -eq 0 ]]; then
    aws ecr create-repository --repository-name $1
fi

The argument would be some repo name. For this to work in CodeBuild, the job will need an IAM role that permits it to create an ECR repo. If you need to get AWS CLI credentials into your code build job, have a look at this AWS Blog post:

https://aws.amazon.com/blogs/devops/how-to-create-an-ami-builder-with-aws-codebuild-and-hashicorp-packer/

We're doing exactly what is described in the "Create a Build Specification" to use JQ to extract AWS credentials.

于 2019-02-08T18:09:19.313 回答
2

如果您希望在 Jenkins 脚本化管道中自动执行此操作,只需使用以下代码片段:

def ensureRegistry(accountId, region, repoName) {
    Logger log = new Logger(this)
    def accId = shell.output("aws --region ${region} ecr describe-repositories --repository-names \"${repoName}\" | jq .repositories[].registryId | tr -d '\"'")
    if (accId == accountId) {
        log.info("Docker repository ${repoName} exists for account ${accId}")
    } else {
        log.info("Docker repository ${repoName} doesn't exist for account ${accId}")
        shell.status("aws --region ${region} ecr create-repository --repository-name \"${repoName}\"")
        log.info("Docker repository ${repoName} was just created for account ${accId}")
    }
}

shell.groovy是:

def output(cmd) {
    sh(script: cmd, returnStdout: true)
}

def status(cmd) {
    sh(script: cmd, returnStatus: true)
}
于 2019-03-28T11:52:52.170 回答
1

除了有条件地创建 repo,如果您还想提取 repo URI,请考虑以下多行 bash 命令:

REPO_URI=$(aws ecr describe-repositories --repository-names "${REPO_NAME}" --query "repositories[0].repositoryUri" --output text 2>/dev/null || \
           aws ecr create-repository --repository-name "${REPO_NAME}"  --query "repository.repositoryUri" --output text)

repo URI 对于tagandpush操作很有用。


部分学分:JS回答

于 2019-10-17T23:19:52.763 回答
0

要检查 ECR 存储库是否存在,可以使用 double。首先检查描述存储库(如果不存在)然后创建存储库始终使用标签,这有助于审计。

- aws ecr describe-repositories --repository-names ${ECRImage} || aws ecr create-repository --repository-name ${ECRImage} --tags Key=Domain,Value=$Domain Key=AppEnv,Value=$AppEnv Key=ApplicationCI,Value=$ApplicationCI Key=Owner,Value=$Owner Key=Requester,Value=$Requester Key=CostCenter,Value=$CostCenter

于 2020-10-01T18:28:51.147 回答
0
export ECR_REPO=`aws ecr describe-repositories --repository-names $REPO_NAME 2>/dev/null | jq .repositories[0].repositoryUri | tr -d \\\" && aws ecr create-repository --repository-name $REPO_NAME --region us-east-1 2>/dev/null | jq .repository.repositoryUri | tr -d \\\"`

这适用于 buildspec.yml 文件,用于始终获取 repo 名称并将其存储在 ECR_REPO var 中。如果它已经存在,它将创建 repo 或静默失败。如果它确实存在,它将获取 repo 名称,如果不存在,它将静默失败。

于 2020-05-02T20:17:21.430 回答