From ad14aecdb42bfea72bb9e3dd72f156df19d19693 Mon Sep 17 00:00:00 2001 From: Alexander Brandstedt Date: Tue, 13 Jul 2021 09:51:40 +0200 Subject: [PATCH] adding option to pass git credential --- action.yml | 7 +++++++ dist/entrypoint.sh | 2 +- dist/steps/set_gitcredential.sh | 36 +++++++++++++++++++++++++++++++++ src/model/__mocks__/input.ts | 1 + src/model/build-parameters.ts | 2 ++ src/model/docker.ts | 2 ++ src/model/input.ts | 4 ++++ 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100755 dist/steps/set_gitcredential.sh diff --git a/action.yml b/action.yml index a0917501..75d168f8 100644 --- a/action.yml +++ b/action.yml @@ -110,6 +110,13 @@ inputs: required: false default: '' description: 'SSH Agent path to forward to the container' + gitCredential: + required: false + default: '' + description: > + Git credential configuration + + example: https://$token@github.com/ chownFilesTo: required: false default: '' diff --git a/dist/entrypoint.sh b/dist/entrypoint.sh index d3dfe655..04788831 100755 --- a/dist/entrypoint.sh +++ b/dist/entrypoint.sh @@ -3,7 +3,7 @@ # # Run steps # - +source /steps/set_gitcredential.sh source /steps/activate.sh source /steps/build.sh source /steps/return_license.sh diff --git a/dist/steps/set_gitcredential.sh b/dist/steps/set_gitcredential.sh new file mode 100755 index 00000000..fdacff39 --- /dev/null +++ b/dist/steps/set_gitcredential.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +set_config_home() { + if [ -z "${XDG_CONFIG_HOME}" ] + then + mkdir -p "${HOME}/.config" + export XDG_CONFIG_HOME="${HOME}/.config" + fi + + mkdir -p "${XDG_CONFIG_HOME}/git" + +} + +configure_git_credentials() { + echo "${GIT_CREDENTIAL}" >> "${XDG_CONFIG_HOME}/git/credentials" + chmod 0600 "${XDG_CONFIG_HOME}/git/credentials" + + git config --global credential.helper store + git config --global --replace-all url.https://github.com/.insteadOf ssh://git@github.com/ + git config --global --add url.https://github.com/.insteadOf git@github.com +} + +if [ -z "${GIT_CREDENTIAL}" ] +then + echo "GIT_CREDENTIAL unset skipping" +else + echo "GIT_CREDENTIAL is set configuring git credentials" + set_config_home + configure_git_credentials +fi + + + + + + diff --git a/src/model/__mocks__/input.ts b/src/model/__mocks__/input.ts index 33f1091a..12a8aaa3 100644 --- a/src/model/__mocks__/input.ts +++ b/src/model/__mocks__/input.ts @@ -12,6 +12,7 @@ export const mockGetFromUser = jest.fn().mockResolvedValue({ customParameters: '', sshAgent: '', chownFilesTo: '', + gitCredential: '', }); export default { diff --git a/src/model/build-parameters.ts b/src/model/build-parameters.ts index e3a48fad..26c07655 100644 --- a/src/model/build-parameters.ts +++ b/src/model/build-parameters.ts @@ -23,6 +23,7 @@ class BuildParameters { public androidKeyaliasPass!: string; public customParameters!: string; public sshAgent!: string; + public gitCredential!: string; public remoteBuildCluster!: string; public awsStackName!: string; public kubeConfig!: string; @@ -62,6 +63,7 @@ class BuildParameters { androidKeyaliasPass: Input.androidKeyaliasPass, customParameters: Input.customParameters, sshAgent: Input.sshAgent, + gitCredential: Input.gitCredential, chownFilesTo: Input.chownFilesTo, remoteBuildCluster: Input.remoteBuildCluster, awsStackName: Input.awsStackName, diff --git a/src/model/docker.ts b/src/model/docker.ts index d0f8915f..0735dd8d 100644 --- a/src/model/docker.ts +++ b/src/model/docker.ts @@ -37,6 +37,7 @@ class Docker { androidKeyaliasPass, customParameters, sshAgent, + gitCredential, chownFilesTo, } = parameters; @@ -80,6 +81,7 @@ class Docker { --env RUNNER_TOOL_CACHE \ --env RUNNER_TEMP \ --env RUNNER_WORKSPACE \ + ${gitCredential ? '--env GIT_CREDENTIAL': ''} \ ${sshAgent ? '--env SSH_AUTH_SOCK=/ssh-agent' : ''} \ --volume "/var/run/docker.sock":"/var/run/docker.sock" \ --volume "${runnerTempPath}/_github_home":"/root" \ diff --git a/src/model/input.ts b/src/model/input.ts index 5c5b8667..6c966253 100644 --- a/src/model/input.ts +++ b/src/model/input.ts @@ -89,6 +89,10 @@ class Input { return core.getInput('sshAgent') || ''; } + static get gitCredential() { + return core.getInput('gitCredential') || ''; + } + static get chownFilesTo() { return core.getInput('chownFilesTo') || ''; }