project_level_variables.md 4.8 KB
Newer Older
1
# Project-level Variables API
T
Tomasz Maczukin 已提交
2 3 4

## List project variables

S
Shinya Maeda 已提交
5
Get list of a project's variables.
T
Tomasz Maczukin 已提交
6 7 8 9 10

```
GET /projects/:id/variables
```

11 12
| Attribute | Type    | required | Description         |
|-----------|---------|----------|---------------------|
13
| `id`      | integer/string | yes      | The ID of a project or [urlencoded NAMESPACE/PROJECT_NAME of the project](README.md#namespaced-path-encoding) owned by the authenticated user |
14 15

```
16
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables"
17
```
T
Tomasz Maczukin 已提交
18 19 20 21 22

```json
[
    {
        "key": "TEST_VARIABLE_1",
23
        "variable_type": "env_var",
T
Tomasz Maczukin 已提交
24 25 26 27
        "value": "TEST_1"
    },
    {
        "key": "TEST_VARIABLE_2",
28
        "variable_type": "env_var",
T
Tomasz Maczukin 已提交
29 30 31 32 33 34 35
        "value": "TEST_2"
    }
]
```

## Show variable details

S
Shinya Maeda 已提交
36
Get the details of a project's specific variable.
T
Tomasz Maczukin 已提交
37 38 39 40 41

```
GET /projects/:id/variables/:key
```

42 43
| Attribute | Type    | required | Description           |
|-----------|---------|----------|-----------------------|
44
| `id`      | integer/string | yes      | The ID of a project or [urlencoded NAMESPACE/PROJECT_NAME of the project](README.md#namespaced-path-encoding) owned by the authenticated user   |
45
| `key`     | string  | yes      | The `key` of a variable |
46 47

```
48
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/TEST_VARIABLE_1"
49 50
```

T
Tomasz Maczukin 已提交
51 52 53
```json
{
    "key": "TEST_VARIABLE_1",
54
    "variable_type": "env_var",
55 56 57
    "value": "TEST_1",
    "protected": false,
    "masked": true
T
Tomasz Maczukin 已提交
58 59 60 61 62
}
```

## Create variable

S
Shinya Maeda 已提交
63
Create a new variable.
T
Tomasz Maczukin 已提交
64 65 66 67 68

```
POST /projects/:id/variables
```

69 70 71 72 73 74 75 76 77
| Attribute           | Type    | required | Description           |
|---------------------|---------|----------|-----------------------|
| `id`                | integer/string | yes      | The ID of a project or [urlencoded NAMESPACE/PROJECT_NAME of the project](README.md#namespaced-path-encoding) owned by the authenticated user   |
| `key`               | string  | yes      | The `key` of a variable; must have no more than 255 characters; only `A-Z`, `a-z`, `0-9`, and `_` are allowed |
| `value`             | string  | yes      | The `value` of a variable |
| `variable_type`     | string  | no       | The type of a variable. Available types are: `env_var` (default) and `file` |
| `protected`         | boolean | no       | Whether the variable is protected |
| `masked`            | boolean | no       | Whether the variable is masked |
| `environment_scope` | string  | no       | The `environment_scope` of the variable **[PREMIUM]** |
78 79

```
80
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"
81 82
```

T
Tomasz Maczukin 已提交
83 84 85
```json
{
    "key": "NEW_VARIABLE",
86
    "value": "new value",
87
    "protected": false,
88
    "variable_type": "env_var",
89
    "protected": false,
90 91
    "masked": false,
    "environment_scope": "*"
T
Tomasz Maczukin 已提交
92 93 94 95 96
}
```

## Update variable

S
Shinya Maeda 已提交
97
Update a project's variable.
T
Tomasz Maczukin 已提交
98 99 100 101 102

```
PUT /projects/:id/variables/:key
```

103 104 105 106 107 108 109 110 111
| Attribute           | Type    | required | Description             |
|---------------------|---------|----------|-------------------------|
| `id`                | integer/string | yes      | The ID of a project or [urlencoded NAMESPACE/PROJECT_NAME of the project](README.md#namespaced-path-encoding) owned by the authenticated user     |
| `key`               | string  | yes      | The `key` of a variable   |
| `value`             | string  | yes      | The `value` of a variable |
| `variable_type`     | string  | no       | The type of a variable. Available types are: `env_var` (default) and `file` |
| `protected`         | boolean | no       | Whether the variable is protected |
| `masked`            | boolean | no       | Whether the variable is masked |
| `environment_scope` | string  | no       | The `environment_scope` of the variable **[PREMIUM]** |
112 113

```
114
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE" --form "value=updated value"
115 116
```

T
Tomasz Maczukin 已提交
117 118 119
```json
{
    "key": "NEW_VARIABLE",
120
    "value": "updated value",
121
    "variable_type": "env_var",
122
    "protected": true,
123 124
    "masked": false,
    "environment_scope": "*"
T
Tomasz Maczukin 已提交
125 126 127 128 129
}
```

## Remove variable

S
Shinya Maeda 已提交
130
Remove a project's variable.
T
Tomasz Maczukin 已提交
131 132 133 134 135

```
DELETE /projects/:id/variables/:key
```

136 137
| Attribute | Type    | required | Description             |
|-----------|---------|----------|-------------------------|
138
| `id`      | integer/string | yes      | The ID of a project or [urlencoded NAMESPACE/PROJECT_NAME of the project](README.md#namespaced-path-encoding) owned by the authenticated user     |
139
| `key`     | string  | yes      | The `key` of a variable |
140 141

```
142
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/variables/VARIABLE_1"
143
```