Code Monkey home page Code Monkey logo

duci's Introduction

duci

Language GitHub release GoDoc Build Status Coverage Status Go Report Card MIT License

duci [zushi] (Docker Under Continuous Integration) is a simple ci server.

DSL is Unnecessary For CI

Let's define the task in the task runner.
Let's define the necessary infrastructure for the task in the Dockerfile.
duci just only execute the task in docker container.

Features

  • Execute the task in Docker container
  • Execute the task triggered by GitHub pull request comment or push
  • Execute tasks asynchronously
  • Create GitHub commit status
  • Store and Show logs

How to use

Target Repository

The target repository must have Dockerfile in repository root or .duci/Dockerfile.
If there is .duci/Dockerfile, duci read it preferentially.

In Dockerfile, I suggest to use ENTRYPOINT.

e.g.

ENTRYPOINT ["mvn"]
CMD ["compile"]
ENTRYPOINT ["fastlane"]
CMD ["build"]

When push to github, duci execute mvn compile / fastlane build.
And when comment ci test on github pull request, execute mvn test / fastlane test.

Using host environment variables

If exists ARG instruction in Dockerfile, override value from host environment variable.

ARG FOO=default
ARG BAR

and you can use as envrionment variable in command.

ARG FOO=default
ENV FOO=$FOO

Runtime configuration

volumes

You can use volumes options for external dependency, cache and etc.
Set configurations in .duci/config.yml

volumes:
  - '/path/to/host/dir:/path/to/container/dir'

environment variable

You can set environment variables in docker container.
Add the following to .duci/config.yml

environments:
  - ENVIRONMENT_VAIRABLE=value

Server Settings

Installation

# binary will be $(go env GOPATH)/bin/duci
curl -sfL https://raw.githubusercontent.com/duck8823/duci/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
duci version

You can also install a specific version. (replace vX.Y.Z with the specific version from the releases page):

# binary will be $(go env GOPATH)/bin/duci
curl -sfL https://raw.githubusercontent.com/duck8823/duci/master/install.sh | sh -s -- -b $(go env GOPATH)/bin vX.Y.Z
duci version

Setting SSH (optional)

If target repository is private, You can use SSH key to clone repository from github.com.
Please set the public key of the pair at https://github.com/settings/keys.

Add Webhooks to Your GitHub repository

