You can use the 1Password Secrets plugin for Jenkins with either a service account or a Connect server. If you configure both a service account and a Connect server, the Connect server takes precedence.
Step 1: Install the 1Password Secrets plugin for Jenkins
You have several available options to install the 1Password Secrets plugin for Jenkins. You can use the Jenkins GUI (graphical user interface), the Jenkins CLI tool, or a direct upload.
Jenkins GUI
Jenkins CLI tool
Direct upload
To install the 1Password Secrets plugin for Jenkins using the GUI:
Sign in to your Jenkins instance.
From your Jenkins dashboard, go to Manage Jenkins > Plugins.
Select the Available plugins tab.
Search for onepassword-secrets.
Select the 1Password Secrets plugin for Jenkins, then select Install.
Refer to Use the GUI for more detailed instructions.
To install the 1Password Secrets plugin for Jenkins using the CLI tool:
The 1Password Secrets plugin for Jenkins relies on 1Password CLI. You must install 1Password CLI on the same machine that runs the Jenkins CI/CD pipeline.You can install 1Password CLI using a Jenkinsfile or using a Freestyle job. Either of these methods can install 1Password CLI in the same pipeline as the 1Password Secrets plugin for Jenkins or in a separate pipeline.
If you install 1Password CLI in the same pipeline, you will need to add the 1Password CLI installation script before you make any calls to the plugin.If you install 1Password CLI in a separate pipeline, you will need to set the 1Password CLI path to the workspace where you performed the installation in your configuration.
Jenkinsfile
Freestyle job
To install 1Password CLI using a Jenkinsfile:
Sign in to your Jenkins instance.
Select New Item.
Enter the job details, then select Pipeline.
Configure the job details.
Scroll to Advanced Project Options > Pipeline.
Select Pipeline script.
Add the code to install 1Password CLI to the Script field.You can install 1Password CLI with a Jenkinsfile using declarative or scripted Jenkins pipeline syntax .
The following examples show how to install 1Password CLI version 2.24 on a Linux amd64 machine. Make sure to change the example scripts to match your platform and use the latest release of 1Password CLI.
Declarative Jenkinsfile
Scripted Jenkinsfile
Declarative Jenkinsfile
Report incorrect code
Copy
Ask AI
pipeline { agent any stages { stage('Install 1Password CLI') { steps { sh 'curl -sSfLo op.zip "https://cache.agilebits.com/dist/1P/op2/pkg/v2.24.0/op_linux_amd64_v2.24.0.zip"' sh 'unzip -o op.zip -d op-dir' sh 'mv -f op-dir/op /usr/local/bin' sh 'rm -r op.zip op-dir' } } }}# code-result[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /config/workspace/Example[Pipeline] {[Pipeline] stage[Pipeline] { (Install 1Password CLI)[Pipeline] sh+ curl -sSfLo op.zip https://cache.agilebits.com/dist/1P/op2/pkg/v2.24.0/op_linux_amd64_v2.24.0.zip[Pipeline] sh+ unzip -o op.zip -d op-dirArchive: op.zipextracting: op-dir/op.siginflating: op-dir/op[Pipeline] sh+ mv -f op-dir/op /usr/local/bin[Pipeline] sh+ rm -r op.zip op-dir[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS
Freestyle jobs don’t let you set both the configuration and secrets at the job level. 1Password recommends using a Jenkinsfile rather than a Freestyle job. See Migrating from chained Freestyle jobs to Pipelines .
To install 1Password CLI using a Freestyle job:
Sign in to your Jenkins instance.
Select New Item.
Enter the job name, then select Freestyle project.
Configure the job details.
Scroll to Build Steps, then select Add build step > Execute shell.
Add the following commands to the Execute shell build step.
Step 3: Configure the 1Password Secrets plugin for Jenkins
You can configure the 1Password Secrets plugin for Jenkins from the Jenkins dashboard:
Login to your Jenkins instance.
Select Manage Jenkins > System.
Scroll to 1Password Secrets.
Set the 1Password CLI path to the location of the 1Password CLI executable. The example script uses /usr/local/bin/op.
The remaining configuration steps depend on whether you use a service account or a Connect server.
Service account
Connect server
You must add at least one Service Account Credential to use a service account with the 1Password Secrets plugin for Jenkins.
Select Add > Jenkins under the Service Account Credential.
Set the Domain.
Set the Kind to Secret text. The plugin only supports secret text .
Set the Scope.
Set the Secret to the service account token.
You can leave the ID and Description empty.
Select Add.
You must specify the Connect Host value and add at least one Connect Credential to use a service account with the 1Password Secrets plugin for Jenkins.
Set Connect Host to the IP address, hostname, or URL of the Connect server.
Select Add > Jenkins under the Connect Credential.
Set the Domain.
Set the Kind to Secret text. The plugin only supports secret text .
Set the Scope.
Set the Secret to the Connect server token.
You can leave the ID and Description empty.
Select Add.
For example configurations and pipeline scripts, go to Usage examples.
If you configure both a Connect server and a service account, the Connect server takes precedence.
If you install 1Password CLI in the same pipeline, you will need to add the 1Password CLI installation script before you make any calls to the plugin.If you install 1Password CLI in a separate pipeline, you will need to set the 1Password CLI path to the workspace where you performed the installation in your configuration.
You can configure the 1Password Secrets plugin for Jenkins globally, per folder, or per job.
Level
Description
Global
Global-level configurations impact all jobs in all folders. To configure the 1Password Secrets plugin for Jenkins globally, add it to your global configuration.
Folder
Folder-level configurations impact all jobs within the folder. To configure the 1Password Secrets plugin for Jenkins for a specific folder, add it to the folder configuration.
Job
Job-level configurations only impact the jobs you configure. To configure the 1Password Secrets plugin for Jenkins for a single job, configure the plugin in the Jenkinsfile or Freestyle job.
The configuration priority is highest for job-level configurations and lowest for global configurations. The more specific the level, the higher the priority. This means you can have a default configuration at a broader level and override it at more specific levels.For example, if you configure a service account (service account A) globally, but override it with a different service account (service account B) in a job-level configuration, the job will use the second service account (service account B).
If you configure both a Connect server and a service account, the Connect server takes precedence.
You can access secrets within the Jenkins pipeline using secret references with the withSecrets function. This function receives the configuration and list of 1Password secrets and loads them as parameters. Explore the following examples to learn how to use the withSecrets function in various contexts.
About the configuration step...
Service account
Connect
The configuration at the beginning of the Jenkinsfile to configure the service account and 1Password CLI is optional. If you don’t provide a configuration, the 1Password Secrets plugin uses a more broadly scoped configuration.Make sure to replace service-account-token with the service account token and op-cli-path with the path you installed the 1Password CLI to (if you used a custom location). See Configuration.
The configuration at the beginning of the Jenkinsfile to configure the Connect server and 1Password CLI is optional. If you don’t provide a configuration, the 1Password Secrets plugin uses a more broadly scoped configuration.Make sure to replace connect-server-token with the Connect server token, connect-host with the Connect server IP address, and op-cli-path with the path you installed the 1Password CLI to (if you used a custom location). See Configuration.
The following examples show how to use the withSecrets function in a Jenkinsfile with a 1Password Service Account.
Declarative Jenkinsfile
Scripted Jenkinsfile
The following declarative Jenkinsfile shows how to use the withSecrets function with a service account. It first defines environment variables to hold the secret values, then uses the withSecrets function to access and assign the values.
Declarative Jenkinsfile
Report incorrect code
Copy
Ask AI
// Configure 1Password CLI and the service account.def config = [ serviceAccountCredentialId: 'service-account-token', opCLIPath: 'op-cli-path']// Define the environment variables for the values of the secrets.// Use the secret reference syntax: `op://<vault>/<item>[/section]/<field>`.def secrets = [ [envVar: 'DOCKER_USERNAME', secretRef: 'op://vault/item/username'], [envVar: 'DOCKER_PASSWORD', secretRef: 'op://vault/item/password']]pipeline { agent any stages{ stage('Push latest docker image') { steps { // Use the `withSecrets` function to access secrets. withSecrets(config: config, secrets: secrets) { docker.withRegistry('http://somehost:5100') { sh 'docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} http://somehost:5100' def image = docker.build('somebuild') image.push 'latest' } } } } }}
The following scripted Jenkinsfile shows how to use the withSecrets function with a service account. It first defines environment variables to hold the secret values, then uses the withSecrets function to access and assign the values.
Scripted Jenkinsfile
Report incorrect code
Copy
Ask AI
node { // Configure 1Password CLI and the service account def config = [ serviceAccountCredentialId: 'service-account-token', opCLIPath: 'op-cli-path' ] // Define the environment variables for the values of the secrets. // Use the secret reference syntax: `op://<vault>/<item>[/section]/<field>`. def secrets = [ [envVar: 'DOCKER_USERNAME', secretRef: 'op://vault/item/username'], [envVar: 'DOCKER_PASSWORD', secretRef: 'op://vault/item/password'] ] stage('Push latest docker image') { // Use the `withSecrets` function to access secrets. withSecrets(config: config, secrets: secrets) { docker.withRegistry('http://somehost:5100') { sh 'docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} http://somehost:5100' def image = docker.build('somebuild') image.push 'latest' } } }}
The following examples show how to use the withSecrets function in a Jenkinsfile with a 1Password Connect Server.
Declarative Jenkinsfile
Scripted Jenkinsfile
The following declarative Jenkinsfile shows how to use the withSecrets function with a Connect server. It first defines environment variables to hold the secret values, then uses the withSecrets function to access and assign the values.
Declarative Jenkinsfile
Report incorrect code
Copy
Ask AI
// Configure 1Password CLI and the Connect server.def config = [ connectHost: 'connect-host', connectCredentialId: 'connect-server-token', opCLIPath: 'op-cli-path']// Define the environment variables for the values of the secrets.// Use the secret reference syntax: `op://<vault>/<item>[/section]/<field>`.def secrets = [ [envVar: 'DOCKER_USERNAME', secretRef: 'op://vault/item/username'], [envVar: 'DOCKER_PASSWORD', secretRef: 'op://vault/item/password']]pipeline { agent any stages{ stage('Push latest docker image') { steps { // Use the `withSecrets` function to access secrets. withSecrets(config: config, secrets: secrets) { docker.withRegistry('http://somehost:5100') { sh 'docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} http://somehost:5100' def image = docker.build('somebuild') image.push 'latest' } } } } }}
The following scripted Jenkinsfile shows how to use the withSecrets function with a Connect server. It first defines environment variables to hold the secret values, then uses the withSecrets function to access and assign the values.
Scripted Jenkinsfile
Report incorrect code
Copy
Ask AI
node { // Configure 1Password CLI and the Connect server. def config = [ connectHost: 'connect-host', connectCredentialId: 'connect-server-token', opCLIPath: 'op-cli-path' ] // Define the environment variables for the values of the secrets. // Use the secret reference syntax: `op://<vault>/<item>[/section]/<field>`. def secrets = [ [envVar: 'DOCKER_USERNAME', secretRef: 'op://vault/item/username'], [envVar: 'DOCKER_PASSWORD', secretRef: 'op://vault/item/password'] ] stage('Push latest docker image') { // Use the `withSecrets` function to access secrets. withSecrets(config: config, secrets: secrets) { docker.withRegistry('http://somehost:5100') { sh 'docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} http://somehost:5100' def image = docker.build('somebuild') image.push 'latest' } } }}
The 1Password Secrets plugin for Jenkins lets you use environment variables to get configuration and secrets. The following examples show how to use environment variables with the 1Password Secrets plugin for Jenkins.You must set the environment variables in the environment block. The following table describes each environment variable.
Variable
Required
Description
OP_CLI_PATH
Always required.
The path where you installed the 1Password CLI. If you followed the 1Password CLI installation instruction, set this value to /usr/local/bin.
The example scripts on this page set this value to op-cli-path.
OP_SERVICE_ACCOUNT_TOKEN
Required if using a service account.
The service account token of the service account to use.
The example scripts on this page set this value to service-account-token variable.
OP_CONNECT_HOST
Required if using a Connect server.
The IP address, hostname, or URL of the Connect server to use.
The example scripts on this page set this value to connect-server-host.
OP_CONNECT_TOKEN
Required if using a Connect server.
The Connect server token of the Connect server to use.
The example scripts on this page set this value to connect-server-token.