A
A
Anatoly Soldatov2020-03-04 15:01:22
Continuous Integration
Anatoly Soldatov, 2020-03-04 15:01:22

Explain on your fingers how to prepare CI using GitLab, GitLab runner, docker, php?

I study continuous integration. I'm trying to figure out the basic processes and I can't get a picture in my head.

Given:
1. The server where GitLab is installed, there is a PHP project. The index.php file with the content <?php phpinfo();.

2. Virtual machine with CentOS 7.
2.1. Installed docer on it.

bash

docker -v
Docker version 19.03.7, build 7141c199a2


2.2 Installed GitLab runner as a separate container according to the instructions .
bash

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


2.3 Registered runner in GitLab.
bash

docker run --rm -t -i -v /docker/gitlab-runner/volumes/config:/etc/gitlab-runner gitlab/gitlab-runner register


I got this config.
config.toml

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800


  name = "xxxRunner"
  url = "http://_xxx_/gitlab/"
  token = "_xxx_"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0


2.4 I start runner. I see it active in GitLab.

3. Created a test .gitlab-ci.yml file in the project:
.gitlab-ci.yml

job1:
  script:
    - php -v
job2:
  script:
    - php -v


After pushing and launching pipelines, the job crashes with an error. Probably because there is no php in the runner container.
job

Running with gitlab-runner 12.8.0 (1b659122)
  on _xxx_Runner 50b311ce
Using Docker executor with image alpine:latest ...
Pulling docker image alpine:latest ...
Using docker image sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a for alpine:latest ...
Running on runner-50b311ce-project-8-concurrent-0 via 2333629aca2f...
Fetching changes...
Reinitialized existing Git repository in /builds/gitlab/_xxx_/_xxx_/.git/
From _xxx_
   2c41219..48aa22e  master     -> origin/master
Checking out 48aa22eb as master...

Skipping Git submodules setup
/bin/sh: eval: line 56: php: not found
$ php -v
ERROR: Job failed: exit code 127



I'm stuck at this stage, I don't understand what's next.

What you need:
A container with apache + php7 (I don’t know which one to choose). Inside the container, a php project.
After the push, the project needs to be downloaded from the git, the image and the container are rebuilt with updates from the master branch.

Questions:
Is it correct to install gitlab runner in a container?
Is the gitlab runner configuration correct?
Do commands from .gitlab-ci.yml run inside runner container or docker host?
If the commands are executed inside a runner container, how do I checkout the project from git and put it on the host?
How can I transfer the project to another container with php, via volumes?

Please tell me a simple recipe on how to set up a deployment on a php project.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
akdes, 2020-03-11
@soldatov

Firstly, I advise you to take the same alpine or any other docker image,
1. run it on your computer
docker run -it *IMAGE* sh
or
docker run -it *IMAGE* bash
and install everything you need to build. php, curl, composer.. depending on the project.
2. Next, based on your actions, create a dockerfile
FROM image
RUN apk update && apk add php7-fpm curl...
3. Use COPY/ADD to copy the project to the container (to simulate the process). And execute the necessary in the container to build the project.
4. Copy the necessary commands to gitlab-ci.yaml
Note that if you want to package your project in docker, for example to run in k8s, you need to use dind (docker in docker) or gitlab runner should work directly, not in docker. Because otherwise you won't be able to run docker build ... in the CI/CD process

If the commands are executed inside a runner container, how do I checkout the project from git and put it on the host?

When you start runner, it "downloads" your project by itself, you can see it here:
Reinitialized existing Git repository in /builds/gitlab/_xxx_/_xxx_/.git/
From _xxx_
   2c41219..48aa22e  master     -> origin/master
Checking out 48aa22eb as master...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question