Skip to content

Commit 08e1800

Browse files
adding support to pass git token directly to github-release step' (#560)
* adding support to pass git token directly to `github-release` step'
1 parent 9b0ba55 commit 08e1800

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

incubating/github-release/run.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -o pipefail
44

5+
FORBID_DECRYPT_MSG="Decrypting contexts is not allowed because 'forbidDecrypt' feature is enabled"
6+
57
bold() { echo -e "\e[1m$@\e[0m" ; }
68
red() { echo -e "\e[31m$@\e[0m" ; }
79
green() { echo -e "\e[32m$@\e[0m" ; }
@@ -29,7 +31,15 @@ ALL_VARS=(`echo "${REQUIRED_VARS[@]}"` `echo "${OPTIONAL_VARS[@]}"`)
2931

3032
function getTokenFromContext() {
3133
bold "Getting a git token from the context \"${GIT_CONTEXT}\"..."
32-
GITHUB_TOKEN=$(codefresh get contexts --type git.github $1 --decrypt -o json | jq -r '.spec.data.auth.password') || return 1
34+
GITHUB_TOKEN_RESPONSE=$(codefresh get contexts --type git.github $1 --decrypt -o json 2>&1)
35+
if [ $? != 0 ]; then
36+
red "Failed to get the git token from context \"${GIT_CONTEXT}\" with error: ${GITHUB_TOKEN_RESPONSE}"
37+
if [ -z "${GITHUB_TOKEN_RESPONSE##*$FORBID_DECRYPT_MSG*}" ]; then
38+
yellow "You should use the 'git_token' argument to pass a github token"
39+
fi
40+
return 1
41+
fi
42+
GITHUB_TOKEN=$(echo ${GITHUB_TOKEN_RESPONSE} | jq -r '.spec.data.auth.password')
3343
export GITHUB_TOKEN
3444
ok
3545
}
@@ -49,6 +59,9 @@ function checkTrigger() {
4959
}
5060

5161
function setDefaultVarValues() {
62+
if [ ! -z "$GITHUB_TOKEN" ]; then
63+
GIT_CONTEXT="_" # This is irrelevant, we are using GITHUB_TOKEN
64+
fi
5265

5366
if [ -z "$GIT_CONTEXT" ]; then
5467
yellow "GIT_CONTEXT var is not set explicitly. Trying to get it from the trigger by default..."
@@ -117,7 +130,10 @@ function main() {
117130
setDefaultVarValues
118131
[ $? != 0 ] && red "Failed to set default value for one of the required variables, exiting..." && return 1
119132

120-
getTokenFromContext $GIT_CONTEXT
133+
if [ -z "$GITHUB_TOKEN" ]; then
134+
getTokenFromContext $GIT_CONTEXT
135+
[ $? != 0 ] && return 1
136+
fi
121137

122138
validateReqVars
123139

incubating/github-release/step.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '1.0'
33
metadata:
44
name: github-release
55
title: Create a GitHub release
6-
version: 1.1.5
6+
version: 1.1.6
77
isPublic: true
88
description: Create a GitHub release.
99
sources:
@@ -38,6 +38,19 @@ metadata:
3838
files:
3939
- ${{CF_VOLUME_PATH}}/${{CF_REPO_NAME}}/my-file1
4040
- ${{CF_VOLUME_PATH}}/${{CF_REPO_NAME}}/my-file2
41+
- description: git-token
42+
workflow:
43+
github_release:
44+
type: github-release
45+
title: Using git token example
46+
description: |-
47+
You can also pass a git token explicitly using the git_token
48+
argument. This is useful when you don't want to use a git-context
49+
or when the 'forbidDecrypt' feature turned on.
50+
arguments:
51+
git_token: ${{GITHUB_TOKEN}}
52+
release_name: my-release
53+
release_tag: my-tag-0.1.0
4154
- description: full
4255
workflow:
4356
github_release:
@@ -95,6 +108,11 @@ spec:
95108
"type": "string",
96109
"description": "Codefresh Git Context to be used by the plugin. Must be a github git context. If empty, it will try to get the context from the pipeline trigger"
97110
},
111+
"git_token": {
112+
"name": "git token",
113+
"type": "string",
114+
"description": "Git token to use for authentication to github. If empty, we will try to get the token from your Codefresh Git Context specified in `git_context_name`"
115+
},
98116
"release_tag": {
99117
"type": "string",
100118
"description": "Git tag to create a release"
@@ -142,6 +160,7 @@ spec:
142160
image: codefresh/github-release
143161
environment:
144162
- 'GIT_CONTEXT=${{git_context_name}}'
163+
- 'GITHUB_TOKEN=${{git_token}}'
145164
- 'CF_BRANCH_TAG_NORMALIZED=${{CF_BRANCH_TAG_NORMALIZED}}'
146165
- 'CF_REPO_OWNER=${{CF_REPO_OWNER}}'
147166
- 'CF_REPO_NAME=${{CF_REPO_NAME}}'

0 commit comments

Comments
 (0)