# Protect tags

Allow only holders of git:PushProtectedTag to create, move or delete tags that match a pattern.

Once a rule exists, creating, moving or deleting a matching tag needs `git:PushProtectedTag` in
addition to `git:PushRepository`: for example, so that only the release pipeline's role can cut a
`v1.2.3` that starts a production build. Replace v* with a tag name or glob.

## Prerequisites

- Permission for `git:ManageBranchProtection` on the repository.

## Add a rule

**Console:** not available. The Deutsche Cloud Console has no form for tag protection yet.
**CLI**
```sh
dcloud git tag-protection create 5d6c2a1e-7b3f-4c8d-9e0a-1b2c3d4e5f60 --pattern 'v*'
```

**API**
```http
POST /git/api/repositories/5d6c2a1e-7b3f-4c8d-9e0a-1b2c3d4e5f60/tag-protection
{ "pattern": "v*" }
```

**Terraform**
```hcl
resource "dcloud_git_tag_protection" "releases" {
  repository_id = dcloud_git_repository.this.id
  pattern       = "v*"
}
```

A pattern holds letters, digits and `. _ / * + -`, up to 200 characters; anything else is refused
with `Letters, digits and . _ / * + - only`. A pattern that already has a rule is refused with 409
`That pattern is already protected`.

## Result

A push that creates, moves or deletes a matching tag without `git:PushProtectedTag` is refused:

```
tag "v1.2.3" is protected (rule "v*"): you may not create or move it. Protected tags need git:PushProtectedTag.
```

A deploy key can never hold `git:PushProtectedTag`, so a push over a deploy key cannot create,
move or delete a protected tag.

## Remove a rule

List the rules with `dcloud git tag-protection list` to find the rule id, then remove it with
`dcloud git tag-protection delete` or `DELETE /git/api/repositories/{repoId}/tag-protection/{ruleId}`.

## Related

- [Delete a tag](delete-a-tag.md)
- `git.tag-protection.create` · `dcloud git tag-protection create` · `dcloud_git_tag_protection`
