Build and Push Docker Image to Artifact
About 236 wordsLess than 1 minute
This article will introduce how to build a Docker image in a pipeline and push it to a Docker Artifact.
Push to CNB Docker Artifact
The pipeline in CNB includes built-in credentials of the pipeline triggerer, which can be directly used to log in and push to the CNB Docker Artifact.
Example configuration:
master:
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_TAG
Push to Official or Other Docker Artifacts
For pushing to official or other Docker Artifacts, you can store the DOCKER credentials in a secret repository and import them into the pipeline using the imports syntax.
Example configuration:
master:
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_TAG
docker-envs.yml
DOCKER_USER: user
DOCKER_PWD: password
DOCKER_REGISTRY: docker.io
DOCKER_GROUP: group_name
DOCKER_REPO_NAME: repo_name