Environments API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

Parameter auto_stop_setting added in GitLab 17.8. Support for GitLab CI/CD job token authentication introduced in GitLab 16.2.

List environments

Get all environments for a given project.

Copy to clipboard
GET /projects/:id/environments
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
namestringnoReturn the environment with this name. Mutually exclusive with search.
searchstringnoReturn list of environments matching the search criteria. Mutually exclusive with name. Must be at least 3 characters long.
statesstringnoList all environments that match a specific state. Accepted values: available, stopping, or stopped. If no state value given, returns all environments.
Copy to clipboard
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments?name=review%2Ffix-foo"

Example response:

Copy to clipboard
[
  {
    "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 a specific environment

Copy to clipboard
GET /projects/:id/environments/:environment_id
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
environment_idintegeryesThe ID of the environment.
Copy to clipboard
curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1"

Example of response

Copy to clipboard
{
  "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": "",
        "skype": "",
        "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"
}

Create a new environment

Creates a new environment with the given name and external_url.

It returns 201 if the environment was successfully created, 400 for wrong parameters.

Copy to clipboard
POST /projects/:id/environments
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
namestringyesThe name of the environment.
descriptionstringnoThe description of the environment.
external_urlstringnoPlace to link to for this environment.
tierstringnoThe tier of the new environment. Allowed values are production, staging, testing, development, and other.
cluster_agent_idintegernoThe cluster agent to associate with this environment.
kubernetes_namespacestringnoThe Kubernetes namespace to associate with this environment.
flux_resource_pathstringnoThe Flux resource path to associate with this environment. This must be the full resource path. For example, helm.toolkit.fluxcd.io/v2/namespaces/gitlab-agent/helmreleases/gitlab-agent.
auto_stop_settingstringnoThe auto stop setting for the environment. Allowed values are always or with_action.
Copy to clipboard
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"

Example response:

Copy to clipboard
{
  "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"
}

Update an existing environment

History

Updates an existing environment’s name and/or external_url.

It returns 200 if the environment was successfully updated. In case of an error, a status code 400 is returned.

Copy to clipboard
PUT /projects/:id/environments/:environments_id
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
environment_idintegeryesThe ID of the environment.
descriptionstringnoThe description of the environment.
external_urlstringnoThe new external_url.
tierstringnoThe tier of the new environment. Allowed values are production, staging, testing, development, and other.
cluster_agent_idinteger or nullnoThe cluster agent to associate with this environment or null to remove it.
kubernetes_namespacestring or nullnoThe Kubernetes namespace to associate with this environment or null to remove it.
flux_resource_pathstring or nullnoThe Flux resource path to associate with this environment or null to remove it.
auto_stop_settingstring or nullnoThe auto stop setting for the environment. Allowed values are always or with_action.
Copy to clipboard
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"

Example response:

Copy to clipboard
{
  "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"
}

Delete an environment

It returns 204 if the environment was successfully deleted, and 404 if the environment does not exist. The environment must be stopped first, otherwise the request returns 403.

Copy to clipboard
DELETE /projects/:id/environments/:environment_id
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
environment_idintegeryesThe ID of the environment.
Copy to clipboard
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1"

Delete multiple stopped review apps

It schedules for deletion multiple environments that have already been stopped and are in the review app folder. The actual deletion is performed after 1 week from the time of execution. By default, it only deletes environments 30 days or older. You can change this default using the before parameter.

Copy to clipboard
DELETE /projects/:id/environments/review_apps
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
beforedatetimenoThe date before which environments can be deleted. Defaults to 30 days ago. Expected in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ).
limitintegernoMaximum number of environments to delete. Defaults to 100.
dry_runbooleannoDefaults to true for safety reasons. It performs a dry run where no actual deletion is performed. Set to false to actually delete the environment.
Copy to clipboard
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/review_apps"

Example response:

Copy to clipboard
{
  "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": []
}

Stop an environment

It returns 200 if the environment was successfully stopped, and 404 if the environment does not exist.

Copy to clipboard
POST /projects/:id/environments/:environment_id/stop
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
environment_idintegeryesThe ID of the environment.
forcebooleannoForce environment to stop without executing on_stop actions.
Copy to clipboard
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/1/environments/1/stop"

Example response:

Copy to clipboard
{
  "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"
}

Stop stale environments

Issue stop request to all environments that were last modified or deployed to before a specified date. Excludes protected environments. Returns 200 if stop request was successful and 400 if the before date is invalid. For details of exactly when the environment is stopped, see Stop an environment.

Copy to clipboard
POST /projects/:id/environments/stop_stale
AttributeTypeRequiredDescription
idinteger/stringyesThe ID or URL-encoded path of the project.
beforedateyesStop environments that have been modified or deployed to before the specified date. Expected in ISO 8601 format (2019-03-15T08:00:00Z). Valid inputs are between 10 years ago and 1 week ago
Copy to clipboard
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"

Example response:

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