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 a Docker Artifact.
Push to CNB Docker Artifact
CNB pipelines include built-in credentials of the triggerer, which can be used directly to log into the CNB Docker Artifact and push images.
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 Official or Other Docker Artifacts
To push to the official or other Docker Artifacts, 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
DOCKER_USER: user
DOCKER_PWD: password
DOCKER_REGISTRY: docker.io
DOCKER_GROUP: group_name
DOCKER_REPO_NAME: repo_name