正式なドキュメントは英語版であり、この日本語訳はAI支援翻訳により作成された参考用のものです。日本語訳の一部の内容は人間によるレビューがまだ行われていないため、翻訳のタイミングにより英語版との間に差異が生じることがあります。最新かつ正確な情報については、英語版をご参照ください。

環境API

  • プラン: Free、Premium、Ultimate
  • 提供形態: GitLab.com、GitLab Self-Managed、GitLab Dedicated

このAPIを使用してGitLab環境を操作します。

環境の一覧表示

特定のプロジェクトのすべての環境を取得します。

GET /projects/:id/environments
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス。
name文字列いいえこの名前の環境を返します。searchと相互に排他的です。
search文字列いいえ検索条件に一致する環境のリストを返します。nameと相互に排他的です。3文字以上にする必要があります。
states文字列いいえ特定のステータスに一致するすべての環境をリストします。指定できる値は、availablestoppingstoppedです。ステータス値が指定されていない場合、すべての環境を返します。
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments?name=review%2Ffix-foo"

レスポンス例:

[
  {
    "id": 1,
    "name": "review/fix-foo",
    "slug": "review-fix-foo-dfjre3",
    "description": "This is review environment",
    "external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
    "state": "available",
    "tier": "development",
    "created_at": "2019-05-25T18:55:13.252Z",
    "updated_at": "2019-05-27T18:55:13.252Z",
    "enable_advanced_logs_querying": false,
    "logs_api_path": "/project/-/logs/k8s.json?environment_name=review%2Ffix-foo",
    "auto_stop_at": "2019-06-03T18:55:13.252Z",
    "kubernetes_namespace": "flux-system",
    "flux_resource_path": "HelmRelease/flux-system",
    "auto_stop_setting": "always"
  }
]

特定の環境を取得

GET /projects/:id/environments/:environment_id
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
environment_id整数はい環境のID。
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1"

レスポンス例

{
  "id": 1,
  "name": "review/fix-foo",
  "slug": "review-fix-foo-dfjre3",
  "description": "This is review environment",
  "external_url": "https://review-fix-foo-dfjre3.gitlab.example.com",
  "state": "available",
  "tier": "development",
  "created_at": "2019-05-25T18:55:13.252Z",
  "updated_at": "2019-05-27T18:55:13.252Z",
  "enable_advanced_logs_querying": false,
  "logs_api_path": "/project/-/logs/k8s.json?environment_name=review%2Ffix-foo",
  "auto_stop_at": "2019-06-03T18:55:13.252Z",
  "last_deployment": {
    "id": 100,
    "iid": 34,
    "ref": "fdroid",
    "sha": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
    "created_at": "2019-03-25T18:55:13.252Z",
    "status": "success",
    "user": {
      "id": 1,
      "name": "Administrator",
      "state": "active",
      "username": "root",
      "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
      "web_url": "http://localhost:3000/root"
    },
    "deployable": {
      "id": 710,
      "status": "success",
      "stage": "deploy",
      "name": "staging",
      "ref": "fdroid",
      "tag": false,
      "coverage": null,
      "created_at": "2019-03-25T18:55:13.215Z",
      "started_at": "2019-03-25T12:54:50.082Z",
      "finished_at": "2019-03-25T18:55:13.216Z",
      "duration": 21623.13423,
      "project": {
        "ci_job_token_scope_enabled": false
      },
      "user": {
        "id": 1,
        "name": "Administrator",
        "username": "root",
        "state": "active",
        "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
        "web_url": "http://gitlab.dev/root",
        "created_at": "2015-12-21T13:14:24.077Z",
        "bio": null,
        "location": null,
        "public_email": "",
        "linkedin": "",
        "twitter": "",
        "website_url": "",
        "organization": null
      },
      "commit": {
        "id": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
        "short_id": "416d8ea1",
        "created_at": "2016-01-02T15:39:18.000Z",
        "parent_ids": [
          "e9a4449c95c64358840902508fc827f1a2eab7df"
        ],
        "title": "Removed fabric to fix #40",
        "message": "Removed fabric to fix #40\n",
        "author_name": "Administrator",
        "author_email": "admin@example.com",
        "authored_date": "2016-01-02T15:39:18.000Z",
        "committer_name": "Administrator",
        "committer_email": "admin@example.com",
        "committed_date": "2016-01-02T15:39:18.000Z"
      },
      "pipeline": {
        "id": 34,
        "sha": "416d8ea11849050d3d1f5104cf8cf51053e790ab",
        "ref": "fdroid",
        "status": "success",
        "web_url": "http://localhost:3000/Commit451/lab-coat/pipelines/34"
      },
      "web_url": "http://localhost:3000/Commit451/lab-coat/-/jobs/710",
      "artifacts": [
        {
          "file_type": "trace",
          "size": 1305,
          "filename": "job.log",
          "file_format": null
        }
      ],
      "runner": null,
      "artifacts_expire_at": null
    }
  },
  "cluster_agent": {
    "id": 1,
    "name": "agent-1",
    "config_project": {
      "id": 20,
      "description": "",
      "name": "test",
      "name_with_namespace": "Administrator / test",
      "path": "test",
      "path_with_namespace": "root/test",
      "created_at": "2022-03-20T20:42:40.221Z"
    },
    "created_at": "2022-04-20T20:42:40.221Z",
    "created_by_user_id": 42
  },
  "kubernetes_namespace": "flux-system",
  "flux_resource_path": "HelmRelease/flux-system",
  "auto_stop_setting": "always"
}

