LoginSignup
14
10

More than 5 years have passed since last update.

GiLab CI | GiLab CI で branch ごとに build 内容を切り替える

Posted at

GiLab CI | GiLab CI で branch ごとに build 内容を切り替える

概要

GiLab CI で branch ごとに build 内容を切り替える方法について。

GitLab CI は push に対して常に build script を実行します。
branch ごとに build 内容を切り替えたい場合は、 build script 内で
$CI_BUILD_REF_NAME 環境変数を利用してブランチ名を参照し、処理を分岐をさせます。

サンプル

build-script
if [ $CI_BUILD_REF_NAME = "master" ] ; then
  echo "build master"
  git submodule update --init
  ls -la
  bundle install
  rspec
  exit 0
elif [[ "$CI_BUILD_REF_NAME" =~ ^tp_.*$ ]] ; then
  echo "build topic branch"
  git submodule update --init
  ls -la
  bundle install
  rspec
  exit 0
else
  echo "not target branch"
  exit 0
fi

デプロイ試行

master ブランチを push

GitLab CI のデプロイ ログを確認

# :
# 略
# :
echo "build master"
build master
# :
# 略
# :
8 examples, 0 failures
exit 0

tp_some_topic ブランチを push

GitLab CI のデプロイ ログを確認

# :
# 略
# :
echo "build topic branch"
build topic branch
# :
# 略
# :
8 examples, 0 failures
exit 0

other ブランチを push

GitLab CI のデプロイ ログを確認

# :
# 略
# :
echo "not target branch"
not target branch
exit 0
14
10
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
14
10