2022年4月19日火曜日

CI/CD devkit の Dagger を試す(Hello, World から Ruby プロジェクトのテストまで)

動作確認用コンテナ起動

docker run -it --rm -v "/var/run/docker.sock:/var/run/docker.sock" debian:bullseye-slim

環境構築

必須ツール(Docker)のインストール

apt update
apt install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

curl -fsSL https://download.docker.com/linux/debian/gpg \
    | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
    $(lsb_release -cs) stable" |  tee /etc/apt/sources.list.d/docker.list > /dev/null

apt update
apt install -y docker-ce docker-ce-cli containerd.io

Dagger インストール

mkdir -p /opt/dagger
cd /opt/dagger
curl -L https://dl.dagger.io/dagger/install.sh | sh

export PATH=$PATH:/opt/dagger/bin
echo "export PATH=$PATH:/opt/dagger/bin" >> ~/.bashrc

動作確認

Getting Started のサンプルを実行

詳細は CI/CD in your local dev を参照のこと。

apt install -y git

cd ~
git clone https://github.com/dagger/dagger
cd dagger
git checkout v0.2.4

cd pkg/universe.dagger.io/examples/todoapp
dagger do build

_build に成果物が生成される。

ゼロからプロジェクトを作る

dagger プロジェクトの初期化

dagger project init

プラン作成

firststep.cue を作成する。

