RealmDeployDocs
GuidesSingle sign-on for apps

Keycloak SSO for Jenkins

Sign in to Jenkins with Keycloak through the OpenId Connect Authentication plugin, map groups to Matrix or Role-based permissions, and use JCasC.

Jenkins signs users in with Keycloak through the OpenId Connect Authentication plugin (oic-auth). The Jenkins server exchanges the authorization code with Keycloak, so the client is confidential and has a secret. Groups come from a claim in the ID token, and Jenkins grants rights to them with Matrix-based security or the Role-based Authorization Strategy.

Checked against the OpenId Connect Authentication plugin 4.737.vb_3d1a_0e7cf0b_ (it needs Jenkins 2.539 or newer) and Keycloak 26.8 on 25 November 2026.

Replace auth.example.com with your RealmDeploy hostname and acme with your realm. The examples use https://jenkins.example.com for Jenkins.

In Keycloak

Create the client

Open Clients, choose Create client, and use these values:

SettingValue
Client typeOpenID Connect
Client IDjenkins
Client authenticationOn
Authentication flowStandard flow only
PKCE methodS256
Root URLhttps://jenkins.example.com
Home URLhttps://jenkins.example.com
Valid redirect URIshttps://jenkins.example.com/securityRealm/finishLogin
Valid post logout redirect URIshttps://jenkins.example.com/OicLogout
Web originsLeave empty. Jenkins talks to Keycloak from its server, not from the browser.

Choose Save, then copy the secret from the Credentials tab.

Add a groups claim

The plugin reads groups from the ID token or the userinfo response.

  1. On the jenkins client, open the Client scopes tab and choose jenkins-dedicated.
  2. Choose Configure a new mapper and pick Group Membership.
  3. Fill it in and choose Save:
FieldValue
Namegroups
Token Claim Namegroups
Full group pathOff
Add to ID tokenOn
Add to access tokenOff
Add to userinfoOn

Then create the groups under Groups, for example jenkins-admins and jenkins-developers, and add users to them.

In Jenkins

First set Jenkins URL under Manage Jenkins → System to https://jenkins.example.com/. The plugin builds the redirect URI from it.

Install OpenId Connect Authentication from Manage Jenkins → Plugins → Available plugins. Then open Manage Jenkins → Security and, under Security Realm, choose Login with OpenID Connect.