duci start to listen webhook with port 8080 (default) and endpoint /.
In GitHub target repository settings (https://github.com/<owner>/<repository>/settings/hooks), Add endpoint of duci to Payload URL and application/json to Content type respectively.

Run Server

$ duci server

Server Configuration file

You can specify configuration file with -c option. The configuration file must be yaml format. Possible values ​​are as follows.

server:
  workdir: '/path/to/tmp/duci'
  port: 8080
  database_path: '$HOME/.duci/db'
github:
  # (optional) You can use SSH key to clone. ex. '${HOME}/.ssh/id_rsa'
  ssh_key_path: ''
  # For create commit status. You can also use environment variable
  api_token: ${GITHUB_API_TOKEN}
job:
  timeout: 600
  concurrency: 4 # default is number of cpu

You can check the configuration values.

$ duci config

Using Docker

You can use Docker to run server.

$ docker run -p 8080:8080 \
             -e GITHUB_API_TOKEN=<your toekn> \
             -v /var/run/docker.sock:/var/run/docker.sock \
             duck8823/duci

When you want to clone with SSH in container,

$ docker run -p 8080:8080 \
             -e GITHUB_API_TOKEN=<your toekn> \
             -e SSH_KEY_PATH=/root/.ssh/id_rsa \
             -v ~/.ssh:/root/.ssh:ro \ 
             -v /var/run/docker.sock:/var/run/docker.sock \
             duck8823/duci

Run with docker-compose

With docker-compose, you can also start ui and reverse proxy together.

$ git clone https://github.com/duck8823/duci.git
$ cd duci
$ docker-compose up -d

If you start up on another host, set your host name (default: localhost) to environment variable DUCI_HOST.

Read job log

GitHub send payload as webhook including X-GitHub-Delivery header.
You can read job log with the X-GitHub-Delivery value formatted UUID.

$ curl -XGET http://localhost:8080/logs/{X-GitHub-Delivery}

The endpoint returns NDJSON (Newline Delimited JSON) formatted log.

{"time":"2018-09-21T22:19:42.572879+09:00","message":"Step 1/10 : FROM golang:1.11-alpine"}
{"time":"2018-09-21T22:19:42.573093+09:00","message":"\n"}
{"time":"2018-09-21T22:19:42.573494+09:00","message":" ---\u003e 233ed4ed14bf\n"}
{"time":"2018-09-21T22:19:42.573616+09:00","message":"Step 2/10 : MAINTAINER shunsuke maeda \[email protected]\u003e"}
{"time":"2018-09-21T22:19:42.573734+09:00","message":"\n"}
...

Health Check

This server has an health check API endpoint (/health) that returns the health of the service. The endpoint returns 200 status code if all green.

$ curl -XGET -I http://localhost:8080/health
HTTP/1.1 200 OK
Date: Wed, 31 Oct 2018 20:33:42 GMT
Content-Length: 0

The check items are as follows

  • Whether the Docker daemon is running or not

You can also check with health sub-command.

$ duci health
INFO[14/Jan/2019 07:17:38.864] ok.

License

MIT License

Copyright (c) 2018 Shunsuke Maeda

See LICENSE file

duci's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar duck8823 avatar wasanx25 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

duci's Issues

サブコマンドで GItHub の設定をできるようにしたい

  • SSH Key の生成と設定
  • Webhooks の設定

が サブコマンド でできると導入コストがグッと減っていいと思った。
イメージとしては

duci init

で ssh key を作って GitHub に登録

duci add duck8823/duci

で webhooks に登録

duci server

で サーバー起動

ChecksAPI

GitHub の ChecksAPI について調べる。
使えそうだったら使う。

非同期にする

webhooks が timeout になるので非同期にする。
wenhooks はジョブのIDを返して、後から結果とログを取得できるようにしよう。

テストのcleanup

コンテナとイメージの削除をしないとディスクが圧迫している。

go get で失敗している

infrastructure/docker/docker.go:9:2: code in directory /go/src/github.com/moby/moby/client expects import "github.com/docker/docker/client"

Waiting の Commit Status をつける

現在並列数を指定しているが、前のジョブが終わってから次のジョブを実行して初めて pending の Commit Status がつく。
キューに入ったことが分かりづらいのでコミットステータスをつけるようにする。

ログをファイルに保存する

今はインメモリな leveldb に保存しているが、ファイルに保存できるようにする。
ファイルパスは設定ファイルで変更できるようにする。

コミットハッシュをチェックアウトするように

指定されたブランチのチェックアウト後最新のコミットハッシュに対して実行しているが、
Runner にコミットハッシュを渡してそれをチェックアウトできるようにする。
それを使って Runner内で commit status を生成するようにし、プレゼンテーション層での commit status の作成はしないようにする。

ログURLを Commit Status に設定する

GitHubからリンクを辿れるようにしよう。
ホストURLはリクエストの中に含まれるものを使うか、設定ファイルで管理できるようにするか。

ログを取得できるように

#1 で非同期にしたけど、後からログが取得できないので取得できるようにしてあげよう。
リクエストごとに生成する UUID でも返して、 logs/{UUID} あたりで取得できるようにすればいい。

実行時に Volumes を渡せるようにする

テスト実行時に ホスト と volume を共有したい。
(ホスト依存のテスト書いてて本来はよくないけどそれは別でやる)

.duci/config.yml に以下のように記述するとコンテナに渡すようにする。

---
volumes:
  - /path/to/host:/path/to/container:ro

multi stage build

アプリ起動用の Dockerfile は multi stage build を利用する

設定ファイルほしい

各種設定ファイルで設定できるといいなあと思った。

  • サーバー起動時オプション ( 全体設定 )
    • working directory #26
    • port number #26
    • path of ssh private key #26
    • timeout #29
    • concurrency (同時ビルド数)
  • リポジトリオプション ( 対象での設定 )
    • path of dockerfile #27
    • timeout ( override )
    • volume 指定 ( cache的に使える )

GitLab対応

GitLabにも対応したい。

  • Webhooks
  • Commit Status

waiting... のまま実行されないケース

以下のログのあとジョブが実行されない。

[08654e00-ac53-11e8-87b2-90223981f97b] 2018-08-30 12:48:48.871 [INFO] Compressing objects: 100% (170/170)
[08654e00-ac53-11e8-87b2-90223981f97b] 2018-08-30 12:48:48.871 [INFO] Compressing objects: 100% (170/170), done.
[08654e00-ac53-11e8-87b2-90223981f97b] 2018-08-30 12:48:48.983 [INFO] Total 240 (delta 95), reused 146 (delta 47), pack-reused 0
[08654e00-ac53-11e8-87b2-90223981f97b] 2018-08-30 12:48:48.999 [ERROR] reference not found
github.com/duck8823/duci/infrastructure/git.(*sshGitClient).Clone
	/home/ubuntu/go/src/github.com/duck8823/duci/infrastructure/git/git.go:37
github.com/duck8823/duci/application/service/runner.(*DockerRunner).run
	/home/ubuntu/go/src/github.com/duck8823/duci/application/service/runner/runner.go:85
github.com/duck8823/duci/application/service/runner.(*DockerRunner).Run.func1
	/home/ubuntu/go/src/github.com/duck8823/duci/application/service/runner/runner.go:50
runtime.goexit
	/home/ubuntu/.goenv/versions/1.10.3/src/runtime/asm_amd64.s:2361
github.com/duck8823/duci/application/service/runner.(*DockerRunner).run
	/home/ubuntu/go/src/github.com/duck8823/duci/application/service/runner/runner.go:87
github.com/duck8823/duci/application/service/runner.(*DockerRunner).Run.func1
	/home/ubuntu/go/src/github.com/duck8823/duci/application/service/runner/runner.go:50
runtime.goexit
	/home/ubuntu/.goenv/versions/1.10.3/src/runtime/asm_amd64.s:2361
[08654e00-ac53-11e8-87b2-90223981f97b] 2018-08-30 12:48:49.216 [ERROR] Failed to create commit status: POST https://api.github.com/repos/duck8823/duci/statuses/0000000000000000000000000000000000000000: 422 No commit found for SHA: 0000000000000000000000000000000000000000 []

再利用しやすいように

こんな感じにしようと思いました。

minimal-ci
├── infrastructure  # 共通の基盤部分
├── service         # 再利用しやすい形でさらにパッケージわける
├── presentation    
│   └── controller  # ≒ hander
└── main.go

タスク実行準備の高速化

いくつか高速化をためす

  • イメージ名をリポジトリで固定してキャッシュを利用する
  • Git Clone 時のオプション( depth とか )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.