# Create a deployment

From **GitHub v3 REST API**.

`POST /repos/{owner}/{repo}/deployments`

Deployments offer a few configurable parameters with certain defaults.

The `ref` parameter can be any named branch, tag, or SHA. At GitHub Enterprise Server we often deploy branches and verify them
before we merge a pull request.

The `environment` parameter allows deployments to be issued to different runtime environments. Teams often have
multiple environments for verifying their applications, such as `production`, `staging`, and `qa`. This parameter
makes it easier to track which environments have requested deployments. The default environment is `production`.

The `auto_merge` parameter is used to ensure that the requested ref is not behind the repository's default branch. If
the ref _is_ behind the default branch for the repository, we will attempt to merge it for you. If the merge succeeds,
the API will return a successful merge commit. If merge conflicts prevent the merge from succeeding, the API will
return a failure response.

By default, [commit statuses](https://docs.github.com/enterprise-server@3.0/rest/reference/commits#commit-statuses) for every submitted context must be in a `success`
state. The `required_contexts` parameter allows you to specify a subset of contexts that must be `success`, or to
specify contexts that have not yet been submitted. You are not required to use commit statuses to deploy. If you do
not require any contexts or create any commit statuses, the deployment will always succeed.

The `payload` parameter is available for any extra information that a deployment system might need. It is a JSON text
field that will be passed on when a deployment event is dispatched.

The `task` parameter is used by the deployment system to allow different execution paths. In the web world this might
be `deploy:migrations` to run schema changes on the system. In the compiled world this could be a flag to compile an
application with debugging enabled.

Users with `repo` or `repo_deployment` scopes can create a deployment for a given ref.

#### Merged branch response
You will see this response when GitHub automatically merges the base branch into the topic branch instead of creating
a deployment. This auto-merge happens when:
*   Auto-merge option is enabled in the repository
*   Topic branch does not include the latest changes on the base branch, which is `master` in the response example
*   There are no merge conflicts

If there are no new commits in the base branch, a new request to create a deployment should give a successful
response.

#### Merge conflict response
This error happens when the `auto_merge` option is enabled and when the default branch (in this case `master`), can't
be merged into the branch that's being deployed (in this case `topic-branch`), due to merge conflicts.

#### Failed commit status checks
This error happens when the `required_contexts` parameter indicates that one or more contexts need to have a `success`
status for the commit to be deployed, but one or more of the required contexts do not have a state of `success`.

## Parameters

### `owner`

- Location: path
- Required: true
- Type: `string`

### `repo`

- Location: path
- Required: true
- Type: `string`

## Request body

- Required: true
### `application/json`

- Type: `object`

```json
{
  "auto_merge": false,
  "description": "Deploy request from hubot",
  "payload": "{ \"deploy\": \"migrate\" }",
  "ref": "topic-branch",
  "required_contexts": [
    "ci/janky",
    "security/brakeman"
  ]
}
```

```json
{"properties":{"auto_merge":{"default":true,"description":"Attempts to automatically merge the default branch into the requested ref, if it's behind the default branch.","type":"boolean"},"description":{"default":"","description":"Short description of the deployment.","nullable":true,"type":"string"},"environment":{"default":"production","description":"Name for the target deployment environment (e.g., `production`, `staging`, `qa`).","type":"string"},"payload":{"oneOf":[{"additionalProperties":true,"type":"object"},{"default":"","description":"JSON payload with extra information about the deployment.","type":"string"}]},"production_environment":{"description":"Specifies if the given environment is one that end-users directly interact with. Default: `true` when `environment` is `production` and `false` otherwise.  \n**Note:** This parameter requires you to use the [`application/vnd.github.ant-man-preview+json`](https://docs.github.com/enterprise-server@3.0/rest/overview/api-previews#enhanced-deployments) custom media type.","type":"boolean"},"ref":{"description":"The ref to deploy. This can be a branch, tag, or SHA.","type":"string"},"required_contexts":{"description":"The [status](https://docs.github.com/enterprise-server@3.0/rest/reference/commits#commit-statuses) contexts to verify against commit status checks. If you omit this parameter, GitHub verifies all unique contexts before creating a deployment. To bypass checking entirely, pass an empty array. Defaults to all unique contexts.","items":{"type":"string"},"type":"array"},"task":{"default":"deploy","description":"Specifies a task to execute (e.g., `deploy` or `deploy:migrations`).","type":"string"},"transient_environment":{"default":false,"description":"Specifies if the given environment is specific to the deployment and will no longer exist at some point in the future. Default: `false`  \n**Note:** This parameter requires you to use the [`application/vnd.github.ant-man-preview+json`](https://docs.github.com/enterprise-server@3.0/rest/overview/api-previews#enhanced-deployments) custom media type.","type":"boolean"}},"required":["ref"],"type":"object"}
```

## Responses

### `201`

Response

### `application/json`

- Type: `object`

A request for a specific ref(branch,sha,tag) to be deployed

```json
{
  "created_at": "2012-07-20T01:19:13Z",
  "creator": {
    "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    "events_url": "https://api.github.com/users/octocat/events{/privacy}",
    "followers_url": "https://api.github.com/users/octocat/followers",
    "following_url": "https://api.github.com/users/octocat/following{/other_user}",
    "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
    "gravatar_id": "",
    "html_url": "https://github.com/octocat",
    "id": 1,
    "login": "octocat",
    "node_id": "MDQ6VXNlcjE=",
    "organizations_url": "https://api.github.com/users/octocat/orgs",
    "received_events_url": "https://api.github.com/users/octocat/received_events",
    "repos_url": "https://api.github.com/users/octocat/repos",
    "site_admin": false,
    "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
    "type": "User",
    "url": "https://api.github.com/users/octocat"
  },
  "description": "Deploy request from hubot",
  "environment": "production",
  "id": 1,
  "node_id": "MDEwOkRlcGxveW1lbnQx",
  "original_environment": "staging",
  "payload": {},
  "production_environment": true,
  "ref": "topic-branch",
  "repository_url": "https://api.github.com/repos/octocat/example",
  "sha": "a84d88e7554fc1fa21bcbc4efae3c782a70d2b9d",
  "statuses_url": "https://api.github.com/repos/octocat/example/deployments/1/statuses",
  "task": "deploy",
  "transient_environment": false,
  "updated_at": "2012-07-20T01:19:13Z",
  "url": "https://api.github.com/repos/octocat/example/deployments/1"
}
```

```json
{"description":"A request for a specific ref(branch,sha,tag) to be deployed","properties":{"created_at":{"example":"2012-07-20T01:19:13Z","format":"date-time","type":"string"},"creator":{"$ref":"#/components/schemas/nullable-simple-user"},"description":{"example":"Deploy request from hubot","nullable":true,"type":"string"},"environment":{"description":"Name for the target deployment environment.","example":"production","type":"string"},"id":{"description":"Unique identifier of the deployment","example":42,"type":"integer"},"node_id":{"example":"MDEwOkRlcGxveW1lbnQx","type":"string"},"original_environment":{"example":"staging","type":"string"},"payload":{"oneOf":[{"additionalProperties":true,"type":"object"},{"type":"string"}]},"performed_via_github_app":{"$ref":"#/components/schemas/nullable-integration"},"production_environment":{"description":"Specifies if the given environment is one that end-users directly interact with. Default: false.","example":true,"type":"boolean"},"ref":{"description":"The ref to deploy. This can be a branch, tag, or sha.","example":"topic-branch","type":"string"},"repository_url":{"example":"https://api.github.com/repos/octocat/example","format":"uri","type":"string"},"sha":{"example":"a84d88e7554fc1fa21bcbc4efae3c782a70d2b9d","type":"string"},"statuses_url":{"example":"https://api.github.com/repos/octocat/example/deployments/1/statuses","format":"uri","type":"string"},"task":{"description":"Parameter to specify a task to execute","example":"deploy","type":"string"},"transient_environment":{"description":"Specifies if the given environment is will no longer exist at some point in the future. Default: false.","example":true,"type":"boolean"},"updated_at":{"example":"2012-07-20T01:19:13Z","format":"date-time","type":"string"},"url":{"example":"https://api.github.com/repos/octocat/example/deployments/1","format":"uri","type":"string"}},"required":["id","node_id","sha","ref","task","environment","creator","payload","description","statuses_url","repository_url","url","created_at","updated_at"],"title":"Deployment","type":"object"}
```

### `202`

Merged branch response

### `application/json`

- Type: `object`

```json
{
  "message": "Auto-merged master into topic-branch on deployment."
}
```

```json
{"properties":{"message":{"type":"string"}},"type":"object"}
```

### `409`

Conflict when there is a merge conflict or the commit's status checks failed

### `422`

Validation failed

### `application/json`

- Type: `object`

Validation Error

```json
{"description":"Validation Error","properties":{"documentation_url":{"type":"string"},"errors":{"items":{"properties":{"code":{"type":"string"},"field":{"type":"string"},"index":{"type":"integer"},"message":{"type":"string"},"resource":{"type":"string"},"value":{"oneOf":[{"nullable":true,"type":"string"},{"nullable":true,"type":"integer"},{"items":{"type":"string"},"nullable":true,"type":"array"}]}},"required":["code"],"type":"object"},"type":"array"},"message":{"type":"string"}},"required":["message","documentation_url"],"title":"Validation Error","type":"object"}
```

## Request examples

### cURL

```shell
curl --request POST \
  --url {protocol}://{hostname}/api/v3/repos/{owner}/{repo}/deployments \
  --header 'content-type: application/json' \
  --data '{
  "auto_merge": false,
  "description": "Deploy request from hubot",
  "payload": "{ \"deploy\": \"migrate\" }",
  "ref": "topic-branch",
  "required_contexts": [
    "ci/janky",
    "security/brakeman"
  ]
}'
```
