概要
GitLab runnerを試してみたくなったのでUbuntu環境で設定を試してみたので、ほぼ自分用のメモとして記事残しておきます。
ざっくりとやることリスト
- Dockerインストール
- GitLab Runnerインストール
- GitLab Runner登録
- お試しプロジェクト作成して実行
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
というエラーが出ます。
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
↑があったら、↓コメントアウトしておく
#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」ボタンをクリック
とりあえず、タグの設定は行わずにRunnerを作成
ステップ1に表示されるコマンドの先頭にsudo
を付けてコマンドラインで実行
# ★★★かならず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
のファイルを配置されます。
もし、CI/CDを設定
というリンク(またはボタン)が見るけられなかったら以下のように作成してプッシュしてみてもOKです。
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."
その後、プロジェクトのメニュー「ビルド」→「パイプライン」でジョブの実行結果が確認できます。
最後に
とりあえず、shellで動かすタイプにしたけど、dockerも試してみようかと思います。