Solution 1: Go Packages and Functions
Let’s solve the challenge set in the previous lesson.
We'll cover the following...
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, andrepo_url.
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
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:
image: docker:stableservices:- name: dockeralias: dockerstages:- download- executecompile:stage: downloadscript:- 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: executescript:- 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:stableline specifies that the GitLab CI/CD runner should use thedocker:stableDocker 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 servicessection defines additional services that should be linked to the CI/CD runner. In this case, it includes the ...