CirclCI自動ビルド検証

#概要

* CirclCIを導入検証する
* CircleCIはビルド・テスト・デプロイのCI/CDを自動で実行するサービス
* とりあえずmasterブランチにPUSHしたらビルドが走るという挙動を試す
* デプロイは次回
* 無料で結構使えるので前向きに検討

#設定

#### アカウント登録

* Githubアカウントがある事が前提

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/5badd120-479b-4b66-d8b6-df089eed4a6a.png width=75%>

* "Sign up with github"でアカウント登録

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/9b0ec528-bc4e-099d-c4f8-c2125cde2e14.png width=75%>

* "Authorize CirclCI"で承認

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/60fe20df-b4de-5a90-94ee-e2be1835ce26.png width=50%>

* "Githubのパスワードを入力

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/e5258060-0304-f0af-6595-a7a140489f56.png width=50%>

#### プロジェクト作成

* CirclCIの画面へ遷移

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/826e143f-5b29-7f2b-7983-99a05eb703fb.png width=75%>

* "Add Project"を選択しプロジェクトを設定する画面へ遷移
* ProjectイコールGithubのリポジトリ

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/4ad79378-c5a3-6ba2-5678-b3b0388fc780.png width=10%>

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/e4ba34d5-6156-328b-6b5d-25f2a78fa5c2.png width=75%>

* "Set Up Project"をクリック

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/66de76de-ba88-ad57-b683-5c706e7b1ec3.png width=50%>

* プロジェクト設定画面へ遷移。OSと言語を選択。
* ”Copy to Clipboad"でSample .yml をコピー
* ローカルで<work_dir>/.circleci/config.yml新規ファイル作成しペースト

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/90b5c89d-e98f-b504-28fa-90f5721351fd.png width=75%>

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/6f0a36ad-25d2-598c-d3c0-47bf35c4e213.png width=75%>

<img src=https://qiita-image-store.s3.amazonaws.com/0/57350/b6a44aae-56f2-2d41-eb3c-84e1a2301db2.png width=75%>

```
$ git clone https://github.com/<repo-name>/circleci.git
$ cd circleci
$ mkdir .circleci
$ vim .circleci/config.yml
$ git add . 
$ git commit -m 'add .circleci/config.yml'
$ git push origin master
```


```go:config.yml
# Golang CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
jobs:
  build:
    docker:
      # specify the version
      - image: circleci/golang:1.9

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    #### TEMPLATE_NOTE: go expects specific checkout path representing url
    #### expecting it in the form of
    ####   /go/src/github.com/circleci/go-tool
    ####   /go/src/bitbucket.org/circleci/go-tool
    working_directory: /go/src/github.com/{{ORG_NAME}}/{{REPO_NAME}}
    steps:
      - checkout

      # specify any bash command here prefixed with `run: `
      - run: go get -v -t -d ./...
      - run: go test -v ./...
```

# ビルド開始

* "start building"をクリックしビルド開始
* ビルドが成功すると”success"が表示される