新しい環境を作成

指定された名前とexternal_urlで新しい環境を作成します。

環境が正常に作成された場合は201、パラメータが間違っている場合は400を返します。

POST /projects/:id/environments
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
name文字列はい環境の名前。
description文字列いいえ環境の説明。
external_url文字列いいえこの環境にリンクする場所。
tier文字列いいえ新しい環境の階層。使用できる値は、productionstagingtestingdevelopmentotherです。
cluster_agent_id整数いいえこの環境に関連付けるクラスタリングエージェント。
kubernetes_namespace文字列いいえこの環境に関連付けるKubernetesネームスペース。
flux_resource_path文字列いいえこの環境に関連付けるFluxリソースパス。これはリソースのフルパスでなければなりません。たとえばhelm.toolkit.fluxcd.io/v2/namespaces/gitlab-agent/helmreleases/gitlab-agentなどです。
auto_stop_setting文字列いいえ環境の自動停止設定。使用できる値は、alwaysまたはwith_actionです。
curl --data "name=deploy&external_url=https://deploy.gitlab.example.com" \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments"

レスポンス例:

{
  "id": 1,
  "name": "deploy",
  "slug": "deploy",
  "description": null,
  "external_url": "https://deploy.gitlab.example.com",
  "state": "available",
  "tier": "production",
  "created_at": "2019-05-25T18:55:13.252Z",
  "updated_at": "2019-05-27T18:55:13.252Z",
  "kubernetes_namespace": "flux-system",
  "flux_resource_path": "HelmRelease/flux-system",
  "auto_stop_setting": "always"
}

既存の環境を更新

既存の環境の名前またはexternal_urlを更新します。

環境が正常に更新された場合は200を返します。エラーが発生した場合、ステータスコード400が返されます。

PUT /projects/:id/environments/:environments_id
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
environment_id整数はい環境のID。
description文字列いいえ環境の説明。
external_url文字列いいえ新しいexternal_url
tier文字列いいえ新しい環境の階層。使用できる値は、productionstagingtestingdevelopmentotherです。
cluster_agent_id整数またはnullいいえこの環境に関連付けるクラスタリングエージェント、または削除する場合はnull
kubernetes_namespace文字列またはnullいいえこの環境に関連付けるKubernetesネームスペース、または削除する場合はnull
flux_resource_path文字列またはnullいいえこの環境に関連付けるFluxリソースパス、または削除する場合はnull
auto_stop_setting文字列またはnullいいえ環境の自動停止設定。使用できる値は、alwaysまたはwith_actionです。
curl --request PUT \
  --data "external_url=https://staging.gitlab.example.com" \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1"

