Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite git-clone as shell script #188

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 69 additions & 33 deletions git/git-clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,78 @@ spec:
description: The precise commit SHA that was fetched by this Task
steps:
- name: clone
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest
image: alpine/git:latest
script: |
#!/usr/bin/env sh
# Most errors handling are handled by the set -eux
chmouel marked this conversation as resolved.
Show resolved Hide resolved
set -eux
CHECKOUT_DIR="$(workspaces.output.path)/$(inputs.params.subdirectory)"
URL="$(inputs.params.url)"
REVISION="$(inputs.params.revision)"
SUBMODULES="$(inputs.params.submodules)"
SSLVERIFY="$(inputs.params.sslVerify)"
DEPTH="$(inputs.params.depth)"

cleandir() {
# Delete any existing contents of the repo directory if it exists.
#
# We don't just "rm -rf $CHECKOUT_DIR" because $CHECKOUT_DIR might be "/"
# or the root of a mounted volume.
if [[ -d "$CHECKOUT_DIR" ]] ; then
# Delete non-hidden files and directories
rm -rf "$CHECKOUT_DIR"/*
# Delete files and directories starting with . but excluding ..
rm -rf "$CHECKOUT_DIR"/.[!.]*
# Delete files and directories starting with .. plus any other character
rm -rf "$CHECKOUT_DIR"/..?*
fi
[[ "$(inputs.params.deleteExisting)" == "true" && -d ${CHECKOUT_DIR} ]] && {
find ${CHECKOUT_DIR}/|grep -v "^${CHECKOUT_DIR}/$"|xargs rm -rf
}

if [[ "$(inputs.params.deleteExisting)" == "true" ]] ; then
cleandir
fi
# https://git.io/JvBCV
ensureHomeEnv() {
local realhomedir=$(getent passwd $(whoami)|cut -f6 -d:)
if [[ ! -d ${realhomedir} ]];then
mkdir -p ${realhomedir}
fi

/ko-app/git-init \
-url "$(inputs.params.url)" \
-revision "$(inputs.params.revision)" \
-path "$CHECKOUT_DIR" \
-sslVerify "$(inputs.params.sslVerify)" \
-submodules "$(inputs.params.submodules)" \
-depth "$(inputs.params.depth)"
cd "$CHECKOUT_DIR"
RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')"
EXIT_CODE="$?"
if [ "$EXIT_CODE" != 0 ]
then
exit $EXIT_CODE
fi
# Make sure we don't add a trailing newline to the result!
echo -n "$RESULT_SHA" > $(results.commit.path)
if [[ ${realhomedir} == ${HOME} ]];then
return
fi

if [[ ! -e ${realhomedir}/.ssh && -w ${realhomedir}/.ssh ]];then
ln -svf ${HOME}/.ssh ${realhomedir}/.ssh
fi
}

# https://git.io/JvBCr
fetch() {
local revision=${REVISION:-master}
[[ -n ${CHECKOUT_DIR} ]] && {
mkdir -p ${CHECKOUT_DIR}
cd ${CHECKOUT_DIR}
}

git init .

trimmedURL=$(echo ${URL}|sed -e 's/^[ ]*//' -e 's/[ ]*$//')
chmouel marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also just curious ... wouldn't trimmedURL=$(echo $URL) work :D ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doesn't trim the spaces:

image

Copy link
Member

@sthaha sthaha Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chmouel are you using sh? or zsh?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chmouel you don't have to change the implementation, I was only curious ...

git remote add origin ${trimmedURL}

git config http.sslVerify ${SSLVERIFY}

fetchArgs="fetch"
if [[ ${DEPTH} > 0 ]];then
fetchArgs="${fetchArgs} --tags --depth=${DEPTH}"
fi

# Translating original go code from https://git.io/JvEtG
# original comment: Fetch can fail if an old commitid was used so try
# git pull, performing regardless of error as no guarantee that the
# same error is returned by all git servers gitlab, github etc...
if git ${fetchArgs} && git reset --hard ${revision};then
true
else
git reset --hard FETCH_HEAD
fi

if [[ ${SUBMODULES} == "true" || ${SUBMODULES} == "yes" ]];then
git submodule init
git submodule update --recursive
git submodule status
fi

echo "Successfully cloned ${trimmedURL} @ ${revision} in path $(readlink -f .)"
}

ensureHomeEnv
fetch

git rev-parse HEAD | tr -d '\n' > "$(results.commit.path)"
118 changes: 118 additions & 0 deletions git/tests/run.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-noargs
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-tag
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
- name: revision
value: 1.0.0

---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-no-submodules
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/githubtraining/example-dependency
- name: submodules
value: "false"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-no-depth-2
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
- name: depth
value: "2"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-sslverify-none
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
- name: sslVerify
value: "false"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-subdirectory
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
- name: subdirectory
value: "hellomoto"
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
name: git-clone-run-delete-existing
spec:
workspaces:
- name: output
emptyDir: {}
taskRef:
name: git-clone
inputs:
params:
- name: url
value: https://github.com/kelseyhightower/nocode
- name: deleteExisting
value: "true"
17 changes: 13 additions & 4 deletions test/e2e-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ function show_failure() {
echo "--- TR Dump"
kubectl get -n ${tns} tr -o yaml
echo "--- Container Logs"
kubectl get pod -o name -n ${tns}|xargs kubectl logs --all-containers -n ${tns}
kubectl get pod -o name -n ${tns}|sed 's,.*/,,'|xargs kubectl logs --all-containers -n ${tns}
exit 1

}

function test_task_creation() {
for runtest in ${@};do
local testname=${runtest%%/*}
Expand All @@ -91,7 +92,7 @@ function test_task_creation() {
done
[[ -n ${skipit} ]] && continue

kubectl create namespace ${tns}
kubectl get namespace ${tns} 2>/dev/null >/dev/null || kubectl create namespace ${tns}

# Install the task itself first
for yaml in ${testname}/*.yaml;do
Expand All @@ -116,9 +117,17 @@ function test_task_creation() {
status=$(kubectl get -n ${tns} tr --output=jsonpath='{.items[*].status.conditions[*].status}')
reason=$(kubectl get -n ${tns} tr --output=jsonpath='{.items[*].status.conditions[*].reason}')
[[ ${status} == *ERROR || ${reason} == *Failed || ${reason} == CouldntGetTask ]] && show_failure ${testname} ${tns}
[[ ${status} == True ]] && {

local good=true
# If one task of all the task is not True then exit
for st in ${status};do
[[ ${st} != True ]] && {
good=false
break
}
done
[[ ${good} == true ]] && {
echo -n "SUCCESS: ${testname} taskrun has successfully executed: " ;
kubectl get pod -o name -n ${tns}|xargs kubectl logs --all-containers -n ${tns}|tail -1
break
}
sleep 10
Expand Down