GitlabでCI debian編


# インストール

以下のコマンドで```deian```へインストールします。
指定してる```shell```の中で```sudo```が使用しており、```sudo```の権限が必要です。

```text:
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5243    0  5243    0     0   7930      0 --:--:-- --:--:-- --:--:--  9873
Detected operating system as debian/7.
Checking for curl...
Detected curl...
Running apt-get update... done.
Installing debian-archive-keyring which is needed for installing 
apt-transport-https on many Debian systems.
Installing apt-transport-https... done.
Installing /etc/apt/sources.list.d/runner_gitlab-ci-multi-runner.list...done.
Importing packagecloud gpg key... done.
Running apt-get update... done.

The repository is setup! You can now install packages.
```

バージョンを指定！？[^1]

```text:
# cat > /etc/apt/preferences.d/pin-gitlab-runner.pref <<EOF
> Explanation: Prefer GitLab provided packages over the Debian native ones
> Package: gitlab-ci-multi-runner
> Pin: origin packages.gitlab.com
> Pin-Priority: 1001
> EOF
```

いよいよ、```gitlab-ci-multi-runner```をインストール

```text:
# apt-get install gitlab-ci-multi-runner
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  adduser git git-man less libbsd0 libclass-isa-perl libedit2 liberror-perl libexpat1 libgdbm3 libpopt0 libswitch-perl
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-blacklist openssh-blacklist-extra
  openssh-client patch perl perl-modules rsync xauth
Suggested packages:
  gettext-base git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn git-email git-gui gitk
  gitweb docker-engine ssh-askpass libpam-ssh keychain monkeysphere ed diffutils-doc perl-doc
  libterm-readline-gnu-perl libterm-readline-perl-perl make libpod-plainer-perl openssh-server
Recommended packages:
  ssh-client
The following NEW packages will be installed:
  adduser git git-man gitlab-ci-multi-runner less libbsd0 libclass-isa-perl libedit2 liberror-perl libexpat1 libgdbm3
  libpopt0 libswitch-perl libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-blacklist
  openssh-blacklist-extra openssh-client patch perl perl-modules rsync xauth
0 upgraded, 28 newly installed, 0 to remove and 0 not upgraded.
Need to get 45.4 MB of archives.
After this operation, 101 MB of additional disk space will be used.
Do you want to continue [Y/n]? ^Ctakara@debian-runner:~$ apt-get install -y gitlab-ci-multi-runner
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
takara@debian-runner:~$ apt-get -y install  gitlab-ci-multi-runner
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
takara@debian-runner:~$ sudo apt-get -y install  gitlab-ci-multi-runner
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  adduser git git-man less libbsd0 libclass-isa-perl libedit2 liberror-perl libexpat1 libgdbm3 libpopt0 libswitch-perl
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-blacklist openssh-blacklist-extra
  openssh-client patch perl perl-modules rsync xauth
Suggested packages:
  gettext-base git-daemon-run git-daemon-sysvinit git-doc git-el git-arch git-cvs git-svn git-email git-gui gitk
  gitweb docker-engine ssh-askpass libpam-ssh keychain monkeysphere ed diffutils-doc perl-doc
  libterm-readline-gnu-perl libterm-readline-perl-perl make libpod-plainer-perl openssh-server
Recommended packages:
  ssh-client
The following NEW packages will be installed:
  adduser git git-man gitlab-ci-multi-runner less libbsd0 libclass-isa-perl libedit2 liberror-perl libexpat1 libgdbm3
  libpopt0 libswitch-perl libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-blacklist
  openssh-blacklist-extra openssh-client patch perl perl-modules rsync xauth
0 upgraded, 28 newly installed, 0 to remove and 0 not upgraded.
Need to get 45.4 MB of archives.
After this operation, 101 MB of additional disk space will be used.
Get:1 http://security.debian.org/ wheezy/updates/main libexpat1 amd64 2.1.0-1+deb7u4 [139 kB]
Get:2 http://security.debian.org/ wheezy/updates/main perl-modules all 5.14.2-21+deb7u4 [3442 kB]
Get:3 http://httpredir.debian.org/debian/ wheezy/main libgdbm3 amd64 1.8.3-11 [46.9 kB]
Get:4 http://httpredir.debian.org/debian/ wheezy/main libpopt0 amd64 1.16-7 [56.4 kB]       
Get:5 http://httpredir.debian.org/debian/ wheezy/main libbsd0 amd64 0.4.2-1 [59.3 kB]        
Get:6 http://httpredir.debian.org/debian/ wheezy/main libedit2 amd64 2.11-20080614-5 [72.9 kB]
Get:7 http://httpredir.debian.org/debian/ wheezy/main libxau6 amd64 1:1.0.7-1 [18.8 kB]       
Get:8 http://security.debian.org/ wheezy/updates/main perl amd64 5.14.2-21+deb7u4 [4422 kB] 
Get:9 http://httpredir.debian.org/debian/ wheezy/main libxdmcp6 amd64 1:1.1.1-1 [26.3 kB]
Get:10 https://packages.gitlab.com/runner/gitlab-ci-multi-runner/debian/ wheezy/main gitlab-ci-multi-runner amd64 1.5.3 [22.4 MB]
Get:11 http://httpredir.debian.org/debian/ wheezy/main libxcb1 amd64 1.8.1-2+deb7u1 [50.2 kB]   
Get:12 http://httpredir.debian.org/debian/ wheezy/main libx11-data all 2:1.5.0-1+deb7u2 [194 kB]                       
Get:13 http://httpredir.debian.org/debian/ wheezy/main libx11-6 amd64 2:1.5.0-1+deb7u2 [901 kB]                        
Get:14 http://security.debian.org/ wheezy/updates/main openssh-client amd64 1:6.0p1-4+deb7u6 [1027 kB]                 
Get:15 http://httpredir.debian.org/debian/ wheezy/main libxext6 amd64 2:1.3.1-2+deb7u1 [54.8 kB]                       
Get:16 http://httpredir.debian.org/debian/ wheezy/main libxmuu1 amd64 2:1.1.1-1 [23.6 kB]                  
Get:17 http://httpredir.debian.org/debian/ wheezy/main openssh-blacklist all 0.4.1+nmu1 [1835 kB]
Get:18 http://httpredir.debian.org/debian/ wheezy/main openssh-blacklist-extra all 0.4.1+nmu1 [1835 kB]             
Get:19 http://httpredir.debian.org/debian/ wheezy/main adduser all 3.113+nmu3 [264 kB]                                 
Get:20 http://httpredir.debian.org/debian/ wheezy/main less amd64 444-4 [135 kB]                        
Get:21 http://httpredir.debian.org/debian/ wheezy/main libclass-isa-perl all 0.36-3 [12.3 kB]         
Get:22 http://httpredir.debian.org/debian/ wheezy/main libswitch-perl all 2.16-2 [21.0 kB]                         
Get:23 http://httpredir.debian.org/debian/ wheezy/main patch amd64 2.6.1-3 [121 kB]                               
Get:24 http://httpredir.debian.org/debian/ wheezy/main liberror-perl all 0.17-1 [23.6 kB]                              
Get:25 http://httpredir.debian.org/debian/ wheezy/main git-man all 1:1.7.10.4-1+wheezy3 [1075 kB]                      
Get:26 http://httpredir.debian.org/debian/ wheezy/main git amd64 1:1.7.10.4-1+wheezy3 [6688 kB]                        
Get:27 http://httpredir.debian.org/debian/ wheezy/main rsync amd64 3.0.9-4 [369 kB]                                    
Get:28 http://httpredir.debian.org/debian/ wheezy/main xauth amd64 1:1.0.7-1 [37.2 kB]                                 
Fetched 45.4 MB in 10s (4441 kB/s)                                                                                     
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libgdbm3:amd64.
(Reading database ... 23925 files and directories currently installed.)
Unpacking libgdbm3:amd64 (from .../libgdbm3_1.8.3-11_amd64.deb) ...
Selecting previously unselected package libpopt0:amd64.
Unpacking libpopt0:amd64 (from .../libpopt0_1.16-7_amd64.deb) ...
Selecting previously unselected package libbsd0:amd64.
Unpacking libbsd0:amd64 (from .../libbsd0_0.4.2-1_amd64.deb) ...
Selecting previously unselected package libedit2:amd64.
Unpacking libedit2:amd64 (from .../libedit2_2.11-20080614-5_amd64.deb) ...
Selecting previously unselected package libexpat1:amd64.
Unpacking libexpat1:amd64 (from .../libexpat1_2.1.0-1+deb7u4_amd64.deb) ...
Selecting previously unselected package libxau6:amd64.
Unpacking libxau6:amd64 (from .../libxau6_1%3a1.0.7-1_amd64.deb) ...
Selecting previously unselected package libxdmcp6:amd64.
Unpacking libxdmcp6:amd64 (from .../libxdmcp6_1%3a1.1.1-1_amd64.deb) ...
Selecting previously unselected package libxcb1:amd64.
Unpacking libxcb1:amd64 (from .../libxcb1_1.8.1-2+deb7u1_amd64.deb) ...
Selecting previously unselected package libx11-data.
Unpacking libx11-data (from .../libx11-data_2%3a1.5.0-1+deb7u2_all.deb) ...
Selecting previously unselected package libx11-6:amd64.
Unpacking libx11-6:amd64 (from .../libx11-6_2%3a1.5.0-1+deb7u2_amd64.deb) ...
Selecting previously unselected package libxext6:amd64.
Unpacking libxext6:amd64 (from .../libxext6_2%3a1.3.1-2+deb7u1_amd64.deb) ...
Selecting previously unselected package libxmuu1:amd64.
Unpacking libxmuu1:amd64 (from .../libxmuu1_2%3a1.1.1-1_amd64.deb) ...
Selecting previously unselected package openssh-blacklist.
Unpacking openssh-blacklist (from .../openssh-blacklist_0.4.1+nmu1_all.deb) ...
Selecting previously unselected package openssh-blacklist-extra.
Unpacking openssh-blacklist-extra (from .../openssh-blacklist-extra_0.4.1+nmu1_all.deb) ...
Selecting previously unselected package adduser.
Unpacking adduser (from .../adduser_3.113+nmu3_all.deb) ...
Selecting previously unselected package less.
Unpacking less (from .../archives/less_444-4_amd64.deb) ...
Selecting previously unselected package libclass-isa-perl.
Unpacking libclass-isa-perl (from .../libclass-isa-perl_0.36-3_all.deb) ...
Selecting previously unselected package perl-modules.
Unpacking perl-modules (from .../perl-modules_5.14.2-21+deb7u4_all.deb) ...
Selecting previously unselected package perl.
Unpacking perl (from .../perl_5.14.2-21+deb7u4_amd64.deb) ...
Selecting previously unselected package libswitch-perl.
Unpacking libswitch-perl (from .../libswitch-perl_2.16-2_all.deb) ...
Selecting previously unselected package openssh-client.
Unpacking openssh-client (from .../openssh-client_1%3a6.0p1-4+deb7u6_amd64.deb) ...
Selecting previously unselected package patch.
Unpacking patch (from .../patch_2.6.1-3_amd64.deb) ...
Selecting previously unselected package liberror-perl.
Unpacking liberror-perl (from .../liberror-perl_0.17-1_all.deb) ...
Selecting previously unselected package git-man.
Unpacking git-man (from .../git-man_1%3a1.7.10.4-1+wheezy3_all.deb) ...
Selecting previously unselected package git.
Unpacking git (from .../git_1%3a1.7.10.4-1+wheezy3_amd64.deb) ...
Selecting previously unselected package gitlab-ci-multi-runner.
Unpacking gitlab-ci-multi-runner (from .../gitlab-ci-multi-runner_1.5.3_amd64.deb) ...
Selecting previously unselected package rsync.
Unpacking rsync (from .../rsync_3.0.9-4_amd64.deb) ...
Selecting previously unselected package xauth.
Unpacking xauth (from .../xauth_1%3a1.0.7-1_amd64.deb) ...
Setting up libgdbm3:amd64 (1.8.3-11) ...
Setting up libpopt0:amd64 (1.16-7) ...
Setting up libbsd0:amd64 (0.4.2-1) ...
Setting up libedit2:amd64 (2.11-20080614-5) ...
Setting up libexpat1:amd64 (2.1.0-1+deb7u4) ...
Setting up libxau6:amd64 (1:1.0.7-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.8.1-2+deb7u1) ...
Setting up libx11-data (2:1.5.0-1+deb7u2) ...
Setting up libx11-6:amd64 (2:1.5.0-1+deb7u2) ...
Setting up libxext6:amd64 (2:1.3.1-2+deb7u1) ...
Setting up libxmuu1:amd64 (2:1.1.1-1) ...
Setting up openssh-blacklist (0.4.1+nmu1) ...
Setting up openssh-blacklist-extra (0.4.1+nmu1) ...
Setting up adduser (3.113+nmu3) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Setting up less (444-4) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Setting up libclass-isa-perl (0.36-3) ...
Setting up openssh-client (1:6.0p1-4+deb7u6) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Setting up patch (2.6.1-3) ...
Setting up git-man (1:1.7.10.4-1+wheezy3) ...
Setting up rsync (3.0.9-4) ...
update-rc.d: using dependency based boot sequencing
invoke-rc.d: policy-rc.d denied execution of restart.
Setting up xauth (1:1.0.7-1) ...
Setting up perl-modules (5.14.2-21+deb7u4) ...
Setting up perl (5.14.2-21+deb7u4) ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode
Setting up liberror-perl (0.17-1) ...
Setting up git (1:1.7.10.4-1+wheezy3) ...
Setting up gitlab-ci-multi-runner (1.5.3) ...
GitLab Runner: creating gitlab-runner...
Setting up libswitch-perl (2.16-2) ...
```

