0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu環境でGitLab Runnerの設定メモ

Last updated at Posted at 2025-01-29

概要

GitLab runnerを試してみたくなったのでUbuntu環境で設定を試してみたので、ほぼ自分用のメモとして記事残しておきます。

ざっくりとやることリスト

  1. Dockerインストール
  2. GitLab Runnerインストール
  3. GitLab Runner登録
  4. お試しプロジェクト作成して実行

Dockerインストール

# パッケージインストール
sudo apt update
sudo apt install ca-certificates curl gnupg

# aptリポジトリを追加
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

# latest docker install
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# インストール確認
docker --version
docker compose version

# 起動
sudo systemctl start docker

# 自動起動有効化
sudo systemctl enable docker

参考

GitLab Runnerインストール

# ダウンロード
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# 権限設定
sudo chmod +x /usr/local/bin/gitlab-runner

# ユーザー追加
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# インストール
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

# 起動
sudo systemctl start gitlab-runner
sudo systemctl enable gitlab-runner
sudo systemctl status gitlab-runner

/home/gitlab-runner/.bash_logoutを確認して、以下がある場合はコメントアウトしておきます。
※コメントアウトしないと、シェルがプロファイルをロードしていることが原因でJob failed (system failure): preparing environmentというエラーが出ます。

/home/gitlab-runner/.bash_logout
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

↑があったら、↓コメントアウトしておく

/home/gitlab-runner/.bash_logout
#if [ "$SHLVL" = 1 ]; then
#    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
#fi

上記をコメントしたら再起動をしておきます。

sudo systemctl start gitlab-runner

参考:
https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading
https://docs.gitlab.com/runner/faq/index.html#job-failed-system-failure-preparing-environment

GitLab Runner登録

「管理者エリア」→「CI/CD」→「Runner」で管理画面を表示して、「新規インスタンスRunner」ボタンをクリック

image.png

とりあえず、タグの設定は行わずにRunnerを作成

image.png

ステップ1に表示されるコマンドの先頭にsudoを付けてコマンドラインで実行

image.png

# ★★★かならずsudoを付けること★★★
sudo gitlab-runner register  --url https://xxxxxxxxxxxxx  --token glrt-xxxxxxxxxxxxxxxxx

ちなみに、sudoを付け忘れると設定ファイルのconfig.tomlが、ログインユーザーのホームディレクトリ配下に/home/xxxxx/.gitlab-runner/config.tomlが作成されて認識してもらえなくてハマりました。

正しく設定できれていれば/etc/gitlab-runner/config.tomlに設定が書きこまれています。

# 設定確認
sudo cat /etc/gitlab-runner/config.toml

あとは、先ほどの管理者の画面でRunnerの状態が「オンライン」であることを確認すればOKです。

お試しプロジェクトでRunner実行

新規にプロジェクトを作成後に、ブラウザで確認するとCI/CDを設定というリンク(昔はボタンみたいなの有ったような記憶g・・・)があるので、そのリンクをクリックするとプロジェクトの直下に.gitlab-ci.ymlのファイルを配置されます。

image.png

もし、CI/CDを設定というリンク(またはボタン)が見るけられなかったら以下のように作成してプッシュしてみてもOKです。

.gitlab-ci.yml
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.
  environment: production
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

その後、プロジェクトのメニュー「ビルド」→「パイプライン」でジョブの実行結果が確認できます。

image.png

最後に

とりあえず、shellで動かすタイプにしたけど、dockerも試してみようかと思います。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?