`echo “hello, world!” を実行するプラン(とアクション)を作成。

package main

import (
    "dagger.io/dagger"
    "dagger.io/dagger/core"
)

dagger.#Plan & {
    actions: {
        _dockerImage: core.#Pull & {source: "alpine:3"}
        hello: core.#Exec & {
            input: _dockerImage.output
            args: ["echo", "hello, world!"]
            always: true
        }
    }
}

プロジェクトアップデート

これで利用するモジュールの依存関係を解決するらしい。

dagger project update

プラン実行

root@f32697b02b38:/work/firststep# dagger do hello --log-format=plain
11:10AM INF actions._dockerImage | computing
11:10AM INF actions._dockerImage | completed    duration=1.9s
11:10AM INF actions.hello | computing
11:10AM INF actions.hello | completed    duration=200ms
11:10AM INF actions.hello | #3 0.153 hello, world!

hello, world! が表示されている、 OK.

既存 Ruby プロジェクトのテストをやってみる

rubyTest.cue を作成。

package main

import (
    "dagger.io/dagger"
    "dagger.io/dagger/core"
)

dagger.#Plan & {

    actions: {
        // ruby コンテナを利用するので Pull
        _dockerImage: core.#Pull & {source: "ruby:2"}

        // Git からコードを取得
        fetch: core.#GitPull & {
            remote: "https://github.com/mikoto2000/binp.git"
            ref: "master"
        }
        _appSource: fetch.output

        // Docker 用マウントオプションを定義
        _sourceMounts: {
            "app source": {
                type: "fs"
                dest: _workDir
                contents: _appSource
            }
        }

        // Docker コンテナを使って依存解決とテスト
        _workDir: "/src"
        dep: core.#Exec & {
            // Pull したコンテナのファイルシステムを使う
            input: _dockerImage.output
            // ソースコードをマウント
            mounts: _sourceMounts
            // マウントした場所をワークディレクトリとする
            workdir: _workDir
            // bundle を使って依存 gem を取得
            args: ["bundle", "install"]
            // 毎回実行する
            always: true
        }
        build: core.#Exec & {
            // dep の実行結果のファイルシステムを継続して使う
            input: dep.output
            // ソースコードをマウント
            mounts: _sourceMounts
            // マウントした場所をワークディレクトリとする
            workdir: _workDir
            // bundle を使ってテスト実行
            args: ["bundle", "exec", "rake", "test"]
            // 毎回実行する
            always: true
        }
    }
}

これを実行すると、以下のようになる。(プロジェクト初期化・更新は省略)

root@f32697b02b38:/work/maven# dagger do build --log-format=plain
1:11PM INF actions.fetch | computing
1:11PM INF actions._dockerImage | computing
1:11PM INF actions.fetch | completed    duration=900ms
1:11PM INF actions.fetch | #2 0.877 76f1e57a51f9f1575803d01e46a4f4a9d718e8ff    refs/heads/master
1:11PM INF actions._dockerImage | completed    duration=3.7s
1:11PM INF actions.dep | computing
1:11PM INF actions.dep | #4 0.296 Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
1:11PM INF actions.dep | #4 0.296 installing your bundle as root will break this application for all non-root
1:11PM INF actions.dep | #4 0.296 users on this machine.
1:11PM INF actions.dep | #4 0.431 fatal: not a git repository (or any parent up to mount point /)
1:11PM INF actions.dep | #4 0.431 Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
1:11PM INF actions.dep | #4 3.547 Fetching gem metadata from https://rubygems.org/
1:11PM INF actions.dep | .
1:11PM INF actions.dep | .
1:11PM INF actions.dep | ..
1:11PM INF actions.dep | .
1:11PM INF actions.dep | .
1:11PM INF actions.dep | .
1:11PM INF actions.dep | #4 5.015 Fetching rake 12.3.3
1:11PM INF actions.dep | #4 6.476 Installing rake 12.3.3
1:11PM INF actions.dep | #4 6.512 Fetching formatador 0.2.5
1:11PM INF actions.dep | #4 7.687 Installing formatador 0.2.5
1:11PM INF actions.dep | #4 7.700 Using binp 4.0.0 from source at `.`
1:11PM INF actions.dep | #4 7.701 Using bundler 2.1.4
1:11PM INF actions.dep | #4 7.702 Fetching docile 1.3.2
1:11PM INF actions.dep | #4 7.909 Installing docile 1.3.2
1:11PM INF actions.dep | #4 7.925 Fetching power_assert 1.2.0
1:11PM INF actions.dep | #4 8.120 Installing power_assert 1.2.0
1:11PM INF actions.dep | #4 8.138 Fetching simplecov-html 0.12.2
1:11PM INF actions.dep | #4 8.513 Installing simplecov-html 0.12.2
1:11PM INF actions.dep | #4 8.558 Fetching simplecov 0.18.5
1:11PM INF actions.dep | #4 8.836 Installing simplecov 0.18.5
1:11PM INF actions.dep | #4 8.864 Fetching test-unit 3.3.6
1:11PM INF actions.dep | #4 9.320 Installing test-unit 3.3.6
1:11PM INF actions.dep | #4 9.393 Bundle complete! 4 Gemfile dependencies, 9 gems now installed.
1:11PM INF actions.dep | #4 9.393 Use `bundle info [gemname]` to see where a bundled gem is installed.
1:11PM INF actions.dep | completed    duration=9.5s
1:11PM INF actions.build | computing
1:11PM INF actions.build | #5 0.307 fatal: not a git repository (or any parent up to mount point /)
1:11PM INF actions.build | #5 0.307 Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
1:11PM INF actions.build | #5 0.537 fatal: not a git repository (or any parent up to mount point /)
1:11PM INF actions.build | #5 0.537 Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
1:11PM INF actions.build | #5 0.731 /src/lib/binp/cli/main.rb:51: warning: assigned but unused variable - e
1:11PM INF actions.build | #5 0.763 Loaded suite /usr/local/lib/ruby/gems/2.7.0/gems/rake-12.3.3/lib/rake/rake_test_loader
1:11PM INF actions.build | #5 0.763 Started
1:11PM INF actions.build | #5 0.763 .....................
1:11PM INF actions.build | #5 0.768 Finished in 0.005315008 seconds.
1:11PM INF actions.build | #5 0.768 -------------------------------------------------------------------------------
1:11PM INF actions.build | #5 0.768 21 tests, 29 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
1:11PM INF actions.build | #5 0.768 100% passed
1:11PM INF actions.build | #5 0.768 -------------------------------------------------------------------------------
1:11PM INF actions.build | #5 0.768 3951.08 tests/s, 5456.25 assertions/s
1:11PM INF actions.build | #5 0.789 Coverage report generated for Unit Tests to /src/coverage. 78 / 120 LOC (65.0%) covered.
1:11PM INF actions.build | completed    duration=900ms

OK っぽい。

0 件のコメント:

コメントを投稿