> ## Documentation Index
> Fetch the complete documentation index at: https://infisical.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# GitLab service account token rotation

> Learn how to automatically rotate GitLab service account tokens.

Rotate the personal access tokens of a GitLab [group service account](https://docs.gitlab.com/user/profile/service_accounts/) on a schedule. Infisical keeps two tokens active. On each rotation, it rotates the older one in GitLab, which revokes it and issues a replacement, and stores the new token in a secret. The service account doesn't need a token beforehand: Infisical creates the first one when you create the rotation.

> [!NOTE]
>
> **Rotation Type: Dual-Phase**
>
> This rotation maintains two active credential sets with overlapping validity, ensuring zero-downtime during rotation cycles.

> [!NOTE]
>
> This rotation needs a [GitLab app connection](https://infisical.com/docs/integrations/app-connections/gitlab.md). This guide walks you through creating a project-scoped app connection.

## Prerequisites

* A [project with secrets configured](https://infisical.com/docs/documentation/platform/secrets-mgmt/quick-starts/deliver-first-secret.md)
* A GitLab [service account](https://docs.gitlab.com/user/profile/service_accounts/) that belongs to a group or subgroup (it can be newly created, with no tokens)
* A GitLab user with the [Owner role](https://docs.gitlab.com/user/permissions/#roles) on that group, to create the token the connection uses
* To create the service account on GitLab Self-Managed or GitLab Dedicated, a user that is an administrator, or a group Owner that is [allowed to create service accounts](https://docs.gitlab.com/administration/settings/account_and_limit_settings/#allow-top-level-group-owners-to-create-service-accounts)

> [!WARNING]
>
> Don't rotate the generated tokens outside Infisical. GitLab lets a token with the `api` or `self_rotate` scope [rotate itself](https://docs.gitlab.com/api/personal_access_tokens/#self-rotate), which revokes it and issues a replacement that Infisical doesn't know about. The next time Infisical rotates that token, GitLab sees it was already rotated and revokes the replacement too. Until then, or if you delete the rotation first, the replacement stays active.

## Step 1: Create a personal access token for Infisical

Infisical needs a GitLab connection whose user has the Owner role on the group that owns the service account, so that Infisical can create and revoke the service account's tokens.

**Step 1**

Create a personal access token with the `api` scope for a user that has the Owner role on the group. This token is for the connection, not for the service account you rotate.

> [!TIP]
>
> To keep the connection independent of a person, give a second service account the Owner role on the group and [create a token for it](https://docs.gitlab.com/api/service_accounts/#create-a-personal-access-token-for-a-group-service-account) to use for the connection. It must be a different service account from the one you rotate: Infisical refuses to create tokens for the user the connection authenticates as.

**Step 2**

When you create the GitLab connection in [Step 2](#step-2-set-up-the-rotation-in-infisical), use this token with **Personal Access Token** as the access token type.

> [!NOTE]
>
> GitLab doesn't let group or project access tokens create other tokens, so the rotation fails with a connection that uses one. A connection that uses OAuth works if its user has the Owner role on the group.

## Step 2: Set up the rotation in Infisical

> [!TIP]
>
> To create the rotation using the API, use the GitLab service account token [Create](https://infisical.com/docs/api-reference/endpoints/secret-rotations/gitlab-service-account-token/create.md) endpoint.

**Step 1**

In your Secrets Management project, open the dashboard, select **Add New**, then select **Add Secret Rotation**.

![Add Secret Rotation](https://mintlify.s3.us-west-1.amazonaws.com/infisical/images/secret-rotations-v2/generic/add-secret-rotation.png)

Then, select the **GitLab Service Account Token** option.

**Step 2**

Under **Configuration**, choose the app connection and when the rotation runs:

* **Environment**: The project environment the rotated secrets are stored in
* **GitLab Connection**: The app connection the rotation uses
* **Rotation Interval (In Days)**: The number of days between rotations, up to 180
* **Rotate At (Local Time)**: The time of day the rotation runs
* **Auto-Rotation Enabled**: When enabled, the token rotates on the schedule; turn it off to rotate manually only

> [tab] Create a new app connection
>
> Under **GitLab Connection**, select **Create GitLab Connection** in the dropdown, then fill in:
>
> * **Name**: A name for the connection
> * **Description** (optional): What the connection is for
> * **Self-hosted URL** (optional): The URL of your GitLab Self-Managed or GitLab Dedicated instance; leave empty for GitLab.com
> * **Method**: **Access Token**
> * **Access Token Type**: **Personal Access Token**
> * **Access Token**: The token from [Step 1](#step-1-create-a-personal-access-token-for-infisical)
>
> Select **Connect to GitLab**. Infisical returns you to the rotation form with the new connection selected.
>
> > [!NOTE]
> >
> > This creates a connection scoped to the current project. To share the connection across other projects, [create it at the organization level](https://infisical.com/docs/integrations/app-connections/gitlab.md) first, then select it here.

> [tab] Use an existing connection
>
> Under **GitLab Connection**, select the app connection you'd like to use.

Then select **Continue**.

**Step 3**

Under **Parameters**, choose the service account and the token Infisical creates for it:

* **Group**: The group that owns the service account; the list shows groups where the connection's user has the Owner role (you can't change it after you create the rotation)
* **Service Account**: The service account whose tokens Infisical rotates (you can't change it after you create the rotation); the list leaves out the account the connection authenticates as
* **Token Name**: The name of each generated token; Infisical appends a timestamp so each rotated token has a distinct name
* **Token Description** (optional): A description shown on each generated token in GitLab
* **Scopes**: The [scopes](https://docs.gitlab.com/security/tokens/access_token_scopes/) granted to each generated token

Then select **Continue**.

**Step 4**

Under **Mappings**, choose the names of the Infisical secrets that hold the rotated values:

* **Token ID**: The name of the secret that holds the ID of the token in GitLab
* **Token**: The name of the secret that holds the token

Then select **Continue**.

**Step 5**

Give the rotation a **Name** and an optional **Description**. The name must be slug-friendly.

Then select **Continue**.

**Step 6**

Under **Alerts**, choose who Infisical notifies if a scheduled rotation fails. [Rotation failure alerts](https://infisical.com/docs/documentation/platform/secret-rotation/overview#get-alerted-when-a-rotation-fails) are on by default.

Then select **Continue**.

**Step 7**

Review your GitLab Service Account Token Rotation configuration, then select **Create secret rotation**.

> [!TIP]
>
> The token is now available through the mapped secrets. If auto-rotation is enabled, the token rotates on your configured schedule.

## Frequently asked questions

> [accordion] When do the generated tokens expire?
>
> Each token expires two rotation intervals plus three days after Infisical creates it, so the token is still valid when the next rotation revokes it. GitLab limits token lifetimes to 365 days by default, so the rotation interval can be at most 180 days.
>
> A token's expiry is fixed when Infisical creates it. When you change the rotation interval, Infisical rotates the token right away so the new token's expiry matches the new interval, and later rotations follow the new schedule.
>
> If an administrator set a shorter [maximum token lifetime](https://docs.gitlab.com/administration/settings/account_and_limit_settings/#limit-the-lifetime-of-access-tokens) on your instance, choose a rotation interval where twice the interval plus three days fits within that limit. Otherwise GitLab rejects the new token and the rotation fails with GitLab's error.

> [accordion] Does Infisical change tokens it didn't create?
>
> No. Infisical only revokes the tokens it created for the rotation, so it never revokes the connection's own token. A rotation can't target the service account the connection authenticates as, because the generated token would carry the connection's own access.
>
> If the service account already had tokens you created by hand, revoke them after your applications read the token from Infisical, so only the rotated tokens stay active.

> [accordion] Which permissions does the generated token have?
>
> The token has the scopes you select, limited by the service account's own role in each group or project. To change what the token can reach, change the service account's role, or edit the rotation's scopes; from the next rotation, Infisical replaces each token with a new one that has the new scopes.

## Next steps

> [card] [GitLab](https://infisical.com/docs/integrations/app-connections/gitlab.md)
>
> Set up the GitLab connection that Infisical uses to manage service account tokens.

> [card] [Overview](https://infisical.com/docs/documentation/platform/secret-rotation/overview)
>
> Learn how dual-phase and single-phase rotations work.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.