LoginSignup
0
5

More than 1 year has passed since last update.

GitLabとGitLab runnerをDockerでセットアップ

Last updated at Posted at 2022-02-11

CIしたいときに普段は手軽なのでGitHub Actionsを使ってますが、公開リポジトリor有料プランじゃないと計算資源が限定的なので、ローカルでGitLab&GitLab runnerを構築してCIしてみます。
あとは会社で仲間内でGitとCI使いたいときも使ってます。
基本的に公式ドキュメント見ながら作っただけです。

ホストに細かくインストールするのは苦手なので、docker-compose使って一気に作ります。

docker-compose

version: '3.6'
services:
  lab:
    image: 'gitlab/gitlab-ee:latest'
    restart: always
    hostname: 'gitlab.my.com'
    container_name: gitlab
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.my.com'
        # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - '8080:80'
      # - '443:443'
      # - '22:22'
    volumes:
      - 'gitlab-config:/etc/gitlab'
      - 'gitlab-logs:/var/log/gitlab'
      - 'gitlab-data:/var/opt/gitlab'
    shm_size: '256m'
    networks:
      - gitlab-network
  runner:
    image: gitlab/gitlab-runner:latest
    restart: always
    container_name: gitlab-runner
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - gitlab-runner-config:/etc/gitlab-runner
    networks:
      - gitlab-network

volumes:
  gitlab-config:
  gitlab-logs:
  gitlab-data:
  gitlab-runner-config:

networks:
  gitlab-network:
    name: gitlab-network

docker volumeは適宜バックアップをお願いします。
仲間内で使うだけなので、SSLなしです。

runner->GitLabが疎通できるようにしたいので、Docker networkを定義しています。

起動

普通に

docker-compose up -d

で起動します。
結構な時間がかかる(5分くらい)ので、お茶でも飲んで待ちます。
http://localhost:8080 にアクセスするとGitLabが起動しています。

image.png

アクセスできたら一旦ログインします。
Usernameはroot
パスワードは乱数で生成されるので、コンテナに入って確認します。

$ docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
Password: g21Y39lUkgwTelSv/vzm/VkBmzCgYrTwtqT3f+8cwns=

入手したPasswordでGitLabにログインします。

ユーザーをいい感じに作ったりリポジトリを作ったら、CIを設定してリポジトリとrunnerを接続します。

CIの設定

image.png

image.png

CIの設定からRunnersに入ります。
まず、GitLabの公式リソースは使わずに100%ローカルパワーで実行するので、「Shared runners」はFalseにします。

次に、この画面はそのままにしておいて、runner側の設定をします。

docker exec gitlab-runner gitlab-runner register -n \
  --url http://gitlab.my.com/ \
  --registration-token C1xiCyrMox_f_q9EHE21 \
  --name myfirstci \
  --executor docker \
  --docker-image ubuntu:20.04 \
  --docker-network-mode gitlab-network
  • registration-token: gitlabのregistration tokenをコピペします
  • name: 好きにつけてください
  • docker-image: 使うdocker imageです
  • docker-network-mode: gitlabとrunnerが動いているネットワーク名です。CIのために生成したコンテナをこのネットワークに所属させないとGit Cloneが失敗します。わからなければdocker network lsで調べてください。

CIを作成

image.png

image.png

とりあえずサンプルから作ってみます。

# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."

unit-test-job:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - echo "Running unit tests... This will take about 60 seconds."
    - sleep 60
    - echo "Code coverage is 90%"

lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 10
    - echo "No lint issues found."

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

サンプルで生成されたCIです。
時間稼ぎしているだけで特に何もしてないので、runnerとの接続に問題がなければ成功するはず。

image.png

コミットしてみます。

Pipelinesを見てみると、走り始めてます。
image.png

全部正常終了しました。
image.png

これでGitLabとCIのセットアップをDockerを使ってサッと完了できました!

0
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
5