Updated on 2025-01-08 GMT+08:00

Setting Up the Jenkins and GitLab Environments

Prerequisites

  • A new VPC has been created. In this example, the VPC is named vpc-X and the CIDR block is 192.168.0.0/16.
  • An ECS in vpc-X (with CIDR block of 192.168.0.0/16) has been created. The recommended flavors for this ECS are 4 vCPUs and 16 GiB, and it runs Huawei Cloud EulerOS 2.0. An EIP has been bound to the ECS to pull images from the Internet.

Installing Docker of a Specified Version

  1. Log in to the ECS.
  2. Quickly install Docker of the latest version on the device running Huawei Cloud EulerOS 2.0. You can also manually install Docker. For details, see Docker Engine installation.

    dnf install docker

  3. Check whether Docker is installed.

    docker info

Installing and Configuring GitLab

  1. Pull the GitLab image.

    docker pull gitlab/gitlab-ce

  2. Run the container.

    docker run -d -p 443:443 -p 80:80 -p 222:22 --name gitlab --restart always -v /home/gitlab/config:/etc/gitlab -v /home/gitlab/logs:/var/log/gitlab -v /home/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce

  3. Check the container status.

    docker ps | grep gitlab

  4. Configure gitlab.rb (with the node path of /home/gitlab/config/gitlab.rb). GitLab generates a project URL based on the container's host name (that is, the container ID) when a project is created. However, a fixed URL is required for the GitLab server. To achieve this, you must configure gitlab.rb.

    1. Run the vi command on the node to open /home/gitlab/config/gitlab.rb.
      vi /home/gitlab/config/gitlab.rb
      Add the following content to it:
      external_url 'http://**.**.**.**' //External IP address of the node, for example, EIP
      gitlab_rails['gitlab_ssh_host'] = '**.**.**.**' //External IP address of the node, for example, EIP
      gitlab_rails['gitlab_shell_ssh_port'] = 222 //This port is mapped when the container is started. (222->22)
    2. Save the changes.

  5. Restart the container.

    docker restart gitlab

  6. Use a browser to access ECS EIP:80 and log in to the GitLab service.

    The default username is root, and the initial login password is stored in the /etc/gitlab/initial_root_password directory of the GitLab container.

    You can run the following command to obtain the password:
    docker exec gitlab cat /etc/gitlab/initial_root_password

Installing and Configuring Jenkins

  1. Pull the Jenkins image.

    docker pull jenkins/jenkins:lts

  2. Run the Jenkins container.

    To execute commands like docker build within the container, the container must have the capability of docker in docker (dind). This requires mounting docker.sock and docker to the container.

    docker run -d -p 8000:8080 -p 50000:50000 \
     -v /var/jenkins_home:/var/jenkins_home \
     -v /var/run/docker.sock:/var/run/docker.sock \
     -v /usr/bin/docker:/usr/bin/docker \
     --name myjenkins --privileged=true -u root jenkins/jenkins:lts

  3. Test whether the docker command can be executed in the Jenkins container.

    docker exec myjenkins docker ps

    If the error message "docker: error while loading shared libraries: libltdl.so.7: cannot open shared object file: No such file or directory" is displayed, run the following command to address this issue:

    docker exec myjenkins sh -c "apt-get update && apt-get install -y libltdl7"

  4. Use a browser to access ECS EIP:8000 and log in to the Jenkins service.

    The default username is root, and the initial login password is stored in the /var/jenkins_home/secrets/initialAdminPassword directory of the container.

    You can run the following command to obtain the password:

    docker exec myjenkins cat /var/jenkins_home/secrets/initialAdminPassword

  5. Install the recommended plugins.

    After the initial configuration is complete, the Jenkins page is displayed.

  6. Install the kubectl command line tool.

    1. Download kubectl to the local PC.

      It is recommended that kubectl be of the same version as the cluster to be used. For details, see kubectl.

      wget https://dl.k8s.io/release/v*.*.*/bin/linux/amd64/kubectl  // v*.*.* specifies the cluster version.
    2. Copy kubectl to the Jenkins container.
      docker cp kubectl myjenkins:/usr/bin/
      docker exec myjenkins chmod +x /usr/bin/kubectl

  7. Configure Jenkins to obtain GitLab code without a password.

    1. Obtain the SSH public key.
      docker exec -it myjenkins ssh-keygen -t rsa

      Press Enter to skip configuring other parameters.

      You do not have to adjust the settings for parameters that are already set to their default values.

      Check the generated public key.

      docker exec myjenkins cat /root/.ssh/id_rsa.pub
    2. Search for SSH in GitLab and add a new key.

    3. Enter an SSH public key.

    4. Save the SSH key.