Skip to content

Commit f679489

Browse files
PhilippPlotnikovcf-ci-botmikhail-klimko
authored
[BE]:Add ssh support for git commit (#527)
* Add ssh support for git commit * Add comma * Change url * Change the way how to get private key * Update * Update * Refactor * Up version * Change image repository * Wip * Refactor * empty commit * empty commit * empty commit * empty commit * empty commit * empty commit * empty commit * Rename ssh folder * empty commit * empty commit * empty commit * empty commit * empty commit * empty commit * Update git commit version * Change use_ssh description * Wip * Wip * Refactor Co-authored-by: admin <[email protected]> Co-authored-by: Mikhail Klimko <[email protected]>
1 parent c8acdfb commit f679489

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

incubating/git-commit/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Moving to ubuntu instead of debian to solve high vulnerabilities
2+
FROM ubuntu:jammy-20221101
3+
4+
RUN apt-get update -y && \
5+
apt-get upgrade -y && \
6+
apt-get install git bash openssl busybox -y && \
7+
ln -s /bin/busybox /usr/bin/[[
8+
9+
# Add ssh record on which ssh key to use
10+
COPY ./ssh/ /root/.ssh/
11+
12+
# USER nodeuser
13+
RUN addgroup --gid 3000 nodegroup && \
14+
adduser --uid 3000 --ingroup nodegroup --shell /bin/sh --disabled-password nodeuser
15+
USER nodeuser

incubating/git-commit/ssh/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IdentityFile ~/.ssh/codefresh

incubating/git-commit/ssh/known_hosts

Whitespace-only changes.

incubating/git-commit/step.yaml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ kind: step-type
22
version: '1.0'
33
metadata:
44
name: git-commit
5-
version: 0.0.19
5+
version: 0.1.0
66
isPublic: true
77
description: Commit and push changes to repository
88
icon:
@@ -126,6 +126,11 @@ spec:
126126
"type": "boolean",
127127
"description": "pull remote changes with rebase flag before push",
128128
"default": false
129+
},
130+
"use_ssh": {
131+
"type": "boolean",
132+
"description": "Is use ssh or https (ssh key will be taken from git integration defined in git argument)",
133+
"default": false
129134
}
130135
}
131136
}
@@ -141,6 +146,8 @@ spec:
141146
- export GIT_ACCESS_TOKEN=$(codefresh get context $GIT_INTEGRATION_NAME --decrypt --prepare -o yaml | yq -r -c .spec.data.auth.password)
142147
- echo GIT_ACCESS_TOKEN=$GIT_ACCESS_TOKEN >> /meta/env_vars_to_export
143148
- export GIT_ACCESS_TOKEN_USER=$(codefresh get context $GIT_INTEGRATION_NAME --decrypt --prepare -o yaml | yq -r -c .spec.data.auth.username)
149+
- export PRIVATE_KEY=$(codefresh get context $GIT_INTEGRATION_NAME --decrypt --prepare -o yaml | yq .spec.data.auth.sshPrivateKey)
150+
- echo PRIVATE_KEY=$PRIVATE_KEY >> /meta/env_vars_to_export
144151
# If the git integration does not include the auth username, then default to the git_user_name argument
145152
- if [ "$GIT_ACCESS_TOKEN_USER" = "null" ]; then export GIT_ACCESS_TOKEN_USER=$GIT_USER_NAME; fi
146153
- echo GIT_ACCESS_TOKEN_USER=$GIT_ACCESS_TOKEN_USER >> /meta/env_vars_to_export
@@ -151,7 +158,7 @@ spec:
151158

152159
commit_and_push:
153160
title: "Commit and push"
154-
image: bitnami/git
161+
image: codefreshplugins/git-commit:0.1.0
155162
shell: bash
156163
environment:
157164
- REPO=${{repo}}
@@ -163,6 +170,8 @@ spec:
163170
- GPG_SECRET_KEY=${{gpg_secret_key}}
164171
- FORCE_PUSH=${{force_push}}
165172
- REBASE=${{rebase}}
173+
- USE_SSH=${{use_ssh}}
174+
- GIT_INTEGRATION_NAME=${{git}}
166175
commands:
167176
- |-
168177
if [[ -n ${GPG_KEY_ID} && -n ${GPG_SECRET_KEY} ]]; then
@@ -180,14 +189,34 @@ spec:
180189
- git add ${ADD_FILES}
181190
- git commit ${ALLOW_EMPTY} -m "${COMMIT_MESSAGE}"
182191
- git status
192+
- |-
193+
REPO_URL="https://$GIT_ACCESS_TOKEN_USER:$GIT_ACCESS_TOKEN@$GIT_FQDN/$REPO.git"
194+
if [ "$USE_SSH" = "true" ]; then
195+
[ -z "$PRIVATE_KEY" ] && (echo "missing PRIVATE_KEY var" | tee /dev/stderr) && exit 1
196+
echo ${PRIVATE_KEY:1:-1} | sed 's/\\n/\n/g' > ~/.ssh/codefresh
197+
chmod 0600 ~/.ssh/*
198+
chmod 0700 ~/.ssh/
199+
200+
# ssh://[email protected]:username/repo.git
201+
# match "github.com" from ssh uri
202+
REPO=${REPO#"ssh://"}
203+
SSH_HOST=$(echo "$REPO" | cut -d ":" -f 1 | cut -d "@" -f 2)
204+
echo "Adding "$SSH_HOST" to known_hosts"
205+
206+
# removes all keys belonging to hostname from a known_hosts file
207+
ssh-keygen -R $SSH_HOST 2>/dev/null
208+
209+
ssh-keyscan -H $SSH_HOST >> ~/.ssh/known_hosts 2> >(grep -v '^#' >&2)
210+
REPO_URL=$REPO
211+
fi
183212
- |-
184213
if [ "$REBASE" = true ]; then
185-
git pull --rebase "https://$GIT_ACCESS_TOKEN_USER:$GIT_ACCESS_TOKEN@$GIT_FQDN/$REPO.git"
214+
git pull --rebase $REPO_URL
186215
fi
187-
- echo git push "https://$GIT_ACCESS_TOKEN_USER:REDACTED@$GIT_FQDN/$REPO.git"
216+
- echo git push $REPO_URL
188217
- |-
189218
if [ "$FORCE_PUSH" = true ]; then
190-
git push --force "https://$GIT_ACCESS_TOKEN_USER:$GIT_ACCESS_TOKEN@$GIT_FQDN/$REPO.git"
219+
git push --force $REPO_URL
191220
else
192-
git push "https://$GIT_ACCESS_TOKEN_USER:$GIT_ACCESS_TOKEN@$GIT_FQDN/$REPO.git"
221+
git push $REPO_URL
193222
fi

0 commit comments

Comments
 (0)