レスポンス例:

{
  "id": 1,
  "name": "staging",
  "slug": "staging",
  "description": null,
  "external_url": "https://staging.gitlab.example.com",
  "state": "available",
  "tier": "staging",
  "created_at": "2019-05-25T18:55:13.252Z",
  "updated_at": "2019-05-27T18:55:13.252Z",
  "kubernetes_namespace": "flux-system",
  "flux_resource_path": "HelmRelease/flux-system",
  "auto_stop_setting": "always"
}

環境を削除する

環境が正常に削除された場合は204、環境が存在しない場合は404を返します。環境は最初に停止する必要があります。そうしない場合、リクエストは403を返します。

DELETE /projects/:id/environments/:environment_id
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
environment_id整数はい環境のID。
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1"

複数の停止済みレビューアプリを削除

すでに停止されている複数の環境と、レビューアプリフォルダー内にある複数の環境の削除をスケジュールします。実際の削除は、実行時から1週間後に行われます。デフォルトでは、30日以上前の環境のみが削除されます。このデフォルトを変更するには、beforeパラメータを使用します。

DELETE /projects/:id/environments/review_apps
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
before日時いいえ環境を削除できる日付(この日付より前の環境)。デフォルトでは、30日前になります。ISO 8601形式(YYYY-MM-DDTHH:MM:SSZ)で指定します。
limit整数いいえ削除する環境の最大数。デフォルトは100です。
dry_runブール値いいえ安全上の理由から、デフォルトはtrueです。実際には削除が実行されないドライランを実行します。環境を実際に削除するには、falseに設定します。
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/review_apps"

レスポンス例:

{
  "scheduled_entries": [
    {
      "id": 387,
      "name": "review/023f1bce01229c686a73",
      "slug": "review-023f1bce01-3uxznk",
      "external_url": null
    },
    {
      "id": 388,
      "name": "review/85d4c26a388348d3c4c0",
      "slug": "review-85d4c26a38-5giw1c",
      "external_url": null
    }
  ],
  "unprocessable_entries": []
}

環境を停止

環境が正常に停止された場合は200、環境が存在しない場合は404を返します。

POST /projects/:id/environments/:environment_id/stop
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
environment_id整数はい環境のID。
forceブール値いいえon_stopアクションを実行せずに環境を強制的に停止させます。
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1/stop"

レスポンス例:

{
  "id": 1,
  "name": "deploy",
  "slug": "deploy",
  "external_url": "https://deploy.gitlab.example.com",
  "state": "stopped",
  "created_at": "2019-05-25T18:55:13.252Z",
  "updated_at": "2019-05-27T18:55:13.252Z",
  "kubernetes_namespace": "flux-system",
  "flux_resource_path": "HelmRelease/flux-system",
  "auto_stop_setting": "always"
}

失効した環境を停止

指定された日付より前に最後に変更またはデプロイされたすべての環境に停止リクエストを発行します。保護環境は除外されます。停止リクエストが成功した場合は200、指定日より前の日付が無効な場合は400を返します。環境が正確にいつ停止されるかの詳細については、環境を停止を参照してください。

POST /projects/:id/environments/stop_stale
属性必須説明
id整数または文字列はいプロジェクトのIDまたはURLエンコードされたパス
before日付はい指定された日付より前に変更またはデプロイされた環境を停止します。ISO 8601形式(2019-03-15T08:00:00Z)で指定します。有効な入力は、10年前から1週間前までです
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/stop_stale?before=10%2F10%2F2021"

レスポンス例:

{
  "message": "Successfully requested stop for all stale environments"
}