You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clarify usage for Docker build processes, especially with deployment keys (#145)
The current docs mention only `docker/build-push-action` in conjunction
with deploy keys.
This might mislead users to believe, that this only applies to said
Action. But the concept applies to all workflows that somehow use
`docker build` with deploy keys.
This PR clarifies the relevant section.
Co-authored-by: Matthias Pigulla <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+25-13Lines changed: 25 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -112,9 +112,11 @@ If you know that your favorite tool or platform of choice requires extra tweaks
112
112
113
113
If you are using this action on container-based workflows, make sure the container has the necessary SSH binaries or package(s) installed.
114
114
115
-
### Using the `docker/build-push-action` Action
115
+
### Building Docker Images and/or Using the `docker/build-push-action` Action
116
116
117
-
If you are using the `docker/build-push-action`, and would like to pass the SSH key, you can do so by adding the following config to pass the socket file through:
117
+
When you are building Docker images with `docker build` or `docker compose build` and need to provide the SSH keys to the build, don't forget to pass `--ssh default=${{ env.SSH_AUTH_SOCK }}` on the command line to pass the SSH agent socket through. See the [Docker documentation](https://docs.docker.com/engine/reference/commandline/buildx_build/#ssh) for more information on this option.
118
+
119
+
If you are using the `docker/build-push-action`, you can do so by adding the following config.
118
120
119
121
```yml
120
122
- name: Build and push
@@ -125,35 +127,45 @@ If you are using the `docker/build-push-action`, and would like to pass the SSH
125
127
default=${{ env.SSH_AUTH_SOCK }}
126
128
```
127
129
128
-
### Using the `docker/build-push-action` Action together with multiple Deploy Keys
130
+
Make sure not to miss the next section, though.
131
+
132
+
### Using Multiple Deploy Keys Inside Docker Builds
129
133
130
-
If you use the `docker/build-push-action` and want to use multiple GitHub deploy keys, you need to copy the git and ssh configuration to the container during the build. Otherwise, the Docker build process would still not know how to handle multiple deploy keys. Even if the ssh agent was set up correctly on the runner.
134
+
When you pass the SSH agent socket to the Docker build environment _and_ want to use multiple GitHub deploy keys, you need to copy the Git and SSH configuration files to the build environment as well. This is necessary _in addition to_ forwarding the SSH agent socket into the build process. The config files are required so that Git can pick the right one from your deployment keys.
131
135
132
-
This requires an additional step in the actions workflow and two additional lines in the Dockerfile.
136
+
This requires an additional step in the workflow file **after** the `ssh-agent` step and **before** the Docker build step. You also need two additional lines in the `Dockerfile` to actually copy the configs.
137
+
138
+
The following example will:
139
+
* collect the necessary Git and SSH configuration files in a directory that must be part of the Docker build context so that...
140
+
* ... the files can be copied into the Docker image (or an intermediate build stage).
133
141
134
142
Workflow:
143
+
135
144
```yml
136
-
- name: Prepare git and ssh config for build context
145
+
- name: ssh-agent setup
146
+
...
147
+
148
+
- name: Collect Git and SSH config files in a directory that is part of the Docker build context
137
149
run: |
138
150
mkdir root-config
139
151
cp -r ~/.gitconfig ~/.ssh root-config/
140
152
141
-
- name: Build and push
142
-
id: docker_build
143
-
uses: docker/build-push-action@v2
144
-
with:
145
-
ssh: |
146
-
default=${{ env.SSH_AUTH_SOCK }}
153
+
- name: Docker build
154
+
# build-push-action | docker [compose] build | etc.
155
+
...
147
156
```
148
157
149
158
Dockerfile:
159
+
150
160
```Dockerfile
161
+
# Copy the two files in place and fix different path/locations inside the Docker image
151
162
COPY root-config /root/
152
163
RUN sed 's|/home/runner|/root|g' -i.bak /root/.ssh/config
153
164
```
154
165
155
-
Have in mind that the Dockerfile now contains customized git and ssh configurations. If you don't want that in your final image, use multi-stage builds.
166
+
Keep in mind that the resulting Docker image now might contain these customized Git and SSH configuration files! Your private SSH keys are never written to files anywhere, just loaded into the SSH agent and forwarded into the container. The config files might, however, give away details about your build or development process and contain the names and URLs of your (private) repositories. You might want to use a multi-staged build to make sure these files do not end up in the final image.
156
167
168
+
If you still get the error message: `fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.`, you most likely forgot one of the steps above.
157
169
158
170
### Cargo's (Rust) Private Dependencies on Windows
0 commit comments