IBM Cloud Docs
パイプラインへのテスト結果およびビルド・スクリプトの追加

パイプラインへのテスト結果およびビルド・スクリプトの追加

新規または既存のテスト・スクリプトおよびビルド・スクリプトの結果を DevSecOps パイプラインの継続的統合フローに追加することで、既存のテストおよびビルド・フローを継続的統合パイプラインに接続します。

継続的統合パイプライン内の以下のステージを使用して、テスト・ステップとビルド・ステップを追加できます。

  • セットアップ
  • 評価
  • Containerize (ビルド)
  • リリース

パイプライン・セットアップ

「セットアップ」 ステージを使用して、テストおよびビルド環境をセットアップし、情報をパイプラインにプルします。 例えば、アプリ関連の複数のリポジトリーを単一のビルドで使用できます。 必要なすべてのリポジトリーを複製し、コンプライアンス関連のチェックおよびスキャンでそれらのリポジトリーをパイプラインに認識させることができます。

パイプラインによって内部的に複製され、参照名 app-reposave_repo pipelinectl インターフェースを使用してパイプラインに追加されるデフォルトのアプリ・リポジトリー。 デフォルトのリポジトリーは、リポジトリーのパイプライン UI パラメーターで指定されるか、ツールチェーンのバインディング名で選択されます (ツールチェーン・テンプレートからパイプラインをセットアップした場合)。

さらに多くのリポジトリーを使用する場合は、それらを setup ステージで複製し、同じ save_repo インターフェースを使用してそれらをパイプラインに追加します。

#
# your scripts cloning the repositories
#
# make sure you prepare or export the following data from each cloned repository:
# - repository URL
# - path where it was cloned, relative to the $WORKSPACE path
# - cloned branch
# - latest commit hash
#
your_clone_scripts

#
# when cloning is complete
# use `save_repo` to add these information to the pipeline
# repo-reference-name can be any name, it is used to refer to the stored repo
#
save_repo <repo-reference-name> \
    url="${REPO_URL}" \
    path="${REPO_PATH}" \
    branch="${CLONED_BRANCH}" \
    commit="${LATEST_GIT_COMMIT}"

このようにして、パイプラインの残りの部分で、これらのリポジトリーをスキャンしてコンプライアンス違反や脆弱性を検出できます。

save_repo を使用して保存されるパスは、ワークスペース・パスに対する相対パスでなければなりません。

スクリプトや基本イメージ用に pipelinectl ツールをインストールする必要はありません。スクリプトのコンテキストに応じて参照用パイプラインがバイナリーを提供します。

テスト

このステージでは、コード・リポジトリーに対してテストを実行します。 セットアップ・ステージで追加されたリポジトリーには、 list_repos インターフェースおよび load_repo pipelinectl インターフェースを使用してアクセスできます。

exit_code=0

#
# `list_repos` returns the list of the reference names of saved repos
#
list_repos | while IFS= read -r repository ; do

    #
    # load_repo returns a property of a saved repository
    #
    # Usage:
    # load_repo <repo-reference-name> <property>
    #
    url="$(load_repo "$repository" url)"
    sha="$(load_repo "$repository" commit)"
    branch="$(load_repo "$repository" branch)"
    path="$(load_repo "$repository" path)"

    #
    # use your repos to test, etc
    #
    run_tests
    result=$?

    if [ $result != 0 ]; then
        exit_code=$result
    fi
done

exit $exit_code

単体テスト・コンプライアンス制御は、ステージ・スクリプトの終了コードに基づいています。 テストが成功した場合は、 0 で終了します。 そうでない場合は、終了時にゼロ以外の終了コードを返します。

結果の保存

テストでは、JSON または XML でのテスト結果など、いくつかのレポート成果物が生成される場合があります。 このステージの save_result pipelinectl インターフェースを使用して、作成されたコンプライアンス証拠に証拠成果物としてテストを添付します。

#
# run tests with some test suite runner, and save output to results.json
#
test_runner -o results.json

#
# save the result for the pipeline, so it can attach it to the unit test evidence
#
save_result test results.json

save_results の最初のパラメーターは、DevSecOps パイプライン構成のステージ名 (test、scan-artifact、acceptance-test など) でなければなりません。 そうしないと、エビデンス・コレクターはそれを見つけて、適切なエビデンスに添付することができません。

save_result pipelinectl インターフェースを使用すると、パイプラインが結果成果物を検出し、それらの成果物がエビデンス・ロッカーにアップロードされ、パイプラインによって作成されたコンプライアンスの証拠に添付されます。

save_result を使用するときに、単体テストで作成されるエビデンスの例を以下に示します。

