@@ -2,7 +2,7 @@ kind: step-type
2
2
version : ' 1.0'
3
3
metadata :
4
4
name : git-commit
5
- version : 0.0.19
5
+ version : 0.1.0
6
6
isPublic : true
7
7
description : Commit and push changes to repository
8
8
icon :
@@ -126,6 +126,11 @@ spec:
126
126
"type": "boolean",
127
127
"description": "pull remote changes with rebase flag before push",
128
128
"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
129
134
}
130
135
}
131
136
}
@@ -141,6 +146,8 @@ spec:
141
146
- export GIT_ACCESS_TOKEN=$(codefresh get context $GIT_INTEGRATION_NAME --decrypt --prepare -o yaml | yq -r -c .spec.data.auth.password)
142
147
- echo GIT_ACCESS_TOKEN=$GIT_ACCESS_TOKEN >> /meta/env_vars_to_export
143
148
- 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
144
151
# If the git integration does not include the auth username, then default to the git_user_name argument
145
152
- if [ "$GIT_ACCESS_TOKEN_USER" = "null" ]; then export GIT_ACCESS_TOKEN_USER=$GIT_USER_NAME; fi
146
153
- echo GIT_ACCESS_TOKEN_USER=$GIT_ACCESS_TOKEN_USER >> /meta/env_vars_to_export
@@ -151,7 +158,7 @@ spec:
151
158
152
159
commit_and_push :
153
160
title : " Commit and push"
154
- image : bitnami /git
161
+ image : codefreshplugins /git-commit:0.1.0
155
162
shell : bash
156
163
environment :
157
164
- REPO=${{repo}}
@@ -163,6 +170,8 @@ spec:
163
170
- GPG_SECRET_KEY=${{gpg_secret_key}}
164
171
- FORCE_PUSH=${{force_push}}
165
172
- REBASE=${{rebase}}
173
+ - USE_SSH=${{use_ssh}}
174
+ - GIT_INTEGRATION_NAME=${{git}}
166
175
commands :
167
176
- |-
168
177
if [[ -n ${GPG_KEY_ID} && -n ${GPG_SECRET_KEY} ]]; then
@@ -180,14 +189,34 @@ spec:
180
189
- git add ${ADD_FILES}
181
190
- git commit ${ALLOW_EMPTY} -m "${COMMIT_MESSAGE}"
182
191
- 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
183
212
- |-
184
213
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
186
215
fi
187
- - echo git push "https://$GIT_ACCESS_TOKEN_USER:REDACTED@$GIT_FQDN/$REPO.git"
216
+ - echo git push $REPO_URL
188
217
- |-
189
218
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
191
220
else
192
- git push "https://$GIT_ACCESS_TOKEN_USER:$GIT_ACCESS_TOKEN@$GIT_FQDN/$REPO.git"
221
+ git push $REPO_URL
193
222
fi
0 commit comments