![image.png](https://qiita-image-store.s3.amazonaws.com/0/57350/9de34e35-fb4b-ad4c-b1b5-faccfab8e8de.png)

* "Jobs"タブを選びビルドの様子をモニタリング

![image.png](https://qiita-image-store.s3.amazonaws.com/0/57350/e8d23e06-73ff-cdc0-2f48-27a9e560c7c7.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/57350/7d5fd157-a457-71a5-d6d2-a272b39cccc4.png)

## 出力サンプル

#### Spin up Environment

```
Build-agent version 1.0.9934-d6876d00 (2019-04-01T12:08:20+0000)
Docker Engine Version: 17.05.0-ce
Kernel Version: Linux 681d5dd07475 4.4.0-141-generic #167~14.04.1-Ubuntu SMP Mon Dec 10 13:20:24 UTC 2018 x86_64 Linux
Starting container circleci/golang:1.9
  image cache not found on this host, downloading circleci/golang:1.9
1.9: Pulling from circleci/golang
:
:
Digest: sha256:acc81847ea4e5b11a61f62aaab537dc622027e406f2b36da6e36215aa7c89447
Status: Downloaded newer image for circleci/golang:1.9
  using image circleci/golang@sha256:acc81847ea4e5b11a61f62aaab537dc622027e406f2b36da6e36215aa7c89447

Using build environment variables
  BASH_ENV=/tmp/.bash_env-5ca5e9ead40cb80009de2c6f-0-build
  CI=true
  CIRCLECI=true
  CIRCLE_BRANCH=master
  CIRCLE_BUILD_NUM=2
  CIRCLE_BUILD_URL=https://circleci.com/gh/<org_name>/circleci/2
  CIRCLE_COMPARE_URL=
  CIRCLE_JOB=build
  CIRCLE_NODE_INDEX=0
  CIRCLE_NODE_TOTAL=1
  CIRCLE_PREVIOUS_BUILD_NUM=1
  CIRCLE_PROJECT_REPONAME=circleci
  CIRCLE_PROJECT_USERNAME=<org_name>
  CIRCLE_REPOSITORY_URL=git@github.com:<org_name>/circleci.git
  CIRCLE_SHA1=200f1a89b0398b0aab9c9031fc73f61f18be00af
  CIRCLE_SHELL_ENV=/tmp/.bash_env-5ca5e9ead40cb80009de2c6f-0-build
  CIRCLE_STAGE=build
  CIRCLE_USERNAME=<org_name>
  CIRCLE_WORKFLOW_ID=eabfbd03-aae7-4da9-b80d-fce9e8b78aab
  CIRCLE_WORKFLOW_JOB_ID=232e1300-5f4b-4744-82fe-6bd74eefa92e
  CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS=
  CIRCLE_WORKFLOW_WORKSPACE_ID=eabfbd03-aae7-4da9-b80d-fce9e8b78aab
  CIRCLE_WORKING_DIRECTORY=/go/src/github.com/<org_name>/circleci

Using environment variables from project settings and/or contexts
  CIRCLE_JOB=**REDACTED**
```



#### Checkout code

```
#!/bin/sh
set -e

\# Workaround old docker images with incorrect $HOME
\# check https://github.com/docker/docker/issues/2968 for details
if [ "\${HOME}" = "/" ]
then
  export HOME=$(getent passwd $(id -un) | cut -d: -f6)
fi

mkdir -p ~/.ssh

echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQExxxxxxxxxxxxxxx43JXiUFFAaQ==
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxxxxxxxxxxxxxxxVBoGqzHM9yXw==
' >> ~/.ssh/known_hosts

(umask 077; touch ~/.ssh/id_rsa)
chmod 0600 ~/.ssh/id_rsa
(cat <<EOF > ~/.ssh/id_rsa
$CHECKOUT_KEY
EOF
)

#### use git+ssh instead of https
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
git config --global gc.auto 0 || true

if [ -e /go/src/github.com/<org_name>/circleci/.git ]
then
  cd /go/src/github.com/<org_name>/circleci
  git remote set-url origin "$CIRCLE_REPOSITORY_URL" || true
else
  mkdir -p /go/src/github.com/<org_name>/circleci
  cd /go/src/github.com/<org_name>/circleci
  git clone "$CIRCLE_REPOSITORY_URL" .
fi

if [ -n "$CIRCLE_TAG" ]
then
  git fetch --force origin "refs/tags/${CIRCLE_TAG}"
else
  git fetch --force origin "master:remotes/origin/master"
fi


if [ -n "$CIRCLE_TAG" ]
then
  git reset --hard "$CIRCLE_SHA1"
  git checkout -q "$CIRCLE_TAG"
elif [ -n "$CIRCLE_BRANCH" ]
then
  git reset --hard "$CIRCLE_SHA1"
  git checkout -q -B "$CIRCLE_BRANCH"
fi

git reset --hard "$CIRCLE_SHA1"
```

```
Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '192.30.xxx.xxx' to the list of known hosts.

remote: Enumerating objects: 16, done.        
remote: Counting objects: 100% (16/16), done.        
remote: Compressing objects: 100% (10/10), done.        
remote: Total 16 (delta 4), reused 7 (delta 2), pack-reused 0        
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (4/4), done.
HEAD is now at 200f1a8 add .circleci/config.yml
HEAD is now at 200f1a8 add .circleci/config.yml
```


* go get -v -t -d ./...

```
#!/bin/bash -eo pipefail
go get -v -t -d ./...
```


* go test -v ./...

```
#!/bin/bash -eo pipefail
go test -v ./...
=== RUN   TestHelloWorld
--- PASS: TestHelloWorld (0.00s)
PASS
ok  	github.com/masamitsu-ezaki/circleci	0.001s
```

# 所感

* 問題なくビルドできた
* UIも見やすい。AWSやJenkinsに比べてもかなり扱いやすく見やすい
* yamlファイルをリポジトリに入れて設定するのはCodeBuildなども同じ。簡単にできそう。
* Githubとの親和性はこちらのほうが高い。Privateリポジトリが無料になったので使わない手はないかな。
* Slack通知が簡単
* デプロイの方も検証してみる