diff --git a/Dockerfile.windows b/Dockerfile.windows new file mode 100644 index 0000000..cc55b70 --- /dev/null +++ b/Dockerfile.windows @@ -0,0 +1,36 @@ +ARG OS_RELEASE + +FROM mcr.microsoft.com/windows/servercore:${OS_RELEASE} + +SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"] + +RUN $url = 'https://cygwin.com/setup-x86_64.exe'; \ + Write-Host ('Downloading {0} ...' -f $url); \ + Invoke-WebRequest -Uri $url -OutFile 'C:/setup-x86_64.exe'; \ + \ + Write-Host 'Installing ...'; \ + New-Item -ItemType directory -Path 'C:/tmp'; \ + Start-Process "C:/setup-x86_64.exe" -NoNewWindow -Wait -PassThru -ArgumentList @('-q','-v','-n','-B','-R','C:/cygwin64','-l','C:/tmp','-s','http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/circa/64bit/2019/03/06/161558','-X','-P','default,git,openssl'); \ + \ + Write-Host 'Removing ...'; \ + Remove-Item -Path 'C:/tmp' -Force -Recurse -ErrorAction Ignore; \ + \ + Write-Host 'Verifying install ...'; \ + Start-Process "C:/cygwin64/bin/cygcheck.exe" -NoNewWindow -Wait -PassThru -ArgumentList @('-c'); \ + \ + Write-Host 'Complete.'; + +RUN $ACL=Get-Acl -Path 'C:\'; \ + Set-Acl -Path 'C:\cygwin64\' -AclObject $ACL + +SHELL ["C:/cygwin64/bin/sh", "-l", "-c"] + +COPY .ssh/ C:/cygwin64/.ssh/ +COPY ./start.sh C:/cygwin64/start.sh + +RUN 'sed -i "s/\r$//" /start.sh' + +LABEL owner="codefresh.io" + +ENTRYPOINT ["C:/cygwin64/bin/sh", "-l", "-c"] +CMD ["/start.sh"] \ No newline at end of file diff --git a/start.sh b/start.sh index c20879f..217bc31 100755 --- a/start.sh +++ b/start.sh @@ -20,7 +20,7 @@ git_retry () { if [[ $EXIT_CODE == 0 ]]; then break elif [[ $EXIT_CODE == "$RETRY_ON_SIGNAL" ]]; then - echo "$COMMAND failed with Exit Code $EXIT_CODE - try $TRY_NUM " + echo "Failed with Exit Code $EXIT_CODE - try $TRY_NUM " TRY_NUM=$(( ${TRY_NUM} + 1 )) sleep $RETRY_WAIT else @@ -34,14 +34,18 @@ git_retry () { trap exit_trap EXIT set -e +WORKING_DIRECTORY="C:${WORKING_DIRECTORY}" + [ -z "$REVISION" ] && (echo "missing REVISION var" | tee /dev/stderr) && exit 1 -echo "$PRIVATE_KEY" > /root/.ssh/codefresh -chmod 700 ~/.ssh/ -chmod 600 ~/.ssh/* +echo "$PRIVATE_KEY" | tr -d '\n' > /.ssh/codefresh +chmod 700 /.ssh/ +chmod 600 /.ssh/* +mkdir -p "$WORKING_DIRECTORY" cd $WORKING_DIRECTORY +HOME=/home git config --global advice.detachedhead false git config --global credential.helper "/bin/sh -c 'echo username=$USERNAME; echo password=$PASSWORD'" @@ -52,29 +56,43 @@ if [ -d "$CLONE_DIR" ]; then echo "Preparing to update $REPO" cd $CLONE_DIR - # Reset the remote URL because the embedded user token may have changed - git remote set-url origin $REPO + # Make sure the CLONE_DIR folder is a git folder + if git status &> /dev/null ; then + # Reset the remote URL because the embedded user token may have changed + git remote set-url origin $REPO - echo "Cleaning up the working directory" - git reset -q --hard - git clean -df - git gc - git_retry git remote prune origin + echo "Cleaning up the working directory" + git reset -q --hard + git clean -df + git_retry git remote prune origin - echo "Fetching the updates from origin" - git_retry git fetch --tags + echo "Fetching the updates from origin" + git_retry git fetch --tags - if [ -n "$REVISION" ]; then + if [ -n "$REVISION" ]; then - echo "Updating $REPO to revision $REVISION" - git checkout $REVISION + echo "Updating $REPO to revision $REVISION" + git checkout $REVISION - CURRENT_BRANCH="`git branch 2>/dev/null | grep '^*' | cut -d' ' -f2-`" + CURRENT_BRANCH="`git branch 2>/dev/null | grep '^*' | cut -d' ' -f2-`" - # If the revision is identical to the current branch we can rebase it with the latest changes. This isn't needed when running detached - if [ "$REVISION" == "$CURRENT_BRANCH" ]; then - echo 'Rebasing current branch $REVISION to latest changes...' - git rebase + # If the revision is identical to the current branch we can rebase it with the latest changes. This isn't needed when running detached + if [ "$REVISION" == "$CURRENT_BRANCH" ]; then + echo 'Rebasing current branch $REVISION to latest changes...' + git rebase + fi + fi + else + # The folder already exists but it is not a git repository + # Clean folder and clone a fresh copy on current directory + cd .. + rm -rf $CLONE_DIR + echo "cloning $REPO" + git_retry git clone $REPO $CLONE_DIR + cd $CLONE_DIR + + if [ -n "$REVISION" ]; then + git checkout $REVISION fi fi else @@ -87,4 +105,4 @@ else if [ -n "$REVISION" ]; then git checkout $REVISION fi -fi +fi \ No newline at end of file