サービスを起動しようとしたらエラーが出る。
調べてみると既に起動している(笑)

```
# service gitlab-runner status
[ ok ] GitLab Runner is running.
```

## プロジェクトに登録
プロジェクト側の```token```で直接プロジェクトに登録します。
```Gitlab``` 上プロジェクトの```token```はプロジェクトを開いて右上に表示される「歯車アイコン」→「runner」をクリックすると赤字で```token```が表示されます。

![スクリーンショット 2016-09-22 17.06.59.png](https://qiita-image-store.s3.amazonaws.com/0/12727/c556b4a8-8d6b-2ac4-3d60-4f409dad9ad0.png "プロジェクトのtokun")


### 登録コマンド

```text:
# gitlab-ci-multi-runner register
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
http://gitlab/ci
Please enter the gitlab-ci token for this runner:
52h1HW-mgqwFCFrDwT4K
Please enter the gitlab-ci description for this runner:
[debian-runner]: 
Please enter the gitlab-ci tags for this runner (comma separated):

Registering runner... succeeded                     runner=52h1HW-m
Please enter the executor: shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, parallels:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
```


いくつか登録内容を聞かれるので入力します。

- ```http://gitlab/ci```<br> これは登録したい```gitlab```サーバーのURLに```ci```付けて、登録先を指定します。

- ```52h1HW-mgqwFCFrDwT4K```<br> これは先ほど調べたプロジェクトの```token```です。

- ```debian-runner```<br> ```runner```の名前ですね。```Gitlab``` 側で表示されても分かるように付けましょう。

- 次にタグですけど、カンマで複数登録できますしここでは空にしています。<br> ```CI``` 処理にこのタグで処理するという時の判断に使われます。

- ```shell``` 今回は```debian```上で走らせるために```shell```を指定します。



登録が上手くいくとプロジェクトの設定画面で次のようになります。


![スクリーンショット 2016-09-22 17.08.50.png](https://qiita-image-store.s3.amazonaws.com/0/12727/94d95338-e739-71fc-b68e-cfa0fbcdbe58.png "プロジェクトと結びついたrunner")


# CI実行
## CI定義ファイル設定
以下のように設定ファイルを用意し上記登録したプロジェクトにプッシュします。

```text:.gitlab-ci.yaml
image: debian:7.11

stages:
  - check
  - test
  - deploy

code_check:
  stage: check
  script:
    - pwd
    - whoami

test1:
  stage: test
  script:
    - echo "test"

deploy_production1:
  stage: deploy
  script:
    - echo "deploy(production)"
  only:
    - release/production

deploy_staging1:
  stage: deploy
  script:
    - echo "deploy(staging)"
  only:
    -  release/staging
```

## 実行結果
実行した結果は以下のようになります。
登録を何回か繰り返したので上の図と登録番号が違いますが、登録した```runner```のidでフォルダが作成されるようです。実行ユーザーは```gitlab-runner```になっています。また[docker編](http://qiita.com/takara@github/items/dce4f443f18e9f115760)で使用していた```image```は無視されているようです。

![スクリーンショット 2016-09-23 0.08.31.png](https://qiita-image-store.s3.amazonaws.com/0/12727/506e561e-1dca-fb2f-14f9-6393068baaac.png "実行結果")

# 関連
- [Gitlab日本語化(docker)インストール](http://qiita.com/takara@github/items/06577c49b7e1d3f68102)
- [Gitlabでlfs](http://qiita.com/takara@github/items/554edd6b22c5dee1be36)
- [GitlabでCI（イメージ）](http://qiita.com/takara@github/items/838d5828e38800f7f72b)
- [GitlabでCI docker編](http://qiita.com/takara@github/items/dce4f443f18e9f115760)

# 参考サイト
- [Run gitlab-runner in a container](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/docker.md)
- [Install using GitLab's repository for Debian/Ubuntu/CentOS/RedHat (preferred)](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/linux-repository.md)

[^1]: 公式のドキュメント通り