Exercise 1: Go Packages and Functions
Let’s test our understanding by attempting this challenge.
We'll cover the following
Let’s practice what we’ve learned so far in the following exercise. The solution to this exercise has been provided in the next lesson.
Problem
Use GitLab CI/CD to push Docker images to Docker Hub.
General instructions
Here are the general steps to use GitLab CI/CD to push Docker images to Docker Hub:
Set up a GitLab repository: Create a new repository or use an existing one on GitLab.
Create a Dockerfile: Create a Dockerfile in the root of your repository. The Dockerfile contains instructions for building your Docker image.
Configure GitLab CI/CD pipeline: Create a
.gitlab-ci.yml
file in the root of your repository. This file defines the CI/CD pipeline for your project, including the stages and jobs to be executed. You must use thedocker:stable
base image in the.gitlab-ci.yml
file so that Docker can run on a machine that is allocated for Gitlab CI/CD. You will also need to add theservices
section in the.gitlab-ci.yml
file so that the machine can know that what is the name of the Docker service that is running.Define a build job: Set up a job in the CI/CD pipeline to build the Docker image using the Dockerfile. This job should use the appropriate Docker commands, such as
docker build
anddocker push
, to build and push the image.Configure Docker Hub credentials: Set up the necessary credentials for Docker Hub in your GitLab CI/CD settings. This typically includes the Docker Hub username and password or access token.
Use variables: Utilize GitLab CI/CD variables to store sensitive information like Docker Hub credentials securely. These variables can be referenced in the CI/CD pipeline configuration. Mark variables as protected and do not forget to make the branch of your project protected as well. To mark variables as protected in GitLab during their creation, you can follow these steps:
Go to your project’s GitLab page.
Navigate to “Settings,” and then select CI/CD from the sidebar.
Scroll down to the “Variables” section.
Click the “Add variable” button.
In the “Key” field, enter the name of the variable.
In the “Value” field, enter the value of the variable.
Check the “Protected” checkbox.
Optionally, you can add a description for the variable in the “Variable description” field.
Click the “Add variable” button to create the protected variable.
Set up image tagging: Decide on a tagging strategy for your Docker images. You can use variables or GitLab CI/CD environment variables to dynamically set the image tags based on the branch, tag, or other criteria.
Trigger pipeline on code changes: Configure your GitLab repository to automatically trigger the CI/CD pipeline whenever changes are pushed to the repository. This ensures that the Docker images are built and pushed whenever there are updates.
Test and verify: Run the CI/CD pipeline and verify that the Docker images are successfully built and pushed to Docker Hub. Check the pipeline logs and Docker Hub to ensure the images are available.
Monitor and iterate: Monitor the CI/CD pipeline and make any necessary adjustments or improvements based on feedback and requirements. This may include adding additional stages or jobs, optimizing the Dockerfile, or enhancing the overall pipeline configuration.
These steps provide a general overview of the process of using GitLab CI/CD to push Docker images to Docker Hub. Remember to tailor the configuration to your specific project requirements and follow best practices for security and efficiency. Follow the above steps and use GitLab CI/CD to push Docker images to Docker Hub in the playground below.
Files
To get started, the Dockerfile
and run_go_file.sh
files are already provided.
Dockerfile
Let’s now look at the Dockerfile
:
Get hands-on with 1400+ tech skills courses.