Configuring gitlab.com CI and VPS server

image
In this article, I decided to describe my experience with configuring gitlab CI and rented VPS.

Background


At work, uv. DevOps'y configured to me a deploy of projects in kubernetes ( I work as the Go-developer ). I have my own pet project - and I wanted to automate, but there was no particular desire to raise my gitlab (VPS is not rubber anyway).

Initial data



Create Repository


We create the repository that we want to automate.

I decided for tests to raise docker with nginx and forwarding to the html page.

Repository structure:

  • Dockerfile
    FROM nginx:latest
    COPY html /var/www/html
    COPY nginx.conf /etc/nginx/nginx.conf


  • nginx.conf
    events {}
    http {
    server {
        listen 80;
        location / {
                root /var/www/html;
        }
    }
    }


  • html
    • index.html
      <html>
      <h1>Hello, Runner!</h1>
      </html>


  • .gitlab-ci.yml
    
    image: docker:19.03.8
    
    before_script:
      - docker info
    
    build:
      stage: build
      script:
        - docker build -t hellorunner .
    
    deploy:
      stage: deploy
      script:
        - docker ps --filter name=hellorunner --quiet |  xargs --no-run-if-empty docker stop | xargs --no-run-if-empty  docker rm
        - docker run -d --restart=always --name hellorunner -p 8090:80 hellorunner
              
    after_script:
        - docker system prune -f
    


Repository setup


Open settings -> CI

image

Then, click Runners, expand.
image

First, turn off the proposed runners - Disable shared runners

image

Then, we are interested in - “Set up a specific Runner manually”

image

We copy the token, in the future we will need it.

VPS Preparation


Install docker .

Install gitlab-runner .

Register a new runner.

! In the executor field, specify the docker version, as in the Dockerfile!
In the token field, specify the token that was remembered from gitlab


gitlab-runner register

image

Now, you need to make small changes to the runner’s config.

nano /etc/gitlab-runner/config.toml

field

volumes = ["/cache"]

change to

volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

image

Restarting the daemon

gitlab-runner restart

Back to gitlab / settings / CI / Runners again.

An active runner should appear.

image

Edit the runner, by pressing.

image

Allow to perform tasks without tags.

image

Now execute push commits and follow the jobs.

image

And finally, open the browser.

image

Link to the

PS repository : I met a problem - I had an inside image that didn’t have access to the external network, the solution is create the file /etc/docker/daemon.json :

{
    "dns": ["8.8.4.4", "8.8.8.8"]
}

 service docker restart

All Articles