blob: eb6c817a1d51b4163e693f958ad0a0ac0b6a4b24 [file] [log] [blame]
# Builds and deploys the site if the build was successful
#
# This job builds the website and deploys the build artifacts to the 'content' directory
# Any additional files that are not part of the site build but should be published (e.g. website configuration files) must be handled explicitly
# See step "Move additional resources to build directory" and sub-steps "Dropping old site resources" and "Adding new site configuration" of the 'publish' step
name: Build & Deploy Site
# Conditions necessary to trigger a build
on:
push:
# Ignore pushes on all branches by default
# Branches that should trigger a build must be explicitly excluded from the ignore using '!BRANCH_NAME'
#
# Excluding the branch that is pushed by this action will lead to an infinite loop
branches-ignore:
- '**'
- '!master'
# Only build if a file in one of these paths has been changed
paths:
- 'src/main/asciidoc/**'
- 'src/main/template/**'
- '.asf.yaml.publish'
- '.htaccess'
jobs:
build:
name: Build & Deploy Site
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 1
- name: Build Site
run: mvn clean compile
# Determines the short sha of the commit that triggered the build
- name: Determine Short SHA
if: success()
id: short-sha
run: |
short_sha=$(git rev-parse --short=10 $GITHUB_SHA)
echo "::set-output name=short_sha::$short_sha"
shell: 'bash'
# Determines the author data of the HEAD commit
# Sets up the author data to be used for the site deploy commit
- name: Determine HEAD Commit Author Data
if: success()
id: author-data
run: |
echo "Reading author data"
author_name=$(git log -1 --format='%aN' HEAD)
author_email=$(git log -1 --format='%aE' HEAD)
echo "Setting up author data to use for deploy commit"
git config user.name $author_name
git config user.email $author_email
shell: 'bash'
# Adds additional configuration files that are supposed to be included in the page deploy to the build directory
#
# This ensures that such files are preserved when checking out the publish branch
- name: Add additional resources to build directory
if: success()
run: |
cp -v .asf.yaml.publish target/site/.asf.yaml
cp -v .htaccess target/site/
# Publishes the site build results to a separate branch
#
# Checks out the site branch
# Replaces the site configuration files and site build artifact with the ones set up in the previous step
# Creates a new commit containing the new site build artifacts and site configuration files
# The commit is created with the author data set up in the previous step
# Pushes the site branch
- name: Publish site branch
if: success()
run: |
branch_name="publish"
echo "Checking out site branch"
git fetch origin $branch_name
git checkout -b $branch_name origin/$branch_name
echo
# Drops all existing files and folders except the base folder and the resources excluded by the regex
# This ensures that old configuration files that were removed on the master will be removed from the site branch as well
# Additional resources to exclude can be added by modifying the regex or adding new regex by using "-a -not -regex '...'"
echo "Dropping old site resources"
find . \
-mindepth 1 -regextype posix-extended \
-not -regex '^\./(target|.git)(/.*)?$' \
-delete -print
echo
echo "Adding new site configuration"
mv -v target/site/.asf.yaml ./
mv -v target/site/.htaccess ./
echo
echo "Adding new site build"
mkdir -v content
mv -v target/site/* content/
echo
# Explicitly removes build dir
# This checks whether there are any remaining resources that were not moved to the correct location
echo "Removing build dir"
rmdir -v -p target/site
echo
echo "Staging new content"
git add .
echo
echo "Committing changes"
git commit -m "Auto-deploy site for commit ${{ steps.short-sha.outputs.short_sha }}"
echo
echo "Pushing site branch"
git push origin $branch_name
shell: 'bash'