...

/

Solution 1: Go Packages and Functions

Solution 1: Go Packages and Functions

Let’s solve the challenge set in the previous lesson.

Solution

Here is the implementation of how to use GitLab CI/CD to push Docker images to Docker Hub. The script.sh file provided in the playground below contains all the necessary commands to successfully push Docker images to Docker Hub using GitLab CI/CD.

Note: Before moving forward with code execution, kindly provide values for the following variables: user_email, user_name, and repo_url.

Please provide values for the following:
user_email
Not Specified...
user_name
Not Specified...
repo_url
Not Specified...
image: docker:stable

services:
    - name: docker
      alias: docker

stages:
  - build
  - run

compile:
  stage: build
  script:
    - docker build -t $USERNAME/gitlab_task_img .
    - echo $PASSWORD | docker login -u $USERNAME --password-stdin
    - docker push $USERNAME/gitlab_task_img:latest
    - echo "Image has been pushed successfully :D :D"
  artifacts:
    paths:
      - bin/

execute:
  stage: run
  script:
    - docker pull $USERNAME/gitlab_task_img:latest
    - docker run $USERNAME/gitlab_task_img:latest /bin/sh run_go_file.sh
main.go

Code explanation

The .gitlab-ci.yml file

We’ll now look at the .gitlab-ci.yml file and the main.go file provided in the solution.

Let’s first look at the .gitlab-ci.yml file to get us started:

Press + to interact
image: docker:stable
services:
- name: docker
alias: docker
stages:
- download
- execute
compile:
stage: download
script:
- docker build -t $USERNAME/gitlab_task_img .
- echo $PASSWORD | docker login -u $USERNAME --password-stdin
- docker push $USERNAME/gitlab_task_img:latest
- echo "Image has been pushed successfully :D :D"
artifacts:
paths:
- bin/
execute:
stage: execute
script:
- docker pull $USERNAME/gitlab_task_img:latest
- docker run $USERNAME/gitlab_task_img:latest /bin/sh run_go_file.sh

Let’s break down the GitLab CI/CD configuration file .gitlab-ci.yml:

  • The image: docker:stable line specifies that the GitLab CI/CD runner should use the docker:stable Docker image as its base image. This image includes the Docker client, allowing us to build and interact with Docker images and containers within the CI/CD pipeline.

  • The services section defines additional services that should be linked to the CI/CD runner. In this case, it includes the ...