如何在构建时将环境变量设置为 docker 图像名称

How to set environment variable as docker image name on build

当使用文件 circle.yml 文件构建 docker 图像时,我试图传递一个环境变量 $CIRCLE_PROJECT_USERNAME 以便文件不那么特定于项目。不幸的是 Docker 使用标签参数 -t 构建失败并显示:

docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH . returned exit code 1

repository name component must match "[a-z0-9]+(?:[._-][a-z0-9]+)*" Action failed: docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH .

Circle.yml 文件:

machine:
  environment:
  services:
    - docker

dependencies:
  override:
    - docker info
    - docker build -t CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH .

test:
  override:
    - docker run -d $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH

deployment:
  hub:
    branch: master
    commands:
      - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASSWORD
      - docker push $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH

deployment:
  hub:
    branch: develop
    commands:
      - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASSWORD
      - docker push $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:$CIRCLE_BRANCH

应该可以,检查 env 是否实际设置并且只包含允许的字符:

[a-z0-9]+(?:[._-][a-z0-9]+)*

编辑:

docker build -t CIRCLE_PROJECT_USERNAME ...

也许这里少了一个 $? ;)