CircleCI Orbsミニハッカソンに参加しました。
ハッシュタグ:#circlecijp
資料:https://docs.google.com/presentation/d/1cZ0fn8_ooqiDfQUPfsvNF3s5TDrHvavBboa1XWoI8qQ/edit#slide=id.g4a5e094114_0_22
ハッカソンの趣旨説明
CircleCIの新機能Orbsを作りながら学ぶ!
CircleCI 2.1 新機能
- stepsで独自コマンド定義できる。
- コマンドにパラメータ渡せる
- ``run: echo << parameters.greeting >>
- 実行環境に関する情報をまとめる
executors
が作れる- 複数ジョブで共通にして利用する環境(Dockerイメージなど)を定義できる
Orbs
- Config 2.1の機能を使って誰でもコンフィグを配布したりできる
- Orbs Registry
- https://circleci.com/orbs/registry/
- デフォルトはCERTIFIED、PARTNERのみ。右上チェックボックス選択でそれ以外も出る。
Orbsの作り方
- CircleCI CLIのインストール
- Dockerのインストール(テスト用)
- Orb Namespaceの作成
- 開発版のOrbは90日後削除される
- ひとつのOrganizationには、ひとつのnamespace
- 参考情報
デモ学びました
教材はMarekのリポジトリ。 https://github.com/yundt/orbs-hackathon-demo
$ circleci setup
# API Tokenを入力。CircleCI hostはhttps://circleci.com
$ circleci namespace create na0ya github na0ya
Namespace `na0ya` created.Please note that any orbs you publish in this namespace are open orbs and are world-readable.
$ circleci orb create na0ya/greet-test
Orb `na0ya/greet-test` created.
Please note that any versions you publish of this orb are world-readable.
You can now register versions of `na0ya/greet-test` using `circleci orb publish`.
$ circleci orb validate orb.yml
Orb at `orb.yml` is valid.
$ circleci orb publish orb.yml na0ya/greet-test@dev:first
Orb `na0ya/greet-test@dev:first` was published.Please note that this is an open orb and is world-readable.
Note that your dev label `dev:first` can be overwritten by anyone in your organization.Your dev orb will expire in 90 days unless a new version is published on the label `dev:first`.
$ circleci orb publish promote na0ya/greet-test@dev:first patch
Orb `na0ya/greet-test@dev:first` was promoted to `na0ya/greet-test@0.0.1`.
Please note that this is an open orb and is world-readable.
Orbsのレジストリにこれで登録される。
https://circleci.com/orbs/registry/orb/na0ya/greet-test
CircleCI上で動かすためのconfig.yml書いてみる。
今回のorb.ymlはcommandsのみ定義なので、どこかのjobsの下のstepsとして紛れ込ませる。run
とか書かずに指定する。
version: 2.1
orbs:
greeting: na0ya/greet-test@0.0.1
jobs:
build:
docker:
- image: circleci/openjdk:latest
steps:
- run: "echo 'testing building...'"
- greeting/hello:
username: na0ya
workflows:
main:
jobs:
- build
config.ymlのインデントが悪くて、以下のエラーがでて、ハマりまくりました。
$ circleci config validate
Error: Error calling workflow: 'main'
Error calling job: 'build'
Error calling command: 'greeting/hello'
Expected a map of parameter names to argument values
Received a with value:
nil
Check that each argument is provided as `parameter-name: argument-value`
あと、orb.yml書き換えてて、自動インデントがタブだったため、またハマる(笑)validate大事。
$ circleci orb validate orb.yml
Error: Unable to parse YAML
while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
in 'string', line 11, column 1:
description: A name of the user ...
開発物のテスト
@dev
で作成したものをローカルで取り込んで動かしてみる。
CLIは2018/12/15時点では、config2.1対応していないので、circleci config process .circleci/config.yml
コマンドでversion 2に変換してから、circleci local execute
みたいなことやると良いかも。
実行例:
$ circleci config process .circleci/config.yml
# Orb 'na0ya/greet-test@0.0.1' resolved to 'na0ya/greet-test@0.0.1'
version: 2
jobs:
build:
docker:
- image: circleci/openjdk:latest
steps:
- run:
command: echo 'testing building...'
- run:
command: echo 'CircleCI Orbs ミニハッカソンへようこそ、na0ya!'
workflows:
main:
jobs:
- build
version: 2
# Original config.yml file:
# version: 2.1
#
# orbs:
# greeting: na0ya/greet-test@0.0.1
#
# jobs:
# build:
# docker:
# - image: circleci/openjdk:latest
# steps:
# - run: \"echo 'testing building...'\"
# - greeting/hello:
# username: na0ya
#
# workflows:
# main:
# jobs:
# - build
#
本題
Redmineプラグイン開発していて、テスト実行時の環境設定がめんどくさいので、Orb化してみる。
orb.yml作る
今回は、環境構築なので、executors
を含むOrbになるはず。コンテナとか環境変数とか定義した後で、Redmineのインストール(展開をする)処理をcommands
ブロックで書いておく。
使う側としては、下記のようなconfig.yml
を記載しておくイメージ。
version: 2.1
orbs:
redmine_executor: na0ya/redmine-plugin-env@dev:0.0.1
jobs:
build:
executor: redmine_executor/default
steps:
- redmine_executor/install
- あとは、必要な処理を書いていく。。。
workflows:
main:
jobs:
- build
作成したorb.yml
version: 2.1
description: Executor orb for Redmine Plugin Development
executors:
default:
description: |
Redmine container for plugin development
parameters:
redmine_ver:
type: string
default: "3.4.6"
orb_working_directory:
type: string
default: "~/redmine/"
docker:
- image: circleci/ruby:2.3-node-browsers
environment:
LANG: C.UTF-8
REDMINE_VER: redmine-<< parameters.redmine_ver >>
RAILS_ENV: test
REDMINE_LANG: ja
BUNDLE_PATH: /tmp/bundle
- image: circleci/postgres:9.5-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_DB: redmine_plugin_test
POSTGRES_PASSWORD: postgres
commands:
install:
description: |
setup Redmine from archive
steps:
- checkout
- run:
name: Setup Redmine
command: |
curl http://www.redmine.org/releases/$REDMINE_VER.tar.gz | tar zx
mv $REDMINE_VER ~/redmine
まずは、redmineの展開完了なので、ここのpluginsフォルダにcheckoutしたコードとか入れて、bundle install
とか実行していく感じか。がんばって書いてみる。
参考ドキュメント
GitterでQ&Aした
- Q:
@dev
でpublishしたものは、同じorganizationからしか見えないのか?- A:誰でも見れる。Orb Registryの一覧には出てこないだけ。
- Q:config 2.1に対して、
circleci local execute
やったら失敗した。- A:2.1はまだ未サポート。
circleci config process
コマンド実行して、version 2に変換したら実行可能。
- A:2.1はまだ未サポート。
- Q:レジストリにpublishしたOrbを削除する方法はあるの?
- A:基本的にはできないです。サポートに連絡して消すっていう方法もあるけど。。。