Skip to content

Commit a1d9f5c

Browse files
author
lboix
committed
Better handling of private SSH key
1 parent c21b297 commit a1d9f5c

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-11
lines changed

Dockerfile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
FROM alpine:latest
22

3-
RUN apk add git openssh-client yq bash curl
3+
RUN apk add --no-cache git openssh yq bash curl
44

55
# TODO : put the email and name of the Git user that have access to your cluster repo, through the SSH key below
66
RUN git config --global user.email "your-git-username@email.com"
77
RUN git config --global user.name "your-git-username"
88

9-
RUN mkdir /root/.ssh
10-
COPY id_rsa* /root/.ssh/
11-
RUN chmod 400 /root/.ssh/id_rsa*
12-
# TODO : this line does assume this Git repo is on gitlab.com, so adapt accordingly
13-
RUN echo -e "Host gitlab.com\n\tStrictHostKeyChecking no" > /root/.ssh/config
9+
COPY ssh/prepare_ssh.sh /
10+
RUN chmod +x /prepare_ssh.sh
1411

1512
COPY main.sh /
16-
1713
RUN chmod +x /main.sh

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ It's really possible (I hope not ^^) that I "reinventend the wheel" here so if y
1414

1515
## Description of environment variables used by the current script
1616
- CLUSTER_GIT_CLONE_URL : the cluster Git repo URL to git clone (through SSH)
17+
- GIT_REPO_USER_RSA : the private SSH key linked to the user you are using to git clone your manifests repo
1718
- YAML_FILE_PATH : the path to the .yaml file containing the Deployment to update
1819
- NAMESPACE : the namespace belonging to your Deployment
1920
- DOCKER_REPO : the name of the registry repo of your image to deploy (*repo*/deployment-name:tag)
@@ -54,9 +55,8 @@ deploy:
5455
```
5556

5657
## Improvement ideas
57-
- Think about the best way to use a SSH key through a Dockerfile
5858
- Add snippets and templates for other tools
59-
- Add a retry scheme in the script : if your CI pipeline is launching parrallel deployment jobs (using this image), then some ones can fail with message `cannot lock ref 'refs/heads/master'` :
59+
- Add a retry scheme in the script : if your CI pipeline is launching parallel deployment jobs (using this image), then some ones can fail with message `cannot lock ref 'refs/heads/master'` :
6060
- I tried but it's more complicated that it sounds in Bash (any help or insight will be really appreciated)
6161
- _But_ there are two workarounds in Gitlab CI that you can use :
6262
- add a `- sleep 5` just before the `- /main.sh` line

id_rsa

Lines changed: 0 additions & 1 deletion
This file was deleted.

id_rsa.pub

Lines changed: 0 additions & 1 deletion
This file was deleted.

main.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ fi
3737
# exit 1
3838
# fi
3939

40+
/prepare_ssh.sh
41+
4042
git clone --depth=1 $CLUSTER_GIT_CLONE_URL
4143
# if needed :
4244
# cd some-folder/some-subfolder/

ssh/prepare_ssh.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
mkdir /root/.ssh
4+
echo "${GIT_REPO_USER_RSA}" > /root/.ssh/id_rsa
5+
chmod 400 /root/.ssh/id_rsa
6+
# TODO : this line does assume this Git repo is on gitlab.com, so adapt accordingly
7+
echo -e "Host gitlab.com\n\tStrictHostKeyChecking no" > /root/.ssh/config

0 commit comments

Comments
 (0)