3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitlab CI/CD あれこれ

Last updated at Posted at 2023-03-02

やりたいこと

弊社内の活動の中で、Gitlab で、Azure Function Apps へのコードデプロイを自動化したので、その内容をまとめます。

前提

  • 弊社の Gitlab サーバはオンプレです。
  • そのため、ジョブを実行するためのコンピューティングリソースが必要です。
    • 今回は個人のローカルマシンでやってみました。
  • 作業はすべて WSL 上で行っています。
    • WSL 上で Docker を使います。

大まかな手順

Gitlab CI/CD

Gitlab での CI/CD についてはここを参照

Gitlab-Runner というアプリケーションを利用して、ローカルのマシンでジョブを動かせるようにする。

事前準備

WSL

以下を参考に環境をつくる。

Docker

以下を参考に WSL 上にインストールする。

ローカルのマシンでジョブを動かせるようにする

まずは、gitlab-runner のイメージからコンテナを起動する。以下のような感じ。

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

以下コマンドで、起動したコンテナに入る (表現正しい?)

docker exec -it gitlab-runner /bin/bash

(コンテナ内で)
gitlab-runner register

インタラクティブにいろいろ入力を求められるので、入力する。
参考: https://e-penguiner.com/build-gitlab-runner-with-docker/#register-runner-for-saas

  • Enter the GitLab instance URL (for example, https://gitlab.com/):
    • Gitlab の URL
  • Enter the registration token:
    • Runner を紐づけたいリポジトリのトークン
  • Enter a description for the runner:
    • 任意。設定しなくても可
  • Enter tags for the runner (comma-separated):
    • 任意。設定しなくても可
  • Enter optional maintenance note for the runner:
    • 任意。設定しなくても可
  • Enter an executor:
    • docker
      • 基本的に docker で OK
  • Enter the default Docker image (for example, ruby:2.7):
    • 何らかイメージを指定する
    • Python:3.9 を使った

登録を解除する場合は、gitlab-runner unregister --url {GITLAB_URL} --token {TOKEN} で。
gitlab-runner list で登録内容を確認できる。

ここまでやると、Gitlab のプロジェクトページから、Settings → CI/CD のページを開き Specific Runners の下の方にさっき登録した Runner が表示されているはず。ステータスが緑になっていれば使用可能な状態。

デプロイのパイプラインを作成する

ひとまず簡単なコマンドを実行させてみる。リポジトリのルートに、.gitlab-ci.yml を以下の内容で作成する。

.gitlab-ci.yml
job:
  script: echo "HelloWorld"

このファイルをリポジトリへ Push するとパイプラインが動き出す。HelloWorld と表示されるはず。

あとは、一般的なデプロイの手順を YAML ファイル内で定義していく。
YAML ファイルの書き方はここ。

すべて CLI で行う必要があるので、Auzre Functions Core Tools を使う。

基本的に上記の内容に沿って定義していけば大丈夫なはず。

ということで

Gitlab での CI/CD の構築方法について大まかにまとめた。
Azure を提供している Microsoft は Github を買収したこともあり、そちらを今後は (というかすでに) 推していくと思うので、どこかのタイミングで Github Actions に移行したい。また、Azure との親和性という意味でも、Github Actions には簡易にデプロイできるアクションが用意されているが、Gitlab でのデプロイは1から構築することになり大変なので、そういう意味でも移行したい。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?