# 1PasswordからJenkinsに秘密情報を読み込む

[Jenkins用の1Password Secretsプラグイン](https://plugins.jenkins.io/onepassword-secrets/)を使うと[、シークレット参照](https://developer.1password.com/docs/cli/secret-reference-syntax/)を使用して、1Password からシークレットを Jenkins CI/CD パイプラインの環境変数として安全に読み込むことができます。シークレット参照は 1Password と自動的に同期され、コード内のプレーンテキストのシークレットが公開されるリスクがなくなります。

プラグインは、[1Password サービス アカウント](https://developer.1password.com/docs/service-accounts/)または[1Password Connect サーバー](https://developer.1password.com/docs/connect/)を使用して認証できます。

情報

1Password Jenkins プラグインは、1Passwordによって公式にサポートおよび管理されています。コミュニティへの貢献は大歓迎です。GitHub[のリポジトリをご覧ください。](https://github.com/jenkinsci/onepassword-secrets-plugin)

### 必要条件 <a href="#id-9ikk2pv0volq" id="id-9ikk2pv0volq"></a>

Jenkins 用の 1Password Secrets プラグインは、[サービス アカウント](https://developer.1password.com/docs/service-accounts/get-started/)または[Connect サーバー](https://developer.1password.com/docs/connect/get-started/)のいずれかで使用できます。サービス アカウントと Connect サーバーの両方を構成すると、Connect サーバーが優先されます。

**サービスアカウント**

* **1Passwordに**[サインアップしてください。](https://start.1password.com/sign-up/plan)
* [サービス アカウントをCreateします。](https://developer.1password.com/docs/service-accounts/get-started/)
* [Jenkins インスタンスを起動して実行します。](https://www.jenkins.io/doc/)

**サーバーに接続**

* **1Passwordに**[サインアップしてください。](https://start.1password.com/sign-up/plan)
* [Connect サーバーをCreateしてデプロイします](https://developer.1password.com/docs/connect/get-started/)。
* [Jenkins インスタンスを起動して実行します。](https://www.jenkins.io/doc/)

### **Get started** <a href="#sff3bi4pzl5a" id="sff3bi4pzl5a"></a>

Jenkins 用の 1Password Secrets プラグインを使い始めるには:

* ①[Jenkins 用の 1Password Secrets プラグインをインストールします。](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect#install-plugin)
* ②[1Password CLI をインストールします。](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect#step-2-install-1password-cli)
* ③[Jenkins 用の 1Password Secrets プラグインを設定します。](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect#configure-plugin)

#### **Step 1: Install the 1Password Secrets plugin for Jenkins** <a href="#id-635zhcnk2vdz" id="id-635zhcnk2vdz"></a>

Jenkins 用の 1Password Secrets プラグインをインストールするには、いくつかのオプションがあります。Jenkins GUI (グラフィカル ユーザー インターフェイス)、Jenkins CLI ツール、または直接アップロードを使用できます。

**Jenkins GUI**

GUI を使用して Jenkins 用の 1Password Secrets プラグインをインストールするには:

* ①Jenkins インスタンスにサインインします。
* ②Jenkins ダッシュボードから、**Manage Jenkins > Plugins**に移動します。
* ③**Available plugins**タブを選択します。
* ④*onepassword-secrets*を検索します。
* ⑤Jenkins 用の 1Password Secrets プラグインを選択し、**Install**を選択します。

より詳細な手順については、[GUIの使用](https://www.jenkins.io/doc/book/managing/plugins/#from-the-web-ui)を参照してください。

**Jenkins CLI tool**

CLI ツールを使用して Jenkins 用の 1Password Secrets プラグインをインストールするには:

* ①[CLIツールの使用説明書に](https://github.com/jenkinsci/plugin-installation-manager-tool)従ってください。
* ②次のコマンドを実行します。必ず1.0.0を正しいバージョンに置き換えてください。
* $ jenkins-plugin-cli --plugins onepassword-secrets:1.0.0

**Direct upload**

直接アップロードを使用して Jenkins 用の 1Password Secrets プラグインをインストールするには:

* ①[リリース](https://plugins.jenkins.io/onepassword-secrets/#releases)の1つをダウンロードする。
* ②Jenkins ダッシュボードから、**Manage Jenkins > Plugins**に移動します。
* ③**Advanced**タブを選択します。
* ④.hpiリリースからダウンロードしたファイルを選択します。
* ⑤**Deploy**を選択します。

[直接アップロードの使用](https://www.jenkins.io/doc/book/managing/plugins/#advanced-installation)を参照してくださいより詳細な手順については、こちらをご覧ください。

#### **Step 2: Install 1Password CLI** <a href="#xkdn11hgq1u8" id="xkdn11hgq1u8"></a>

Jenkins 用の 1Password Secrets プラグインは 1Password CLI に依［Save］しています。Jenkins CI/CD パイプラインを実行するのと同じマシンに 1Password CLI をインストールする必要があります。

[Jenkinsfileを](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/)使用して1Password CLIをインストールできますまたは、Freestyle ジョブを使用します。どちらの方法でも、1Password CLI を Jenkins の 1Password Secrets プラグインと同じパイプラインにインストールすることも、別のパイプラインにインストールすることもできます。

情報

同じパイプラインに 1Password CLI をインストールする場合は、プラグインを呼び出す前に 1Password CLI インストール スクリプトを追加する必要があります。

[1Password CLI を別のパイプラインにインストールする場合は、構成](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect\&plugin-install=direct-upload#configuration)でインストールを実行したワークスペースへの**1Password CLI** パスを設定する必要があります。

**Jenkinsfile**

Jenkinsfile を使用して 1Password CLI をインストールするには:

* ①Jenkins インスタンスにサインインします。
* ②**New Item**を選択します。
* ③ジョブの詳細を入力し、**Pipeline**を選択します。
* ④ジョブの詳細を設定します。
* ⑤**Advanced Project Options > Pipeline**までスクロールします。
* ⑥**Pipeline script**を選択します。
* ⑦1Password CLI をインストールするためのコードを**Script**フィールドに追加します。\
  宣言型またはスクリプト化された[Jenkinsパイプライン構文](https://www.jenkins.io/doc/book/pipeline/syntax/)を使用して、Jenkinsfileで1Password CLIをインストールできます。。<br>
* 情報\
  次の例は、Linux amd64 マシンに 1Password CLI バージョン 2.24 をインストールする方法を示しています。サンプル スクリプトをプラットフォームに合わせて変更し、[1Password CLI の最新リリース](https://app-updates.agilebits.com/product_history/CLI2)を使用してください。

**Declarative Jenkinsfile**

**Declarative Jenkinsfile**

**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'**

**}**

**}**

**}**

**}**

**Scripted Jenkinsfile**

**Scripted Jenkinsfile**

**node {**

**stage('Install 1Password CLI') {**

**sh '''**

**ARCH="amd64"**

**curl -sSfLo op.zip "<https://cache.agilebits.com/dist/1P/op2/pkg/v2.24.0/op\\_linux\\_${ARCH}\\_v2.24.0.zip>"**

**unzip -o op.zip -d op-dir**

**mv -f op-dir/op /usr/local/bin**

**rm -r op.zip op-dir**

**'''**

**}**

**}**

**See result...**

**\[Pipeline] Start of Pipeline**

**\[Pipeline] node**

**Running 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-dir**

**Archive: op.zip**

**extracting: op-dir/op.sig**

**inflating: 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 Pipeline**

**Finished: SUCCESS**

* ⑧ビルドをSaveします。

#### **Freestyle job** <a href="#id-52z1wxyl1mlf" id="id-52z1wxyl1mlf"></a>

注意

フリースタイルジョブでは、ジョブレベルで設定とシークレットの両方を設定することはできません。1Password では、フリースタイルジョブではなく Jenkinsfile の使用を推奨しています。[連鎖フリースタイルジョブからパイプラインへの移行を参照してください。](https://www.jenkins.io/blog/2016/06/29/from-freestyle-to-pipeline/)

Freestyle ジョブを使用して 1Password CLI をインストールするには:

* ①Jenkins インスタンスにサインインします。
* ②**New Item**を選択します。
* ③ジョブ名を入力し、**Freestyle project**を選択します。
* ④ジョブの詳細を設定します。
* ⑤**Build Steps**までスクロールし、**Add build step > Execute shell**を選択します。
* ⑥**Execute shell**の実行ステップに次のコマンドを追加します。

$ ARCH="amd64"

$ curl -sSfLo op.zip "<https://cache.agilebits.com/dist/1P/op2/pkg/v2.24.0/op\\_linux\\_${ARCH}\\_v2.24.0.zip>"

$ unzip -o op.zip -d op-dir

$ mv -f op-dir/op /usr/local/bin

$ rm -r op.zip op-dir

* See result...

Running as SYSTEM

Building in workspace /config/workspace/Example

\[Test 1Password Secrets Freestyle] $ /bin/sh -xe /tmp/jenkins14763651031574634007.sh

\+ ARCH=amd64

\+ curl -sSfLo op.zip <https://cache.agilebits.com/dist/1P/op2/pkg/v2.24.0/op\\_linux\\_amd64\\_v2.24.0.zip>

\+ unzip -o op.zip -d op-dir

Archive: op.zip

extracting: op-dir/op.sig

inflating: op-dir/op

\+ mv -f op-dir/op /usr/local/bin

\+ rm -r op.zip op-dir

Finished: SUCCESS

* ⑦ビルドをSaveします。

#### **Step 3: Configure the 1Password Secrets plugin for Jenkins** <a href="#ylbpy12tylqg" id="ylbpy12tylqg"></a>

Jenkins ダッシュボードから Jenkins 用の 1Password Secrets プラグインを設定できます。

* ①Jenkins インスタンスにログインします。
* ②**Manage Jenkins > System**を選択します。
* ③**1Password Secrets**までスクロールします。
* ④ **1Password CLI path**を1Password CLI 実行可能ファイルの場所に設定します。\
  サンプル スクリプトでは /usr/local/bin/opを使用します。

残りの構成手順は、サービス アカウントを使うか、Connect サーバーを使うかによって異なります。

**Service account**

Jenkins の 1Password Secrets プラグインでサービス アカウントを使うには、少なくとも 1 つのサービス アカウント資格情報を追加する必要があります。

* ①サービス アカウント資格情報の下にある**Add > Jenkins**を選択します。
* ②**Domain**を設定します。
* ③ **Kind** を**Secret text**に設定します。\
  プラグインは[秘密のテキスト](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#secret-text)のみをサポートします。
* ④**Scope**を設定します。
* ⑤**Secret**をサービス アカウント トークンに設定します。
* ⑥**ID**と**Description**は空のままにしておくことができます。
* ⑦**Add**を選択します。

**Connect server**

Jenkins の 1Password Secrets プラグインでサービス アカウントを使うには、Connect Host 値を指定し、少なくとも 1 つの Connect Credential を追加する必要があります。

* ①**Connect Host** をConnect サーバーの IP アドレス、ホスト名、または URL に設定します。
* ②Connect Credentialの下にある**Add > Jenkins**を選択します。
* ③**Domain**を設定します。
* ④**Kind**を**Secret text**に設定します。\
  プラグインは[秘密のテキスト](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#secret-text)のみをサポートします。
* ⑤**Scope**を設定します。
* ⑥**Secret**をConnect サーバー トークンに設定します。
* ⑦**ID**と**Description**は空のままにしておくことができます。
* ⑧**Add**を選択します。

構成とパイプライン スクリプトの例については、[「使用例」](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#usage-examples)を参照してください。

情報

Connect サーバーとサービス アカウントの両方を構成する場合、Connect サーバーが優先されます。

### **Configuration** <a href="#glbsg9552e9h" id="glbsg9552e9h"></a>

次のセクションでは、使用可能な構成[オプション](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#configuration-options)と構成[スコープ](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#configuration-scope)について説明します。

情報

同じパイプラインに 1Password CLI をインストールする場合は、プラグインを呼び出す前に 1Password CLI インストール スクリプトを追加する必要があります。

1Password CLI を別のパイプラインにインストールする場合は、構成でインストールを実行したワークスペースへの**1Password CLI path**を設定する必要があります。

#### **Configuration options** <a href="#kwxku2q9oqv6" id="kwxku2q9oqv6"></a>

Jenkins 用の 1Password Secrets プラグインには、1Password CLI、Connect サーバー、およびサービス アカウントの構成オプションがあります。

| **設定**           | **説明**                                                                                                                                                                                                                         |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| ホストを接続           | Connect サーバーのホスト名、IP アドレス、または URL。                                                                                                                                                                                             |
| 接続資格情報           | Connectサーバーで認証するための[Connectサーバートークン](https://developer.1password.com/docs/connect/connect-security/#connect-server-tokens)。この設定は[Jenkinsの秘密テキスト認証情報です。](https://www.jenkins.io/doc/book/using/using-credentials/)              |
| サービス アカウント認証情報   | サービスアカウントで認証するための[サービスアカウントトークン](https://developer.1password.com/docs/service-accounts/security/#service-accounts-and-token-generation)。この設定は[Jenkinsの秘密のテキスト認証情報です。](https://www.jenkins.io/doc/book/using/using-credentials) |
| 1Password CLI パス | 1Password CLI実行ファイルへのパス。デフォルトの場所は/usr/local/bin/op                                                                                                                                                                             |

#### **Configuration scope** <a href="#koxpduzfcc5h" id="koxpduzfcc5h"></a>

Jenkins の 1Password Secrets プラグインは、グローバル、フォルダーごと、またはジョブごとに設定できます。

| **レベル** | **説明**                                                                                                                |
| ------- | --------------------------------------------------------------------------------------------------------------------- |
| グローバル   | グローバル レベルの構成は、全てのフォルダー内の全てのジョブに影響します。Jenkins の 1Password Secrets プラグインをグローバルに構成するには、それをグローバル構成に追加します。                 |
| フォルダ    | フォルダー レベルの設定は、フォルダー内の全てのジョブに影響します。特定のフォルダーに対して Jenkins 用の 1Password Secrets プラグインを設定するには、それをフォルダー設定に追加します。            |
| 仕事      | ジョブレベルの設定は、設定したジョブにのみ影響します。Jenkins の 1Password Secrets プラグインを単一のジョブに設定するには、Jenkinsfile または Freestyle ジョブでプラグインを設定します。 |

構成の優先度は、ジョブ レベルの構成では最も高く、グローバル構成では最も低くなります。レベルが具体的であるほど、優先度が高くなります。つまり、より広いレベルでデフォルト構成を設定し、より具体的なレベルでそれを上書きすることができます。

例えば、サービス アカウント (サービス アカウント A) をグローバルに構成したが、ジョブ レベルの構成で別のサービス アカウント (サービス アカウント B) で上書きした場合、ジョブは 2 番目のサービス アカウント (サービス アカウント B) を使用します。

情報

Connect サーバーとサービス アカウントの両方を構成する場合、Connect サーバーが優先されます。

### **Usage examples** <a href="#id-7da3uzu78unl" id="id-7da3uzu78unl"></a>

次のセクションでは、Jenkins 用の 1Password Secrets プラグインのいくつかの使用例について説明します。

* [Jenkinsfile で使う](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#use-with-a-jenkinsfile)
* [環境変数を使う](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#use-with-environment-variables)
* [フリースタイルジョブで使う](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#use-in-a-freestyle-job)

#### **Use with a Jenkinsfile** <a href="#id-369ol5rps185" id="id-369ol5rps185"></a>

withSecrets関数で[シークレット参照](https://developer.1password.com/docs/cli/secret-reference-syntax/)を使用して、Jenkins パイプライン内のシークレットにアクセスできます。この関数は、1Password シークレットの設定とリストを受け取り、それらをパラメータとして読み込みます。次の例を調べて、さまざまなコンテキストでwithSecrets関数を使う方法を学習してください。

設定手順について...

**Service account**

サービス アカウントと 1Password CLI を構成するための Jenkinsfile の先頭の構成はオプションです。構成を指定しない場合、1Password Secrets プラグインは[より広範囲にわたる構成を](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#configuration-scope)使用します。

service-account-token をサービス アカウント トークンに、op-cli-path を1Password CLI をインストールしたパス (カスタムの場所を使用した場合) に置き換えてください。構成[を](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload#configuration)参照してください。

![](https://4200399111-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8GVOyHz3wUSpdV3qlPwR%2Fuploads%2FH1TOvNCMRDdQlZLemxTu%2F0.png?alt=media)

**Connect**

Jenkinsfile の先頭にある、Connect サーバーと 1Password CLI を構成するための構成はオプションです。構成を指定しない場合、1Password Secrets プラグインは[より広範囲にわたる構成を](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect\&plugin-install=direct-upload#configuration-scope)使用します。

connect-server-tokenを Connect サーバーのトークンに、connect-host をConnect サーバーの IP アドレスに、

op-cli-path を1Password CLI をインストールしたパス (カスタムの場所を使用した場合) に置き換えてください。 [「構成」](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=connect\&plugin-install=direct-upload#configuration)を参照してください。

![](https://4200399111-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8GVOyHz3wUSpdV3qlPwR%2Fuploads%2FAOQB3wl8ZcC3Tco6Mfn0%2F1.png?alt=media)

**Service account**

withSecrets次の例は、1Password サービス アカウントを使用して Jenkinsfile で関数を使う方法を示しています。

**Declarative Jenkinsfile**

次の[宣言型Jenkinsfile](https://www.jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline)withSecretsサービス アカウントで関数を使う方法を示します。最初に、秘密の値を保持するための環境変数を定義し、次にwithSecrets関数を使用して値にアクセスし、値を割り当てます。

宣言型 Jenkinsfile

// 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'

}

}

}

}

}

}

**Scripted Jenkinsfile**

次の[スクリプト化されたJenkinsfile](https://www.jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline)withSecretsサービス アカウントで関数を使う方法を示します。最初に、秘密の値を保持するための環境変数を定義し、次にwithSecrets関数を使用して値にアクセスし、値を割り当てます。

スクリプト化された Jenkinsfile

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'

}

}

}

}

Jenkins パイプライン構文ヘルパー

[Jenkins パイプライン構文ヘルパーを](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload\&syntax-type=scripted\&jenkins-syntax=scripted-jenkinsfile\&install-type=freestyle-job\&pipeline-syntax=scripted#use-in-a-freestyle-job)使用してパイプライン スクリプトをCreateすることもできます。

#### **Use with environment variables** <a href="#id-8ugbv91uct0" id="id-8ugbv91uct0"></a>

Jenkins用の1Password Secretsプラグインを使うと、[環境変数を使うことができます](https://www.jenkins.io/doc/pipeline/tour/environment/#using-environment-variables)設定とシークレットを取得します。次の例は、Jenkins の 1Password Secrets プラグインで環境変数を使う方法を示しています。

ブロック内に環境変数を設定する必要がありますenvironment。次の表では、各環境変数について説明します。

![](https://4200399111-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8GVOyHz3wUSpdV3qlPwR%2Fuploads%2F2d6yHDzUeIJF4VI7H4lm%2F2.png?alt=media)

これらの環境変数を設定しない場合、1Password Secrets プラグインは[より広範囲の設定](https://developer.1password.com/docs/ci-cd/jenkins/?workflow-type=service-account\&plugin-install=direct-upload\&syntax-type=scripted\&jenkins-syntax=scripted-jenkinsfile\&install-type=freestyle-job\&pipeline-syntax=scripted#configuration-scope)を使用します。

**Service account**

次の例は、Jenkinsfile で環境変数を使用してサービス アカウントを使う方法を示しています。

**Declarative Jenkinsfile**

**Declarative Jenkinsfile**

**pipeline {**

**agent any**

**environment {**

**// Configure 1Password CLI and the service account.**

**OP\_SERVICE\_ACCOUNT\_TOKEN = credentials('service-account-token')**

**OP\_CLI\_PATH = 'op-cli-path'**

**// Define the environment variables using the secret reference \`op\://\<vault>/\<item>\[/section]/\<field>\`.**

**DOCKER\_USERNAME = 'op\://vault/item/username'**

**DOCKER\_PASSWORD = 'op\://vault/item/password'**

**}**

**stages{**

**stage('Push latest docker image') {**

**steps {**

**// Access 1Password secrets.**

**withSecrets() {**

**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'**

**}**

**}**

**}**

**}**

**}**

**}**

**Scripted Jenkinsfile**

**Scripted Jenkinsfile**

**node {**

**def environment = \[**

**// Configure 1Password CLI.**

**'OP\_CLI\_PATH = op-cli-path',**

**// Define the environment variables for the values of the secrets.**

**// Use the secret reference syntax: \`op\://\<vault>/\<item>\[/section]/\<field>\`.**

**'DOCKER\_USERNAME=op\://vault/item/username',**

**'DOCKER\_PASSWORD=op\://vault/item/password'**

**]**

**// Define the service account token.**

**def credentials = \[**

**string(credentialsId: 'service-account-token', variable: 'OP\_SERVICE\_ACCOUNT\_TOKEN')**

**]**

**withEnv(environment) {**

**withCredentials(credentials) {**

**stage('Push latest docker image') {**

**// Use the \`withSecrets\` function to access secrets.**

**withSecrets() {**

**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'**

**}**

**}**

**}**

**}**

**}**

**}**

**Connect server**

次の例は、Jenkinsfile で環境変数を使用して Connect サーバーを使う方法を示しています。

**Declarative Jenkinsfile**

Declarative Jenkinsfile

pipeline {

agent any

environment {

// Configure 1Password CLI and the Connect server.

OP\_CONNECT\_HOST = 'connect-host'

OP\_CONNECT\_TOKEN = credentials('connect-server-token')

OP\_CLI\_PATH = 'op-cli-path'

// Define the environment variables for the values of the secrets.

// Use the secret reference syntax: \`op\://\<vault>/\<item>\[/section]/\<field>\`.

DOCKER\_USERNAME = 'op\://vault/item/username'

DOCKER\_PASSWORD = 'op\://vault/item/password'

}

stages{

stage('Push latest docker image') {

steps {

// Use the \`withSecrets\` function to access secrets.

withSecrets() {

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'

}

}

}

}

}

}

**Scripted Jenkinsfile**

**Scripted Jenkinsfile**

**node {**

**def environment = \[**

**// Configure 1Password CLI and the Connect server.**

**'OP\_CONNECT\_HOST=connect-host',**

**'OP\_CLI\_PATH = op-cli-path',**

**// Define the environment variables for the values of the secrets.**

**// Use the secret reference syntax: \`op\://\<vault>/\<item>\[/section]/\<field>\`.**

**'DOCKER\_USERNAME=op\://vault/item/username',**

**'DOCKER\_PASSWORD=op\://vault/item/password'**

**]**

**def credentials = \[**

**string(credentialsId: 'connect-server-token', variable: 'OP\_CONNECT\_TOKEN')**

**]**

**withEnv(environment) {**

**withCredentials(credentials) {**

**stage('Push latest docker image') {**

**// Use the \`withSecrets\` function to access secrets.**

**withSecrets() {**

**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'**

**}**

**}**

**}**

**}**

**}**

**}**

#### **Use in a Freestyle Job** <a href="#f9fmg8kjhmhm" id="f9fmg8kjhmhm"></a>

Jenkins Pipeline構文ヘルパーを使用して[環境変数を設定できます](https://www.jenkins.io/doc/pipeline/tour/environment/#using-environment-variables)Freestyle ジョブでシークレットにアクセスします。

**Service account**

Jenkins パイプライン構文ヘルパーを使用して、Freestyle ジョブでサービス アカウントを使用します。

* ①Jenkins の Freestyle ジョブから、**Steps**セクションに移動します。
* ②**1Password CLI path**を1Password CLI をインストールした場所に設定します。\
  サンプル スクリプトでは/usr/local/bin/op を使用します。
* ③**Service Account Credential**をサービス アカウント トークンに設定します。
* ④**Add a 1Password secret**を選択します。
* ⑤シークレットの**Environment variable**を設定します。
* ⑥**Secret reference**を設定します。[シークレット参照構文<br>](https://developer.1password.com/docs/cli/secret-reference-syntax/)を使用して、1Password シークレットを指定します。
* ⑦**Generate Pipeline Script**を選択します。

**Connect server**

Jenkins パイプライン構文ヘルパーを使用して、Freestyle ジョブで Connect サーバーを使用します。

* ①Jenkins の Freestyle ジョブから、**Steps**セクションに移動します。
* ②**1Password CLI path**を1Password CLI をインストールした場所に設定します。\
  サンプル スクリプトでは /usr/local/bin/opを使用します。
* ③**Connect Host** をConnect サーバーの IP アドレスまたはホスト名に設定します。
* ④**Connect Credential**を Connect サーバー トークンに設定します。
* ⑤**Add a 1Password secret**を選択します。
* ⑥シークレットの**Environment variable**を設定します。
* ⑦**Secret reference**を設定します。[シークレット参照を<br>](https://developer.1password.com/docs/cli/secret-reference-syntax/)使用して1Password シークレットを指定します。
* ⑧**Generate Pipeline Script**の生Createを選択します。
