Build and Push Docker Image to Artifact
About 260 wordsLess than 1 minute
This article describes how to build a Docker image in a pipeline and push it to an artifact registry.
Push to CNB Docker Artifact
CNB pipelines include built-in credentials of the triggerer, which can be used to log into the CNB Docker Artifact and push images directly.
Example configuration:
.cnb.yml
main:
push:
- services:
- docker # After declaration, docker commands can be used directly in the pipeline
stages:
- name: set docker tag
script: echo -n "${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:latest"
exports:
info: IMAGE_TAG
- name: docker build
script: docker build -t $IMAGE_TAG .
- name: push image
script: docker push $IMAGE_TAGPush to Third-party Docker Artifacts
To push to third-party Docker artifact registries, store Docker credentials in a secret repository, then import them into the pipeline via the imports syntax.
Example configuration:
.cnb.yml
main:
push:
- services:
- docker # After declaration, docker commands can be used directly in the pipeline
# Import Docker credentials from a secret repository into environment variables
imports: https://cnb.cool/<your-repo-slug>/-/blob/main/xxx/docker-envs.yml
stages:
- name: set docker tag
script: echo -n "${DOCKER_REGISTRY}/${DOCKER_GROUP}$/${DOCKER_REPO_NAME}$:$CNB_COMMIT_SHORT"
exports:
info: IMAGE_TAG
- name: docker build
script: docker build -t $IMAGE_TAG .
- name: push image
script: docker push $IMAGE_TAGdocker-envs.yml file content:
DOCKER_USER: user
DOCKER_PWD: password
DOCKER_REGISTRY: docker.io
DOCKER_GROUP: group_name
DOCKER_REPO_NAME: repo_name