Build an automated test environment for gitlab docker

create runner

Now set up CI on gitlab, http://192.168.xxx.xxx/path/to/src/settings/ci_cdand get the registration token.

Then create the runner:

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

After entering the runner container, run the setup command and follow the wizard to set up the runner

gitlab-runner register

Modify the runner configuration vi /etc/gitlab-runner/config.toml Add later configuration under [runners.docker]

	pull_policy = "if-not-present"

Create a test image

When gitlab executes tests through docker, it needs to generate a container from the image. This image needs to meet the running environment of the test program. We first build this running environment.

Create a container based on the ubuntu base image

docker run -dit ubuntu bash

After entering the container, install the basic compilation environment to ensure that you can pull the code from gitlab, compile the code, and successfully test the program.

apt install build-essential git cmake
git clone xxxxxxxxx surcode
cd surcode
mkdir -p build
cd build
cmake ..
make
make test

When the above command is executed successfully, exit the container and submit the container as an image

docker commit c51ea85567e1 build-image

Create a new CI configuration file

In the project root directory, create a new configuration file vi.gitlab-ci.yml

image: build-image
 
test:
  script:
    - mkdir -p build
    - cd build
    - cmake ..
    - make
    - make test

The format requirements of the yaml file are as follows:

  • Indentation requirement is 2 spaces
  • -, :a space is required after

Add this file to the repository and submit it to the server

git add .gitlab-ci.yml 
git commit -m "添加CI文件"
git push

View CI results

Go back to the gitlab page and view the CI execution results

http://192.168.xxx.xxx/path/to/src/pipelines

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324982660&siteId=291194637