Prepare the environment

Docker and Docker-compose environments are required

Docker installation and docker-compose reference docker installation use and program deployment

Install GitLab CE and GitLab Runner

Create the gitlab folder and create the docker-comemage.yml file once inside

version: '3'
services:
  gitlab:
    image: 'gitlab/gitlab-ce'
    restart: always
    container_name: gitlab
    hostname: '192.168.2.132:8001'
    environment:
      TZ: 'Asia/Shanghai'
      GITLAB_OMNIBUS_CONFIG: | external_url 'http://192.168.2.132:8001' gitlab_rails the [' gitlab_shell_ssh_port] = 2222 # unicorn [' port '] = 8888 nginx['listen_port'] = 80    ports:
      - '8001:80'
      - '8443:443'
      - '2222:22'
    volumes:
      - /etc/localtime:/etc/localtime
      - ./config:/etc/gitlab
      - ./data:/var/opt/gitlab
      - ./logs:/var/log/gitlab
  gitlab-runner:
    image: gitlab/gitlab-runner
    restart: always
    hostname: gitlab-runner
    container_name: gitlab-runner
    extra_hosts:
      - Git. Imlcs. Top: 192.168.2.132
    depends_on:
      - gitlab
    volumes:
      - /etc/localtime:/etc/localtime
      - ./runner:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock
Copy the code

Docker-compose up -d to run the container

The deployment takes a certain period of time

Configuration GitLab

Go to http://192.168.2.132:8001 and set up a password

Log in as user root and the password you set earlier

Set up Chinese

  1. Go to Profile picture –> Settings

  1. Click on the left Preferences

3. What Language do you prefer

Refresh the page and it becomes Chinese

Get GitLab CI address and token parameters

Upload the project to GitLab

Find Runner in Project Settings ->CI/CD and expand

Getting the URL and registering the token will be used next to configure GitLab Runner

Configuration GitLab Runner

Methods a

docker exec- it gitlab - runner gitlab - runner register - n \ \ - url http://192.168.2.132:8001/ - registration - token WE6zAWHJniXzWDxq2qPU \ --description"dockersock" \
  --docker-privileged=true \
  --docker-pull-policy="if-not-present" \
  --docker-image "docker:latest" \
  --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
  --docker-volumes /root/m2:/root/.m2 \
  --executor docker
Copy the code

If the following prompt appears

ERROR: Registering runner... failed                 runner=WE6zAWHJ status=couldn't execute POST against http://192.168.2.132:8001/api/v4/runners: Post http://192.168.2.132:8001/api/v4/runners: dial tcp 192.168.2.132:8001: connect: no route to host
PANIC: Failed to register this runner. Perhaps you are having network problems
Copy the code

If the firewall is disabled, restart the Docker service

Add the label information as shown in method 2

Method 2

This method makes it easier to understand each step

[root@localhost gitlab]# docker exec -it gitlab-runner gitlab-runner registerRuntime Platform Arch = AMD64 OS = Linux PID =17 Revision = A998CACd Version =13.2.2 Runningin system-mode.
Enter GitLab addressBoth Please enter the gitlab - ci coordinator URL (e.g., https://gitlab.com/) : http://192.168.2.132:8001/Enter the GitLab Token
Please enter the gitlab-ci token for this runner:
WE6zAWHJniXzWDxq2qPU
# Enter Runner's description (can be null, can be modified later)
Please enter the gitlab-ci description for this runner:
[gitlab-runner]:
# Set Tag can be used to specify that ci is triggered when a specified Tag is built (this can be modified later)
Please enter the gitlab-ci tags for this runner (comma separated):
docker,maven
# Select runner, here we select docker
Please enter the executor: shell, docker+machine, docker-ssh+machine, kubernetes, custom, docker-ssh, parallels, ssh, virtualbox, docker:
docker
Please enter the default Docker image (e.g. ruby:2.6):
docker:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Copy the code

When the above steps are completed, refresh the current page, you can see that there is an extra runner under the current page:

Click the Edit button to the right of the lock

Go to the Runner directory and you’ll see a config.tooml file. Edit it

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "ci/cd-test"
  url = "http://192.168.2.132:8001/"
  token = "Ygz9Wn82NjxzYTnEDoty"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock"."/cache"."/root/m2:/root/.m2"]
    shm_size = 0
    pull_policy = "if-not-present"
Copy the code

Each runner that is configured generates a [[runners]] in the configuration file

  • Modified Privileged = true

    The default value is false. Change it to true. If false, the health check will be performed during CI construction, which is time-consuming and still fails. If set to true, the health check will be skipped automatically.

  • Modify volumes = [“/var/run/docker. The sock: / var/run/docker. The sock “, “/ cache”, “/ root/m2: / root/m2”]

    If you have a Maven environment locally, you can hang it locally, so that you can use the local environment directly when handling dependencies, and you can use the Ali Cloud mirror source.

  • Add pull_policy = “if-not-present”

    Dependent pull policy: if there is a local pull, do not check, if not pull

Containerization with Gitlab CI Auto DevOps configuration

Write a Dockerfile file

Create a Dockerfile file in the project root directory

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY  /target/*.jar app.jar
ENV PORT 5000
EXPOSE $PORT
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-Dserver.port=${PORT}"."-jar"."/app.jar"]
Copy the code

Configure the.gitlab-ci.yml file

Add the configuration file for Gitlab CI core — ==.gitlab-ci.yml== and place it in the root directory of the project. When the Gitlab project is created, the Auto DevOps pipeline will be enabled by default. When a code is pushed to the repository, it will automatically scan the root directory for.gitlab-ci.yml. If so, Applications are built, tested, and deployed automatically based on predefined continuous integration and continuous delivery configurations. For detailed syntax, please refer to the official documentation:.gitlab.yML Reference

You can also edit settings. XML file in /root/.m2 to add ali Cloud image information
before_script:
  - rm -rf /root/.m2/settings.xml
  - echo -e "
      1.0\ "" encoding=\""UTF-8\""? > XMLNS = \ "" http://maven.apache.org/SETTINGS/1.0.0\" " xmlns:xsi=\""http://www.w3.org/2001/XMLSchema-instance\"" Xsi: schemaLocation = \ "http://maven.apache.org/SETTINGS/1.0.0" "Http://maven.apache.org/xsd/settings-1.0.0.xsd\" > < mirrors > < mirror > < id > alimaven < / id > < name > aliyun maven
      
       http://maven.aliyun.com/nexus/content/groups/public/
      
      
       central
      " > /root/.m2/settings.xml

image: docker:latest
variables: Define the environment variables needed to execute the job
  DOCKER_DRIVER: overlay2
  # docker host: TCP ://192.168.2.132:2375 # docker host
  TAG: Root/gitlab - ci: v1.0  # mirror name
  CONTAINER_NAME: gitlab-ci-cd
  PORT: 8765
cache: Maven-loaded dependencies can be cached so you don't need to download them again.
  paths:
    - .m2/repository
services: Configure additional services needed. Docker: Dind, something used to run a Docker inside a Docker, needed in the build of a project.
  - docker:dind
stages: "Stages" is a concept in Gitlab CI. Stages stands for construction phase. Stages define what Stages a pipeline has, and Stages are performed in the order defined
  - package
  - deploy
maven-package: # one of the Jobs defined for building jar packages. Internally, maven images are introduced to handle the package process.
  image: Maven: 3.6.3 - JDK - 8
  tags: # Specify which Gitlab runner can pickup the job to execute. Each Gitlab runner has a tag
    - maven
  stage: package
  script: Define the command to be executed by the job
    - mvn clean package -Dmaven.test.skip=true
  artifacts: # define which files will be used by the next job
    paths:
      - target/*.jar
build-master: # one of the Jobs defined for building Docker images. Responsible for executing the deploy process. Execute build and run.
  tags:
    - docker
  stage: deploy
  script:
    - docker build -t $TAG . # Component mirroring
    - docker rm -f $CONTAINER_NAME || true # delete container
    - docker run -d --name $CONTAINER_NAME -p $PORT:$PORT $TAG Run the container
  only: Specify which branch push commit triggers the job to execute. This example specifies that only the master executes the deploy job
    - master
Copy the code

Git commit&Push starts the automatic build after the configuration is complete. Go back to the Gitlab page and you can see the build process.