Skip to content

Add reusable guide for SSH key usage during Docker builds #10918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/continuous-integration/shared/ssh-during-docker-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Using SSH Keys During Docker Builds

If your Dockerfile needs to perform SSH-based operations — such as cloning private Git repositories or connecting to internal services — you can securely forward an SSH key into the build using Docker BuildKit's `--ssh` feature. This allows access to sensitive resources without baking credentials into your image.

> The examples below use GitHub as the SSH target, but the approach works with any SSH-based service, including internal Git servers, artifact sources, or deployment infrastructure.

### 1. Enable SSH mount in your Dockerfile

At the top of your Dockerfile, enable BuildKit features:

```Dockerfile
# syntax=docker/dockerfile:1.2
...
```
Then use the `--mount=type=ssh` instruction with a named ID:

```Dockerfile
...
RUN --mount=type=ssh,id=github_ssh git clone [email protected]:your-org/private-repo.git
...
```
:::note
The `id` in `--mount=type=ssh,id=...` is just a label — it can be anything, but must match what’s passed via `PLUGIN_BUILDX_OPTIONS`.
:::

### 2. Configure your Build and Push step
Set the `PLUGIN_BUILDX_OPTIONS` environment variable to pass the SSH option:

```yaml
- step:
identifier: BuildAndPushDockerRegistry
type: BuildAndPushDockerRegistry
name: Build and Push Image
spec:
connectorRef: account.harnessImage
repo: ghcr.io/my-org/my-service
tags:
- latest
caching: true
envVariables:
PLUGIN_BUILDX_OPTIONS: "--ssh=github_ssh=id_rsa"
```

### 3. Reference the SSH key from Harness Secrets Manager

Mount the SSH key by referencing it from Secrets Manager using the same key name (`id_rsa`):

```yaml
secrets:
id_rsa: <+secrets.getValue("github_deploy_key")>
```

This setup allows your Docker build to securely perform SSH-based operations — such as cloning private Git repositories or connecting to internal services — without exposing credentials in your Docker image or scripts.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ redirect_from:
---

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

This topic explains how to configure the **Build and Push to ACR** step in a Harness CI pipeline. This step is used to build and push to [Azure Container Registry (ACR)](https://azure.microsoft.com/en-us/products/container-registry).

Expand Down Expand Up @@ -186,6 +187,8 @@ You can find the following settings on the **Advanced** tab in the step settings
- [Failure Strategy](/docs/platform/pipelines/failure-handling/define-a-failure-strategy-on-stages-and-steps): Control what happens to your pipeline when a step fails.
- [Use looping strategies](/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Define a matrix, repeat, or parallelism strategy for an individual step.

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ redirect_from:

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import Tar from '/docs/continuous-integration/shared/build-and-push-local-tar.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

This topic explains how to use the [Build and Push an image to Docker Registry step](./build-and-push-to-docker-registry.md) to build and push an image to [JFrog Artifactory](https://www.jfrog.com/confluence/display/JFROG/JFrog+Artifactory) Docker registries.

Expand Down Expand Up @@ -242,6 +243,8 @@ Settings:

[Plugin on Dockerhub](https://hub.docker.com/r/plugins/artifactory-publish-docker-buildinfo/tags)

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ canonical_url: https://www.harness.io/blog/docker-multi-stage-build
---

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

This topic explains how to configure the **Build and Push an image to Docker Registry** step in a Harness CI pipeline. This step creates a Docker image from a [Dockerfile](https://docs.docker.com/engine/reference/builder/) and pushes it to a Docker registry. This is one of several options for [building and pushing artifacts in Harness CI](/docs/continuous-integration/use-ci/build-and-upload-artifacts/build-and-upload-an-artifact).

Expand Down Expand Up @@ -200,11 +201,12 @@ You can find the following settings on the **Advanced** tab in the step settings
- [Failure Strategy](/docs/platform/pipelines/failure-handling/define-a-failure-strategy-on-stages-and-steps): Control what happens to your pipeline when a step fails.
- [Use looping strategies](/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Define a matrix, repeat, or parallelism strategy for an individual step.

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:


- [Why do I encounter an authentication error when using V2 API](/kb/continuous-integration/continuous-integration-faqs/#why-build-and-push-steps-dont-support-v2-api-urls)
- [What drives the Build and Push steps? What is kaniko?](/kb/continuous-integration/continuous-integration-faqs/#what-drives-the-build-and-push-steps-what-is-kaniko)
- [Does a kaniko build use images cached locally on the node? Can I enable caching for kaniko?](/kb/continuous-integration/continuous-integration-faqs/#does-a-kaniko-build-use-images-cached-locally-on-the-node-can-i-enable-caching-for-kaniko)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ redirect_from:
---

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

[Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) is a fully managed service from AWS that you can use to store and manage Docker images securely and reliably. In addition, ECR provides a simple web-based interface for creating, managing, and sharing Docker images and integrating them with other AWS services. For more information, go to the AWS documentation on [Pushing a Docker image](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html).

Expand Down Expand Up @@ -248,6 +249,8 @@ You can find the following settings on the **Advanced** tab in the step settings
- [Failure Strategy](/docs/platform/pipelines/failure-handling/define-a-failure-strategy-on-stages-and-steps): Control what happens to your pipeline when a step fails.
- [Use looping strategies](/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Define a matrix, repeat, or parallelism strategy for an individual step.

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ redirect_from:
---

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

This topic explains how to configure the **Build and Push to GAR** step in a Harness CI pipeline. This step is used to build and push to [Google Artifact Registry (GAR)](https://cloud.google.com/artifact-registry).

Expand Down Expand Up @@ -194,6 +195,8 @@ You can find the following settings on the **Advanced** tab in the step settings
- [Failure Strategy](/docs/platform/pipelines/failure-handling/define-a-failure-strategy-on-stages-and-steps): Control what happens to your pipeline when a step fails.
- [Use looping strategies](/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Define a matrix, repeat, or parallelism strategy for an individual step.

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ redirect_from:
---

import Flags from '/docs/continuous-integration/shared/build-and-push-runtime-flags.md';
import SshOps from '/docs/continuous-integration/shared/ssh-during-docker-build.md';

This topic explains how to use the [Build and Push an image to Docker Registry step](./build-and-push-to-docker-registry.md) to build and push an image to [GitHub Container Registry](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry).

Expand Down Expand Up @@ -184,6 +185,8 @@ You can find the following settings on the **Advanced** tab in the step settings
- [Failure Strategy](/docs/platform/pipelines/failure-handling/define-a-failure-strategy-on-stages-and-steps): Control what happens to your pipeline when a step fails.
- [Use looping strategies](/docs/platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism): Define a matrix, repeat, or parallelism strategy for an individual step.

<SshOps/>

## Troubleshoot Build and Push steps

Go to the [CI Knowledge Base](/kb/continuous-integration/continuous-integration-faqs) for questions and issues related to building and pushing images, such as:
Expand Down