Skip to content

Instantly share code, notes, and snippets.

@quyse
Last active December 19, 2015 10:52
Show Gist options
  • Save quyse/7178ec5737235bdf60bf to your computer and use it in GitHub Desktop.
Save quyse/7178ec5737235bdf60bf to your computer and use it in GitHub Desktop.
script for caching haskell-stack's artifacts on appveyor
@echo off
setlocal ENABLEEXTENSIONS
:: GRUMPY_ARTIFACT_REPO_URL - link to artifact git repo (without starting https:// !!! just something like github.com/user/repo. https:// will be prepended automatically)
:: GRUMPY_ARTIFACT_REPO_BRANCH (optional) - branch in this repo
:: GRUMPY_ARTIFACT_REPO_PATH - local path of the repo
:: GRUMPY_ARTIFACT_REPO_USERNAME - username for the artifact repo
:: GRUMPY_ARTIFACT_REPO_PASSWORD - password for the artifact repo
:: GRUMPY_ARTIFACT_REPO_USER_EMAIL - email for putting into commit
:: STACK_ROOT - should be set for stack
if "%GRUMPY_ARTIFACT_REPO_URL%"=="" (
echo %~n0: GRUMPY_ARTIFACT_REPO_URL is not set
exit /b 1
)
if "%GRUMPY_ARTIFACT_REPO_PATH%"=="" (
echo %~n0: GRUMPY_ARTIFACT_REPO_PATH is not set
exit /b 1
)
if "%GRUMPY_ARTIFACT_REPO_USERNAME"=="" (
echo %~n0: GRUMPY_ARTIFACT_REPO_USERNAME is not set
exit /b 1
)
if "%GRUMPY_ARTIFACT_REPO_PASSWORD%"=="" (
echo %~n0: GRUMPY_ARTIFACT_REPO_PASSWORD is not set
exit /b 1
)
if "%STACK_ROOT%"=="" (
echo %~n0: STACK_ROOT is not set
exit /b 1
)
if "%GRUMPY_ARTIFACT_REPO_BRANCH%" NEQ "" set GRUMPY_ARTIFACT_REPO_BRANCH=-b %GRUMPY_ARTIFACT_REPO_BRANCH%
goto :%1
:init
:: clone artifact repo
echo %~n0: cloning artifact repo...
git clone -q --depth 1 --single-branch "https://%GRUMPY_ARTIFACT_REPO_USERNAME%:%GRUMPY_ARTIFACT_REPO_PASSWORD%@%GRUMPY_ARTIFACT_REPO_URL%" %GRUMPY_ARTIFACT_REPO_BRANCH% %GRUMPY_ARTIFACT_REPO_PATH%
if "%ERRORLEVEL%" NEQ "0" (
echo %~n0: failed to clone artifact repo
exit /b 1
)
:: get stack
echo %~n0: fetching stack...
curl -ostack.zip -L https://www.stackage.org/stack/windows-x86_64 && 7z x stack.zip stack.exe
if "%ERRORLEVEL%" NEQ "0" (
echo %~n0: failed to fetch stack
exit /b 1
)
:: link stack data
echo %~n0: linking caches...
mklink /j %STACK_ROOT% %GRUMPY_ARTIFACT_REPO_PATH%\StackRoot && mklink /j %LOCALAPPDATA%\Programs\stack %GRUMPY_ARTIFACT_REPO_PATH%\LocalAppDataProgramsStack
if "%ERRORLEVEL%" NEQ "0" (
echo %~n0: failed to link caches
exit /b 1
)
goto :eof
:finish
set GIT_DIR=%GRUMPY_ARTIFACT_REPO_PATH%\.git
set GIT_WORK_TREE=%GRUMPY_ARTIFACT_REPO_PATH%
:: add all changes
echo %~n0: adding changes to cache...
git add -A
if "%ERRORLEVEL%" NEQ "0" (
echo %~n0: failed to add changes to cache
exit /b 0
)
:: commit
:: if it fails, probably it's empty
echo %~n0: committing changes to cache...
git commit -q -m "build cache: APPVEYOR_BUILD_ID=%APPVEYOR_BUILD_ID%, APPVEYOR_BUILD_NUMBER=%APPVEYOR_BUILD_NUMBER%" --author "%GRUMPY_ARTIFACT_REPO_USERNAME% <%GRUMPY_ARTIFACT_REPO_USER_EMAIL%>"
if "%ERRORLEVEL%"=="0" (
echo %~n0: pushing changes to cache...
git push -q "https://%GRUMPY_ARTIFACT_REPO_USERNAME%:%GRUMPY_ARTIFACT_REPO_PASSWORD%@%GRUMPY_ARTIFACT_REPO_URL%"
if "%ERRORLEVEL%" NEQ "0" (
echo %~n0: failed to push changes to cache
) else (
echo %~n0: changes pushed successfully
)
) else (
echo %~n0: failed to commit changes, probably there're no changes
)
:: in any case exit with success, error pushing changes should not affect build status
exit /b 0
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment