kb84tkhrのブログ

何を書こうか考え中です あ、あと組織とは関係ないってやつです 個人的なやつ

PPP3: Circle CI (続き6)

CircleCIにはCLIもある Using the CircleCI Local CLI - CircleCI
コレほしかった Validate A CircleCI Config

上から順番に見ていこう
ジョブをローカルで走らせるなんてことも書いてある
コンテナで動かしてるんだからできるか
これだとConfigをValidateするだけじゃなくていろいろ通るようにしてから
pushできるね(思ったとおりの機能なら)

インストールは一番単純な方法でやってみる

$ curl -fLSs https://circle.ci/cli | sudo bash
Starting installation.
Installing CircleCI CLI v0.1.5725
Installing to /usr/local/bin
/usr/local/bin/circleci

トークンとホストの設定
トークンはCircleCIのUser Settings - Personal API Tokensで
普通にCircleCIを使うならホストはデフォルトのままでOK

$ circleci setup
✔ CircleCI API Token: ****************************************
API token has been set.
✔ CircleCI Host: https://circleci.com
CircleCI host has been set.
Setup complete.
Your configuration has been saved to /home/takahiro/.circleci/cli.yml.

Trying an introspection query on API to verify your setup... Ok.
Trying to query our API for your profile name... Hello, Takahiro Kobayashi.

configをvalidateする

$ circleci config validate
Config file at .circleci/config.yml is valid.

わざとエラーを出してみる

$ circleci config validate
Error: Unable to parse YAML
while parsing a block mapping
 in 'string', line 1, column 1:
    version: 2
    ^
expected <block end>, but found '<block mapping start>'
 in 'string', line 15, column 3:
      test:
      ^

でもこれ、yamlの文法が合ってたらconfigとしてはおかしくても通っちゃう?
workflows配下まとめてインデントしたらvalidって言うんだよなあ
まあでもチェックが無いよりはいいよね

ローカルで動かすをやってみる

$ circleci local execute
Error: could not find `docker` on the PATH; please ensure that docker is installed

そうかdocker入ってないか
snapでできるみたいだからやってみるか
まだsnapに全幅の信頼を置いているわけではないけど

$ sudo snap install docker
docker 18.06.1-ce from Canonical✓ installed

あっさり
じゃもう1回

$ circleci local execute
Error: failed to connect to docker; please ensure that docker is running, and that `docker version` succeeds

えー
メッセージが違うからさっきよりはよくなったのか・・・

$ docker version
Client:
 Version:           18.06.1-ce
 ...
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/version: dial unix /var/run/docker.sock: connect: permission denied

permission deniedだからsudo?

$ sudo docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.4
 Git commit:        e68fc7a
 Built:             Tue May  7 17:57:34 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       e68fc7a
  Built:            Tue May  7 17:57:34 2019
  OS/Arch:          linux/amd64
  Experimental:     false

成功はしたけどこれであってるの?
circleciもsudoにしたら動く?

$ sudo circleci local execute
Downloading latest CircleCI build agent...
Docker image digest: sha256:463f8b03fa34e7609792bbc3e8983167fbb1221d9cefb1be401c0952973b3ebb
====>> Spin up Environment
Build-agent version 1.0.12193-577088f2 (2019-07-17T10:12:44+0000)
Error: Configuration errors: 1 error occurred:

* Cannot find a job named `build` to run in the `jobs:` section of your configuration file.
If you expected a workflow to run, check your config contains a top-level key called 'workflows:'
Step failed
Task failed
Error: 1 error occurred:

* Cannot find a job named `build` to run in the `jobs:` section of your configuration file.
If you expected a workflow to run, check your config contains a top-level key called 'workflows:'

エラーは出たけど進んだ
しかしこれは違うだろう
dockerというグループを作ってユーザを登録してやるといいのか
できた

さてエラーの方は何かな

jobs:セクションにbuildというジョブがないよ
ワークフローを動かしたかったらworkflows:っていうキーを作ってね

workflows:はあるから、buildだったところをlintとかtestっていう名前に
変えたからだろうか
testbuildに変えるか

お、動いた

$ circleci local execute
Docker image digest: sha256:463f8b03fa34e7609792bbc3e8983167fbb1221d9cefb1be401c0952973b3ebb
====>> Spin up Environment
Build-agent version 1.0.12193-577088f2 (2019-07-17T10:12:44+0000)
Docker Engine Version: 18.06.1-ce
Kernel Version: Linux 7d05073533ff 4.18.0-25-generic #26~18.04.1-Ubuntu SMP Thu Jun 27 07:28:31 UTC 2019 x86_64 Linux
Starting container circleci/python:3.6
  image cache not found on this host, downloading circleci/python:3.6
3.6: Pulling from circleci/python
5ae19949497e: Pulling fs layer
...
066c2434995b: Pull complete
Digest: sha256:c41a3ed08aab6b3c38badaea4ffcfe7674e081acf39d79dc23b40e6c2b1cf3d5
Status: Downloaded newer image for circleci/python:3.6
  using image circleci/python@sha256:c41a3ed08aab6b3c38badaea4ffcfe7674e081acf39d79dc23b40e6c2b1cf3d5

Using build environment variables
  BASH_ENV=/tmp/.bash_env-localbuild-1563707431
  CI=true
  ...
  CIRCLE_WORKING_DIRECTORY=~/project

====>> Checkout code
  #!/bin/bash -eo pipefail
mkdir -p /home/circleci/project && cd /tmp/_circleci_local_build_repo && git ls-files | tar -T - -c | tar -x -C /home/circleci/project && cp -a /tmp/_circleci_local_build_repo/.git /home/circleci/project
====>> python3.6 -m venv venv
source ./venv/bin/activate
pip install pytest
pytest test_calculations.py

  #!/bin/bash -eo pipefail
python3.6 -m venv venv
source ./venv/bin/activate
pip install pytest
pytest test_calculations.py

Collecting pytest
  ...
Installing collected packages: more-itertools, zipp, importlib-metadata, pluggy, pyparsing, six, packaging, wcwidth, py, attrs, atomicwrites, pytest
Successfully installed atomicwrites-1.3.0 attrs-19.1.0 importlib-metadata-0.18 more-itertools-7.2.0 packaging-19.0 pluggy-0.12.0 py-1.8.0 pyparsing-2.4.0 pytest-5.0.1 six-1.12.0 wcwidth-0.1.7 zipp-0.5.2
You are using pip version 18.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/circleci/project
collected 2 items
test_calculations.py ..                                                  [100%]

=========================== 2 passed in 0.03 seconds ===========================
Success!

けっこう時間がかかった
dockerイメージのダウンロードが長かった
もう一度やるとどうかな
だいぶ速い

これなら毎回pushしないでいいしいいな
しょーもないミスの入ったファイルをpushしなくてすむのは気が楽