Skip to main content
In this guide, we’ll extend our GitLab CI/CD pipeline by adding a docker_push job that uploads your built image to Docker Hub. By the end, you’ll have a fully automated workflow—from unit tests to containerization and Docker pushes.

1. Existing Pipeline Overview

Here’s the current .gitlab-ci.yml with four jobs: unit_testing, code_coverage, docker_build, and docker_test. We run these on the main branch or any feature/* branch, including merge requests.

2. Adding the docker_push Job

We’ll append a fifth stage, docker_push, under containerization. This job will:
  • Depend on docker_build and docker_test via needs
  • Leverage Docker-in-Docker (dind) for pushing images
  • Load the previously saved artifact
  • Authenticate to Docker Hub using CI/CD variables
  • Push the image tagged with $IMAGE_VERSION
Ensure your DOCKER_PASSWORD is masked and protected in GitLab. Never hard-code credentials in your .gitlab-ci.yml.

3. Defining CI/CD Variables

Navigate to Settings → CI/CD → Variables in your GitLab project and add:
The image shows a GitLab CI/CD settings page displaying variables, including masked keys like "DOCKER_PASSWORD" and "M_DB_PASSWORD." The sidebar contains navigation options for various settings and features.
You can also set pipeline-level variables via the GitLab API or include them in a protected group for reuse across multiple projects.

4. Pipeline Visualization

After pushing the updated .gitlab-ci.yml, GitLab will display five jobs in sequence. The docker_push job runs last, once image build and tests complete.
The image shows a GitLab CI/CD pipeline interface for a NodeJS project, displaying the status of various jobs like code coverage, unit testing, and Docker processes.

5. Sample docker_push Logs

Below is an example of a successful Docker push in your CI logs:
Once the job completes successfully, visit your Docker Hub repository (siddharth67/solar-system) to confirm the newly pushed tags.

Further Reading

Watch Video