doc/ci/cloud_services/azure/_index.md
{{< details >}}
{{< /details >}}
[!warning]
CI_JOB_JWT_V2was deprecated in GitLab 15.9 and is scheduled to be removed in GitLab 17.0. Use ID tokens instead.
This tutorial demonstrates how to use a JSON web token (JWT) in a GitLab CI/CD job to retrieve temporary credentials from Azure without needing to store secrets.
To get started, configure OpenID Connect (OIDC) for identity federation between GitLab and Azure. For more information on using OIDC with GitLab, read Connect to cloud services.
Prerequisites:
Owner access level.Application Developer access level.To complete this tutorial:
For more information about Azure identity federation, see workload identity federation.
To create an Entra ID application and service principal for GitLab:
In the Azure CLI, create the application for GitLab:
appId=$(az ad app create --display-name gitlab-oidc --query appId -otsv)
Save the appId (Application client ID) output, as you need it later
to configure your GitLab CI/CD pipeline.
Create a corresponding Service Principal:
az ad sp create --id $appId --query appId -otsv
Instead of the Azure CLI, you can use the Azure Portal to create these resources.
To create the federated identity credentials for the previous Entra ID application
for a specific branch in <mygroup>/<myproject>:
objectId=$(az ad app show --id $appId --query id -otsv)
cat <<EOF > body.json
{
"name": "gitlab-federated-identity",
"issuer": "https://gitlab.example.com",
"subject": "project_path:<mygroup>/<myproject>:ref_type:branch:ref:<branch>",
"description": "GitLab service account federated identity",
"audiences": [
"https://gitlab.example.com"
]
}
EOF
az rest --method POST --uri "https://graph.microsoft.com/beta/applications/$objectId/federatedIdentityCredentials" --body @body.json
For issues related to the values of issuer, subject or audiences, see the
troubleshooting details.
Optionally, you can now verify the Entra ID application and the Entra ID federated identity credentials from the Azure Portal:
gitlab-oidc.Application (client) ID,
Object ID, and Tenant ID.Certificates & secrets, go to Federated credentials to review your
Entra ID federated identity credentials.To create credentials for any branch or tag (wildcard matching), you can use flexible federated identity credentials.
For all branches in <mygroup>/<myproject>:
objectId=$(az ad app show --id $appId --query id -otsv)
cat <<EOF > body.json
{
"name": "gitlab-federated-identity",
"issuer": "https://gitlab.example.com",
"subject": null,
"claimsMatchingExpression": {
"value": "claims['sub'] matches 'project_path:<mygroup>/<myproject>:ref_type:branch:ref:*'",
"languageVersion": 1
},
"description": "GitLab service account federated identity",
"audiences": [
"https://gitlab.example.com"
]
}
EOF
az rest --method POST --uri "https://graph.microsoft.com/beta/applications/$objectId/federatedIdentityCredentials" --body @body.json
For all tags in <mygroup>/<myproject>:
objectId=$(az ad app show --id $appId --query id -otsv)
cat <<EOF > body.json
{
"name": "gitlab-federated-identity",
"issuer": "https://gitlab.example.com",
"subject": null,
"claimsMatchingExpression": {
"value": "claims['sub'] matches 'project_path:<mygroup>/<myproject>:ref_type:tag:ref:*'",
"languageVersion": 1
},
"description": "GitLab service account federated identity",
"audiences": [
"https://gitlab.example.com"
]
}
EOF
az rest --method POST --uri "https://graph.microsoft.com/beta/applications/$objectId/federatedIdentityCredentials" --body @body.json
After you create the credentials, use role assignment
to grant permissions to the previous service principal so it gets access to the Azure resources:
az role assignment create --assignee $appId --role Reader --scope /subscriptions/<subscription-id>
You can find your subscription ID in:
The previous command grants read-only permissions to the entire subscription. For more information on applying the principle of least privilege in the context of your organization, read Best practices for Entra ID roles.
After you configure the Entra ID application and federated identity credentials, the CI/CD job can retrieve a temporary credential by using the Azure CLI:
default:
image: mcr.microsoft.com/azure-cli:latest
variables:
AZURE_CLIENT_ID: "<client-id>"
AZURE_TENANT_ID: "<tenant-id>"
auth:
id_tokens:
GITLAB_OIDC_TOKEN:
aud: https://gitlab.com
script:
- az login --service-principal -u $AZURE_CLIENT_ID -t $AZURE_TENANT_ID --federated-token $GITLAB_OIDC_TOKEN
- az account show
The CI/CD variables are:
AZURE_CLIENT_ID: The application client ID you saved earlier.AZURE_TENANT_ID: Your Microsoft Entra ID tenant ID. You can
find it by using the Azure CLI or Azure Portal.GITLAB_OIDC_TOKEN: An OIDC ID token.No matching federated identity record foundIf you receive the error ERROR: AADSTS70021: No matching federated identity record found for presented assertion.
you should verify:
Issuer defined in the Entra ID federated identity credentials, for example
https://gitlab.com or your own GitLab URL.Subject identifier defined in the Entra ID federated identity credentials,
for example project_path:<mygroup>/<myproject>:ref_type:branch:ref:<branch>.
gitlab-group/gitlab-project project and main branch it would be:
project_path:gitlab-group/gitlab-project:ref_type:branch:ref:main.mygroup and myproject can be retrieved by checking the URL
when accessing your GitLab project or, in the upper-right corner of the project's overview page, selecting Code.Audience defined in the Entra ID federated identity credentials, for example https://gitlab.com
or your own GitLab URL.You can review these settings, as well as your AZURE_CLIENT_ID and AZURE_TENANT_ID
CI/CD variables, from the Azure Portal:
gitlab-oidc.Application (client) ID,
Object ID, and Tenant ID.Certificates & secrets, go to Federated credentials to review your
Entra ID federated identity credentials.Review Connect to cloud services for further details.
Request to External OIDC endpoint failed messageIf you receive the error ERROR: AADSTS501661: Request to External OIDC endpoint failed.
you should verify that your GitLab instance is publicly accessible from the internet.
Azure must be able to access the following GitLab endpoints to authenticate with OIDC:
GET /.well-known/openid-configurationGET /oauth/discovery/keysIf you update your firewall and still receive this error, clear the Redis cache and try again.
No matching federated identity record found for presented assertion audience messageIf you receive the error ERROR: AADSTS700212: No matching federated identity record found for presented assertion audience 'https://gitlab.com'
you should verify that your CI/CD job uses the correct aud value.
The aud value should match the audience used to create the federated identity credentials.