FieldValue
Client idjenkins
Client secretThe secret from the Credentials tab
Configuration modeDiscovery via well-known endpoint
Well-known configuration endpointhttps://auth.example.com/realms/acme/.well-known/openid-configuration
Override scopes (under the mode's advanced options)openid profile email

Under Advanced configuration → User fields:

FieldValue
User name field namepreferred_username
Full name field namename
Email field nameemail
Groups field namegroups

Further down in Advanced configuration:

FieldValue
Logout from OpenID ProviderOn
Post logout redirect URLhttps://jenkins.example.com/OicLogout
PropertiesAdd Enable Proof Key for Code Exchange (PKCE)

Without Override scopes, the plugin asks for every scope listed in the discovery document. openid profile email is enough: the groups claim comes from the client's own mapper whatever the scope.

Set the authorization strategy in the same page (see Groups and roles) and choose Save.

Once you save, every sign-in goes through Keycloak. If the configuration is wrong, nobody can sign in. Before you save, consider adding the Escape Hatch property, which keeps one local username and password that bypasses OpenID Connect. Keep its secret somewhere safe.

How logout works. With Logout from OpenID Provider on, signing out of Jenkins sends the browser to Keycloak's end-session endpoint with id_token_hint and the post_logout_redirect_uri you set. Keycloak ends the session and returns the user to /OicLogout, which must be listed in Valid post logout redirect URIs. No call goes from Keycloak to Jenkins, so no outgoing connection is needed.

The same settings in JCasC

The plugin documents its Configuration as Code fields. With the Configuration as Code plugin installed:

jenkins.yaml
jenkins:
  securityRealm:
    oic:
      serverConfiguration:
        wellKnown:
          wellKnownOpenIDConfigurationUrl: "https://auth.example.com/realms/acme/.well-known/openid-configuration"
          scopesOverride: "openid profile email"
      clientId: "jenkins"
      clientSecret: "${JENKINS_OIDC_CLIENT_SECRET}"
      userNameField: "preferred_username"
      fullNameFieldName: "name"
      emailFieldName: "email"
      groupsFieldName: "groups"
      logoutFromOpenidProvider: true
      postLogoutRedirectUrl: "https://jenkins.example.com/OicLogout"
      properties:
        - "pkce"
  authorizationStrategy:
    globalMatrix:
      entries:
        - group:
            name: "jenkins-admins"
            permissions:
              - "Overall/Administer"
        - group:
            name: "authenticated"
            permissions:
              - "Overall/Read"
              - "Job/Read"
              - "Job/Build"
unclassified:
  location:
    url: "https://jenkins.example.com/"

${JENKINS_OIDC_CLIENT_SECRET} is read from the environment of the Jenkins process, so the secret stays out of the file.

Groups and roles

The plugin turns each value of the groups claim into a Jenkins group of the same name. Jenkins does not list them anywhere; you type the names in the authorization strategy, with the same case and spaces as in Keycloak. The built-in authenticated group covers every signed-in user.

Matrix-based security (Matrix Authorization Strategy plugin):

  1. In Manage Jenkins → Security → Authorization, choose Matrix-based security.
  2. Choose Add group, enter jenkins-admins and tick Overall/Administer.
  3. For Authenticated Users, tick Overall/Read and the job permissions everyone should have.
  4. Choose Save.

Role-Based Strategy (Role-based Authorization Strategy plugin):

  1. In Manage Jenkins → Security → Authorization, choose Role-Based Strategy and save.
  2. Open Manage Jenkins → Role Management → Manage Roles and create a global role, for example developer with Overall/Read and Job/Build.
  3. Create a second global role with Overall/Administer, for example administrator.
  4. On Assign Roles, add the group jenkins-developers and tick developer, then add jenkins-admins and tick administrator.

Realm roles work too. Keycloak's built-in roles scope puts them in the access token under realm_access.roles, not in the ID token. Add a User Realm Role mapper to jenkins-dedicated with Token Claim Name roles and Add to ID token on, then set Groups field name to roles. The field takes a JMESPath expression, so realm_access.roles also works when that claim is in the ID token or userinfo.

Test it

  1. Open https://jenkins.example.com in a private window. Jenkins sends you to Keycloak.
  2. Sign in as a user in jenkins-developers.
  3. Open https://jenkins.example.com/whoAmI/ (the case matters). Authorities lists the groups Jenkins received. If your groups are missing, the plugin did not find the claim.
  4. To see the claims Keycloak sends, open the jenkins client in Keycloak, go to Client scopes → Evaluate, pick the user and look at Generated ID token. Paste a token into the JWT decoder to read it.
  5. Sign out. You pass through Keycloak's logout and land on Jenkins' logout page. Signing in again asks for the password.

Common errors

ErrorCauseFix
Invalid parameter: redirect_uri on the Keycloak pageThe redirect URI Jenkins sent is not in Valid redirect URIs, often http:// or the wrong host because Jenkins URL is not set.Set Jenkins URL and list https://jenkins.example.com/securityRealm/finishLogin exactly. Compare both with the redirect URI matcher.
Could not retrieve well-known config when savingJenkins cannot reach the discovery URL, or the URL is wrong.Open the URL from the Jenkins server, for example with curl. Check it with the OIDC discovery viewer.
Token request failed after Keycloak loginWrong client secret, or Client authentication is off.Copy the secret again from the Credentials tab. Look up the returned code in the OAuth error lookup.
No field 'preferred_username' was suppliedThe claim is not in the ID token or userinfo, usually because the profile scope was not requested.Keep profile in Override scopes.
Users sign in but have no rightsThe groups claim is missing, or Full group path is on, so Jenkins sees /jenkins-admins.Check /whoAmI/. Turn Full group path off, or enter the names with the leading slash.
Invalid redirect uri on logout/OicLogout is not in Valid post logout redirect URIs.Add https://jenkins.example.com/OicLogout to the client.
Login fails with a PKCE errorPKCE method is S256 in Keycloak but the PKCE property is not added in Jenkins.Add Enable Proof Key for Code Exchange (PKCE) under Properties, or clear PKCE method.

FAQ

How do scripts and agents call Jenkins now?

With an API token, not a password. Jenkins never sees the user's Keycloak password, so basic authentication with it does not work. Each user creates a token on their Jenkins account page.

Should I use the Keycloak Authentication plugin instead?

There is a separate Keycloak Authentication plugin for Jenkins, and it is not marked deprecated. This guide uses the OpenId Connect Authentication plugin because it works from Keycloak's standard discovery document and documents every field, including JCasC.

Do I need to ask you to open a connection?

No. Jenkins calls Keycloak's discovery and token endpoints, which are public on your hostname, and logout runs through the browser. Keycloak itself never calls Jenkins.

Sources

On this page