5.3. ワークスペースを使用したシークレットの提供
ワークスペースを使用して、Git リポジトリーおよびコンテナーリポジトリー向けの認証シークレットを提供できます。
タスク名前付き Workspace を Task に設定し、ワークスペースのマウント先となるパスを指定できます。タスクを実行するときに、この名前のワークスペースとしてシークレットを指定します。OpenShift Pipelines がタスクを実行すると、シークレット内の情報がタスクで利用できるようになります。
ワークスペースを使用して認証シークレットを指定する場合は、シークレットのアノテーションは必要ありません。
5.3.1. ワークスペースを使用した Git の SSH 認証の設定 リンクのコピーリンクがクリップボードにコピーされました!
パイプラインが SSH キーで設定されたリポジトリーからリソースを取得するには、そのパイプラインの SSH ベースの認証を設定する必要があります。
パイプラインの SSH ベースの認証を設定するには、SSH 秘密鍵を使用して認証シークレットを作成し、タスクでこのシークレットの名前付きワークスペースを設定し、タスクの実行時にシークレットを指定します。
手順
次のコマンドを入力して、既存の
.sshディレクトリー内のファイルから Git SSH 認証シークレットを作成します。$ oc create secret generic my-github-ssh-credentials \1 --from-file=id_ed25519=/home/user/.ssh/id_ed25519 \2 --from-file=known_hosts=/home/user/.ssh/known_hosts3 タスク定義で、Git 認証用の名前付きワークスペース (例:
ssh-directory) を設定します。ワークスペースの定義例
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: git-clone spec: workspaces: - name: ssh-directory description: | A .ssh directory with private key, known_hosts, config, etc.-
タスクの手順では、
$(workspaces.<workspace_name>.path)環境変数のパス (例:$(workspaces.ssh-directory.path)) を使用してディレクトリーにアクセスします。 タスクを実行するときは、
tkn task startコマンドに--workspace引数を含めて、名前付きワークスペースのシークレットを指定します。$ tkn task start <task_name> --workspace name=<workspace_name>,secret=<secret_name>1 # ...- 1
<workspace_name>は、設定したワークスペースの名前に、<secret_name>は、作成したシークレットの名前に置き換えます。
認証に SSH 鍵を使用して Git リポジトリーをクローンするタスクの例
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: git-clone
spec:
workspaces:
- name: output
description: The git repo will be cloned onto the volume backing this Workspace.
- name: ssh-directory
description: |
A .ssh directory with private key, known_hosts, config, etc. Copied to
the user's home before git commands are executed. Used to authenticate
with the git remote when performing the clone. Binding a Secret to this
Workspace is strongly recommended over other volume types
params:
- name: url
description: Repository URL to clone from.
type: string
- name: revision
description: Revision to checkout. (branch, tag, sha, ref, etc...)
type: string
default: ""
- name: gitInitImage
description: The image providing the git-init binary that this Task runs.
type: string
default: "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.37.0"
results:
- name: commit
description: The precise commit SHA that was fetched by this Task.
- name: url
description: The precise URL that was fetched by this Task.
steps:
- name: clone
image: "$(params.gitInitImage)"
script: |
#!/usr/bin/env sh
set -eu
# This is necessary for recent version of git
git config --global --add safe.directory '*'
cp -R "$(workspaces.ssh-directory.path)" "${HOME}"/.ssh
chmod 700 "${HOME}"/.ssh
chmod -R 400 "${HOME}"/.ssh/*
CHECKOUT_DIR="$(workspaces.output.path)/"
/ko-app/git-init \
-url="$(params.url)" \
-revision="$(params.revision)" \
-path="${CHECKOUT_DIR}"
cd "${CHECKOUT_DIR}"
RESULT_SHA="$(git rev-parse HEAD)"
EXIT_CODE="$?"
if [ "${EXIT_CODE}" != 0 ] ; then
exit "${EXIT_CODE}"
fi
printf "%s" "${RESULT_SHA}" > "$(results.commit.path)"
printf "%s" "$(params.url)" > "$(results.url.path)"
- 1
- スクリプトは、シークレットの内容 (フォルダー形式) を
${HOME}/.sshにコピーします。これは、sshが認証情報を検索する標準フォルダーです。
タスクを実行するためのコマンドの例
$ tkn task start git-clone
--param url=git@github.com:example-github-user/buildkit-tekton
--workspace name=output,emptyDir=""
--workspace name=ssh-directory,secret=my-github-ssh-credentials
--use-param-defaults --showlog
5.3.2. ワークスペースを使用したコンテナーレジストリー認証の設定 リンクのコピーリンクがクリップボードにコピーされました!
パイプラインがレジストリーからコンテナーイメージを取得するには、そのレジストリーの認証を設定する必要があります。
コンテナーレジストリーの認証を設定するには、Docker 設定ファイルを使用して認証シークレットを作成し、タスクでこのシークレットの名前付きワークスペースを設定し、タスクの実行時にシークレットを指定します。
手順
次のコマンドを入力して、認証情報を含めて、既存の
config.jsonファイルからコンテナーレジストリー認証シークレットを作成します。$ oc create secret generic my-registry-credentials \1 --from-file=config.json=/home/user/credentials/config.json2 タスク定義で、Git 認証用の名前付きワークスペース (例:
ssh-directory) を設定します。ワークスペースの定義例
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: skopeo-copy spec: workspaces: - name: dockerconfig description: Includes a docker `config.json` # ...-
タスクの手順では、
$(workspaces.<workspace_name>.path)環境変数のパス (例:$(workspaces.dockerconfig.path)) を使用してディレクトリーにアクセスします。 タスクを実行するには、
tkn task startコマンドに--workspace引数を含めて、名前付きワークスペースのシークレットを指定します。$ tkn task start <task_name> --workspace name=<workspace_name>,secret=<secret_name>1 # ...- 1
<workspace_name>は、設定したワークスペースの名前に、<secret_name>は、作成したシークレットの名前に置き換えます。
Skopeo を使用してコンテナーリポジトリーからイメージをコピーするタスクの例
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: skopeo-copy
spec:
workspaces:
- name: dockerconfig
description: Includes a docker `config.json`
steps:
- name: clone
image: quay.io/skopeo/stable:v1.8.0
env:
- name: DOCKER_CONFIG
value: $(workspaces.dockerconfig.path)
script: |
#!/usr/bin/env sh
set -eu
skopeo copy docker://docker.io/library/ubuntu:latest docker://quay.io/example_repository/ubuntu-copy:latest
タスクを実行するためのコマンドの例
$ tkn task start skopeo-copy
--workspace name=dockerconfig,secret=my-registry-credentials
--use-param-defaults --showlog
5.3.3. ワークスペースを使用してシークレットを特定のステップのみに限定する手順 リンクのコピーリンクがクリップボードにコピーされました!
ワークスペースを使用して認証シークレットを提供し、タスクでワークスペースを定義すると、デフォルトでは、そのワークスペースはタスク内のすべてのステップで使用できるようになります。
シークレットを特定のステップに制限するには、タスク仕様とステップ仕様の両方でワークスペースを定義します。
手順
次の例のように、タスク仕様とステップ仕様の両方の下に、
workspaces:定義を追加します。1 つのステップのみが認証情報ワークスペースにアクセスできるタスク定義の例
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: git-clone-build spec: workspaces:1 - name: ssh-directory description: | A .ssh directory with private key, known_hosts, config, etc. # ... steps: - name: clone workspaces:2 - name: ssh-directory # ... - name: build3 # ...