From 0569dcca90f577118b4171905ccc020df409585b Mon Sep 17 00:00:00 2001 From: Chris Dennis Date: Fri, 25 Oct 2019 10:28:10 -0400 Subject: [PATCH] Generate Netlify previews for PRs --- azure-pipelines.yml | 160 +++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 63 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9bd8c8dd..5922cf06 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -3,67 +3,101 @@ # Add steps that build, test, save build artifacts, deploy, and more: # https://aka.ms/yaml -pool: - vmImage: 'Ubuntu 16.04' +jobs: + - job: + timeoutInMinutes: 120 + pool: + vmImage: 'ubuntu-latest' + + steps: + - checkout: self + persistCredentials: 'true' + clean: 'true' + - task: YodLabs.VariableTasks.SetVariablesWithCredentials.SetVariablesWithCredentials@0 + displayName: 'Github auth' + inputs: + ConnectionName: gh-push-tc-org + userNameVarName: U + passwordVarName: P + - task: YodLabs.VariableTasks.SetVariablesWithCredentials.SetVariablesWithCredentials@0 + displayName: 'Github auth PR' + inputs: + ConnectionName: 'gh-pac' + passwordVarName: 'GITHUB_TOKEN' + - task: YodLabs.VariableTasks.SetVariablesWithCredentials.SetVariablesWithCredentials@0 + displayName: 'Netlify auth' + inputs: + ConnectionName: netlify-quartz + userNameVarName: NETLIFY_SITE_ID + passwordVarName: NETLIFY_AUTH_TOKEN + - task: Docker@0 + displayName: 'Jekyll and all Ruby steps' + inputs: + containerRegistryType: 'Container Registry' + action: 'Run an image' + # should match Gemfile: + imageName: 'jekyll/builder:3.7.3' + volumes: | + $(build.sourcesDirectory):/srv/jekyll + $(build.binariesDirectory):/srv/jekyll/_site + containerCommand: 'sh -x -c "chmod a+w Gemfile.lock && bundle install && bash -e get-deps.sh && JEKYLL_ENV=production jekyll build"' + detached: false + - bash: | + set -e + # not sure why some files are root owned, maybe docker + sudo chown -R $(id -u):$(id -g) $BUILD_SOURCESDIRECTORY $BUILD_BINARIESDIRECTORY + + COMMIT=$(git rev-parse HEAD) + + # Determine whether to publish staging or live site + if [ -n "$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" ] ; then + gh_url="https://$U:$P@github.com/quartz-scheduler/quartz-scheduler.org-site.git" + gh_branch=gh-pages + gh_source="#${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}" + echo "Publishing staging from $gh_source" + elif [ "$BUILD_SOURCEBRANCHNAME" == 'master' ] ; then + gh_url="https://$U:$P@github.com/quartz-scheduler/quartz-scheduler.github.io.git" + gh_branch=master + gh_source=master + echo "Publishing Live site" + (bash add-analytics.sh $BUILD_SOURCESDIRECTORY $BUILD_BINARIESDIRECTORY) + else + echo "Unknown branch to deploy: $BUILD_SOURCEBRANCHNAME" + exit 5 + fi + + rm -rf * .git* # start clean in source directory + git clone --single-branch -b $gh_branch $gh_url remote-site + cd remote-site + echo "Target dir:"; ls -l + echo "Generated dir:"; ls -l $BUILD_BINARIESDIRECTORY + rsync -a --delete --exclude .git --exclude CNAME $BUILD_BINARIESDIRECTORY/ . + echo "Result:"; ls -l + + git config --local user.name "autogen" + git config --local user.email "autogen-noreply@no-reply.softwareag.com" + git add -A . + git status + git commit -m "Autogenerated from ${gh_source} ${COMMIT}" + git push $gh_url $gh_branch + condition: not(eq(variables['Build.Reason'], 'PullRequest')) + displayName: 'Publish' + - bash: | + set -xe + uname -a + cat /etc/*elease* + npm -v + node -v + npm install netlify-cli + du -sh $BUILD_BINARIESDIRECTORY + find $BUILD_BINARIESDIRECTORY | wc -l + $(npm bin)/netlify deploy -d $BUILD_BINARIESDIRECTORY --json | tee netlify.json + + NETLIFY_URL=$(jq -r '.deploy_url' netlify.json) + echo "Informing GitHub about deploy url: ${NETLIFY_URL}" + curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST -d "{\"body\": \"[A Live Preview](${NETLIFY_URL}) of this Pull Request at commit ${SYSTEM_PULLREQUEST_SOURCECOMMITID} is now available\", \"commit_id\": \"${SYSTEM_PULLREQUEST_SOURCECOMMITID}\"}" "https://api.github.com/repos/quartz-scheduler/quartz-scheduler.org-site/issues/${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}/comments" + + displayName: 'Netlify Publish' + condition: eq(variables['Build.Reason'], 'PullRequest') + -steps: -- checkout: self - persistCredentials: 'true' - clean: 'true' -- task: YodLabs.VariableTasks.SetVariablesWithCredentials.SetVariablesWithCredentials@0 - displayName: 'Github auth' - inputs: - ConnectionName: gh-push-tc-org - userNameVarName: U - passwordVarName: P -- task: Docker@0 - displayName: 'Jekyll and all Ruby steps' - inputs: - containerRegistryType: 'Container Registry' - action: 'Run an image' - # should match Gemfile: - imageName: 'jekyll/builder:3.7.3' - volumes: | - $(build.sourcesDirectory):/srv/jekyll - $(build.binariesDirectory):/srv/jekyll/_site - containerCommand: 'sh -x -c "chmod a+w Gemfile.lock && bundle install && bash -e get-deps.sh && JEKYLL_ENV=production jekyll build"' - detached: false -- bash: | - set -e - # not sure why some files are root owned, maybe docker - sudo chown -R $(id -u):$(id -g) $BUILD_SOURCESDIRECTORY $BUILD_BINARIESDIRECTORY - COMMIT=$(git rev-parse HEAD) - # Determine whether to publish staging or live site - if [ -n "$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" ] ; then - echo "Not publishing staging at this time" - exit 0 # do not continue. we're OK. - - gh_url="https://$U:$P@github.com/quartz-scheduler/quartz-scheduler.org-site.git" - gh_branch=gh-pages - gh_source="#${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}" - echo "Publishing staging from $gh_source" - elif [ "$BUILD_SOURCEBRANCHNAME" == 'master' ] ; then - gh_url="https://$U:$P@github.com/quartz-scheduler/quartz-scheduler.github.io.git" - gh_branch=master - gh_source=master - echo "Publishing Live site" - (bash add-analytics.sh $BUILD_SOURCESDIRECTORY $BUILD_BINARIESDIRECTORY) - else - echo "Unknown branch to deploy: $BUILD_SOURCEBRANCHNAME" - exit 5 - fi - rm -rf * .git* # start clean in source directory - git clone --single-branch -b $gh_branch $gh_url remote-site - cd remote-site - echo "Target dir:"; ls -l - echo "Generated dir:"; ls -l $BUILD_BINARIESDIRECTORY - rsync -a --delete --exclude .git --exclude CNAME $BUILD_BINARIESDIRECTORY/ . - echo "Result:"; ls -l - git config --local user.name "autogen" - git config --local user.email "autogen-noreply@no-reply.softwareag.com" - git add -A . - git status - git commit -m "Autogenerated from ${gh_source} ${COMMIT}" - git push $gh_url $gh_branch - condition: not(eq(variables['Build.Reason'], 'PullRequest')) - displayName: 'Publish'