Concourse使ってみた

* https://concourse.ci/single-page.html

## 1. コンテナ間用のssh鍵作成

```rb:鍵作成
mkdir -p keys/web keys/worker
 
ssh-keygen -t rsa -f ./keys/web/tsa_host_key -N ''
ssh-keygen -t rsa -f ./keys/web/session_signing_key -N ''
 
ssh-keygen -t rsa -f ./keys/worker/worker_key -N ''
ssh-keygen -t rsa -f ./keys/worker/darwin_worker_key -N ''
 
cat ./keys/worker/worker_key.pub >> ./keys/web/authorized_worker_keys
cat ./keys/worker/darwin_worker_key.pub >> ./keys/web/authorized_worker_keys
 
cp ./keys/web/tsa_host_key.pub ./keys/worker
```

```sh:作成た鍵一覧
keys/web/session_signing_key
keys/web/session_signing_key.pub
keys/web/authorized_worker_keys
keys/web/tsa_host_key
keys/web/tsa_host_key.pub
keys/worker/worker_key
keys/worker/worker_key.pub
keys/worker/darwin_worker_key
keys/worker/tsa_host_key.pub
keys/worker/darwin_worker_key.pub
```

## 2. docker-compose.yml

```rb:docker-compose.yml
version: '3'
 
services:
  concourse-db:
    image: postgres:9.5
    environment:
      POSTGRES_DB: concourse
      POSTGRES_USER: concourse
      POSTGRES_PASSWORD: changeme
      PGDATA: /database
 
  concourse-web:
    image: concourse/concourse
    links: [concourse-db]
    depends_on: [concourse-db]
    command: web
    restart: unless-stopped
    ports: ["8080:8080", "2222:2222"]
    volumes: ["./keys/web:/concourse-keys"]
    environment:
      #CONCOURSE_EXTERNAL_URL: "${CONCOURSE_EXTERNAL_URL}"
      CONCOURSE_BASIC_AUTH_USERNAME: concourse
      CONCOURSE_BASIC_AUTH_PASSWORD: changeme
      CONCOURSE_POSTGRES_HOST: concourse-db
      CONCOURSE_POSTGRES_USER: concourse
      CONCOURSE_POSTGRES_DATABASE: concourse
      CONCOURSE_POSTGRES_PASSWORD: changeme
 
  concourse-worker:
    image: concourse/concourse
    privileged: true
    links: [concourse-web]
    depends_on: [concourse-web]
    command: worker
    volumes: ["./keys/worker:/concourse-keys"]
    environment:
      CONCOURSE_TSA_HOST: concourse-web
```

```sh:起動
docker-compose up -d
```

## 3. fly コマンド取得

* http://localhost:8080 にアクセスし、fly をダウンロード

![1.png](https://qiita-image-store.s3.amazonaws.com/0/25728/d07f3080-71fa-72b9-db0a-f49339396cdd.png)

```sh:flyにconcourseを登録
./fly -t concourseci login -c http://localhost:8080
```

```rb:targets
$ ./fly targets
name         url                    team  expiry                       
concourseci  http://localhost:8080  main  Sun, 10 Sep 2017 21:19:48 UTC
```

```sh:worker一覧表示
$ ./fly -t concourseci workers
name          containers  platform  tags  team  state    version
b8854618b82c  0           linux     none  none  running  1.2    
```

## 4. とあるコマンドだけを実行するだけのpipelineを登録してみる

* https://concourse.ci/hello-world.html

```rb:pipeline2.yml(書き方1)
---

jobs:
- name: hello-world
  plan:
  - task: run-tests
    config:
      platform: linux
      image_resource:
        type: docker-image
        source:
          repository: ubuntu
      run:
        path: /bin/bash
        args:
        - -c
        - |
          echo "Hello, world"
```

```rb:pipeline2.yml(書き方2)
---
jobs:
- name: hello-world
  plan:
  - task: say-hello
    config:
      platform: linux
      image_resource:
        type: docker-image
        source: {repository: ubuntu}
      run:
        path: echo
        args: ["Hello, world!"]
```

```rb:pipelineの登録
./fly -t concourseci set-pipeline -c pipeline2.yml -p hello-world
./fly -t concourseci unpause-pipeline -p hello-world
```

![スクリーンショット_2017-09-10_06-49-04.png](https://qiita-image-store.s3.amazonaws.com/0/25728/dfbd5206-16c8-25e1-ee0e-e31c6e58dde4.png)

## 5. pipeline 削除

```
./fly -t concourseci ps
./fly -t concourseci dp -p パイプライン名
```

```sh:worker削除
./fly -t concourseci workers
./fly -t concourseci prune-worker -w ワーカー名
```

## 6. worker にネイティブのLinuxを追加

* [Jenkins はもうオワコン？ Concourse CI で iOS 向けビルドをやってみた (フェンリル | デベロッパーズブログ)](https://blog.fenrir-inc.com/jp/2017/07/how_to_build_ios_codes_by_concourse-ci.html)

https://github.com/concourse/concourse/releases/ から `concourse_linux_amd64`をダウンロード

```rb
sudo ./concourse worker \
  --name native1 \
  --work-dir ./working \
  --tsa-host 127.0.0.1 \
  --tsa-public-key ./keys/worker/tsa_host_key.pub \
  --tsa-worker-private-key ./keys/worker/darwin_worker_key
```

下記のようなログが出力されます。

> {"timestamp":"1504994760.335169315","source":"tsa","message":"tsa.connection.channel.forward-worker.register.done"...



```sh:native1が追加されたことを確認
$ ./fly -t concourseci workers
name          containers  platform  tags  team  state    version
b8854618b82c  0           linux     none  none  running  1.2  
native1       0           linux     none  none  running  1.2      
```

使い方はようわからん