{
  "evidence_type_id": "com.ibm.unit_tests",
  "evidence_type_version": "1.0.0",
  "date": "2021-03-31T07:41:31.881Z",
  "result": "success",
  "pipeline_id": "8c2b6750-91db-45fb-98ee-51684843b821",
  "pipeline_run_id": "89a04de9-2795-4e8e-be90-52a92ac7f9c1",
  "issues": [],
  "artifacts": [
    {
      "url": "https://s3.us-south.cloud-object-storage.appdomain.cloud/cos-bucket-name/ci/89a04de9-2795-4e8e-be90-52a92ac7f9c1/artifacts/compliance-app-COMPACT-20210218231513608/unit-tests-results.json_d9619521e7444fef0ff052e59fd54049",
      "hash": "d9619521e7444fef0ff052e59fd54049"
    }
  ],
  "toolchain_crn": "crn:v1:bluemix:public:toolchain:us-south:a/40111714589c4f7099032529b26a7a63:39d4f080-55e5-42ee-a787-26d936fb2b97::",
  "log": [
    {
      "url": "https://cloud.ibm.com/devops/pipelines/tekton/8c2b6750-91db-45fb-98ee-51684843b821/runs/89a04de9-2795-4e8e-be90-52a92ac7f9c1/code-unit-tests/run-stage?env_id=ibm:yp:us-south",
      "hash": null
    },
    {
      "url": "https://s3.us-south.cloud-object-storage.appdomain.cloud/cos-bucket-name/ci/89a04de9-2795-4e8e-be90-52a92ac7f9c1/artifacts/logs/code-unit-tests/run-stage.log_dae902fb1455b1fc9c565273aa4fe1bc",
      "hash": "dae902fb1455b1fc9c565273aa4fe1bc"
    }
  ]
}

ビルドまたはコンテナー化

このステージでは、成果物をビルドできます。 パイプラインには Docker イメージ・タイプの成果物用のデフォルト・フィーチャーがいくつか用意されていますが、ここで任意の成果物をビルドすることができます。 後でパイプラインでスキャンを実行したり、リリース・ステージで成果物を使用したりできるように、パイプライン用に作成された成果物を保存します。

ビルドされた成果物に関する情報を提供するには、pipelinectl の save_artifact インターフェースを使用します。

#
# your scripts building the artifact
#
# make sure you prepare or export the following data from each built artifact:
# - type (image for docker images, package for rpms, npm tarballs, etc )
# - full artifact URL with version tag
# - artifact digest
#
your_build_scripts

#
# when the build is complete
# use `save_artifact` to add these information to the pipeline
# artifact-reference-name can be any name, it is used to refer to the stored artifact
#
save_artifact <artifact-reference-name> \
    type=image" \
    name="${IMAGE_URL}" \
    digest="${IMAGE_DIGEST}"

推奨されるイメージ名の形式は image-URL:build-tag です。例えば、wcp-compliance-automation-team-docker-local.artifactory.swg-devops.com/compliance-baseimage:2.8.0 のように指定します。 {: important]

Docker イメージをビルドする場合は、 save_artifact インターフェースを使用して、デフォルトの組み込みイメージ署名および CR IBM Informix 仮想アプライアンス・スキャン・タスク用にそれらのイメージを送信します。

リリース

パイプラインの最後では、ビルドされた成果物をインベントリーに追加して、デプロイメントにプロモートできるようにする必要があります。 Helm チャートなどの他の成果物もインベントリーに追加する場合は、リリース・ステージで柔軟に対応できます。

このステージでは、CLI の cocoa inventory add コマンドと、pipelinectl コマンドからのデータを使用して、インベントリーのエントリーを作成できます。

パイプラインの実行に問題がある場合は、問題のあるインベントリーを回避するために、インベントリーの更新をスキップすることを選択できます。 インベントリーの更新をスキップするには、以下の環境変数を使用します。

  • skip-inventory-update-on-failure パイプラインから環境変数をオプトインして、在庫を更新するかどうかを指定します。
  • パイプライン実行でステージ障害が発生した場合は、 one-pipeline-status 1 に設定します。

このステージで cocoa inventory add を呼び出す前に、これらの変数を確認してください。

# Check the status of pipeline and then release the artifacts to inventory

ONE_PIPELINE_STATUS=$(get_env one-pipeline-status 0)
if [ -n "$(get_env skip-inventory-update-on-failure "")" ]; then
    if [ $ONE_PIPELINE_STATUS -eq 1 ]; then
          echo "Skipping release stage as some of the pipeline stages are not successful."
          exit 1
    fi
fi

#
# `list_artifacts` returns the list of the reference names of saved artifacts
#
list_artifacts | while IFS= read -r artifact ; do
    #
    # Add a new value to the inventory repository. `cocoa inventory add` creates a new file with the name option,
    # if does not exist otherwise overwrites it.
    #
    cocoa inventory add \
        --name="${artifact}" \
        --artifact="$(load_artifact $artifact name)" \
        --repository-url="$(load_repo app-repo url)" \
        --commit-sha="$(load_repo app-repo commit)" \
        --build-number="${BUILD_NUMBER}" \
        --pipeline-run-id="${PIPELINE_RUN_ID}" \
        --version="$(get_env version)" \
        --app-artifacts="{ \
            \"signature\": \"$(load_artifact $artifact signature)\", \
            \"provenance\": \"$(load_artifact $artifact name)\"\
        }"
done

CLI を使用するには、スクリプトにインストールするか、CLI がプリインストールされている基本イメージを使用する必要があります。