Skip to content

Build and Deploy

Build and Deploy #1237

name: Build and Deploy
on:
workflow_dispatch:
schedule:
- cron: "30 11 * * *" # 5:00 PM IST Everyday
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
jobs:
check-recent-commits:
name: Check Recent Commits
runs-on: ubuntu-latest
outputs:
database_has_changed: ${{ steps.check-new-commits-in-database.outputs.has_new_commits }}
webserver_has_changed: ${{ steps.check-new-commits-in-webserver.outputs.has_new_commits }}
steps:
- uses: actions/checkout@v3
with:
repository: vedupraity/ancientknowledgedatabase
ref: main
token: ${{ secrets.PAT_GITHUB_REPO }}
- name: Check commits in ancientknowledgedatabase since 24 hours ago
id: check-new-commits-in-database
run: |
if [[ "$(git log --since='24 hours ago')" != "" ]]; then
echo "has_new_commits=true" >> $GITHUB_OUTPUT
else
echo "has_new_commits=false" >> $GITHUB_OUTPUT
fi
- uses: actions/checkout@v3
with:
repository: vedupraity/ancientknowledgewebserver
ref: main
token: ${{ secrets.PAT_GITHUB_REPO }}
- name: Check commits in ancientknowledgewebserver since 24 hours ago
id: check-new-commits-in-webserver
run: |
if [[ "$(git log --since='24 hours ago')" != "" ]]; then
echo "has_new_commits=true" >> $GITHUB_OUTPUT
else
echo "has_new_commits=false" >> $GITHUB_OUTPUT
fi
fetch-build-urls:
name: Fetch Build URLs
needs: [check-recent-commits]
if: ${{ needs.check-recent-commits.outputs.database_has_changed == 'true' || needs.check-recent-commits.outputs.webserver_has_changed == 'true' }}
runs-on: ubuntu-latest
outputs:
build_job_ids: ${{ steps.generate-urls.outputs.build_job_ids }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install Requirements
run: |
pip3 install -r requirements.txt
- name: Generate Freeze Candidate URLs
id: generate-urls
run: |
source .env.prod
python generate_urls.py
echo "build_job_ids=$(cat build_job_ids.json | jq -c)" >> $GITHUB_OUTPUT
- name: Cache Freeze Candidates
uses: actions/cache@v2
with:
path: |
build_job_ids.json
urls_per_build_job.json
key: freeze-candidates-${{ github.run_number }}
build-matrix:
name: Build Matrix
runs-on: ubuntu-latest
needs: [fetch-build-urls]
strategy:
matrix:
id: ${{ fromJson(needs.fetch-build-urls.outputs.build_job_ids) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install Requirements
run: |
pip3 install -r requirements.txt
- name: Cache Freeze Candidates
uses: actions/cache@v2
with:
path: |
build_job_ids.json
urls_per_build_job.json
key: freeze-candidates-${{ github.run_number }}
- name: Freeze URLs
run: |
source .env.prod
python freeze_urls.py --build_id=${{ matrix.id }} > >(tee stdout.log) 2> >(tee stderr.log >&2)
has_error=$(cat stderr.log | grep -i "INTERNAL SERVER ERROR") || true
if [[ ! -z $has_error ]]; then
cat stderr.log
exit 1
fi
mv build build_${{ matrix.id }}
du -h -d 1 build_${{ matrix.id }}
- name: Minify and Archive Build
run: |
css-html-js-minify --multiple --overwrite ./build_${{ matrix.id }}
du -h -d 1 build_${{ matrix.id }}
tar -cf build_${{ matrix.id }}.tar.xz ./build_${{ matrix.id }}
- name: Upload build_${{ matrix.id }} Artifact
uses: actions/upload-artifact@v2
with:
name: build-artifact-${{ matrix.id }}
path: build_${{ matrix.id }}.tar.xz
consolidate-build:
name: Consolidate Build
runs-on: ubuntu-latest
needs: [build-matrix]
steps:
- name: Cache Freeze Candidates
uses: actions/cache@v2
with:
path: |
build_job_ids.json
urls_per_build_job.json
key: freeze-candidates-${{ github.run_number }}
- name: Download All Build Artifacts
uses: actions/download-artifact@v2
with:
path: build_artifacts
- name: Decompress and Consolidate Builds
run: |
mkdir build
jq -c '.[]' build_job_ids.json | while read id; do
tar -xf build_artifacts/build-artifact-${id}/build_${id}.tar.xz -C build_artifacts/
rsync -a build_artifacts/build_${id}/ build/
done
- name: Optimize build for GitHub Pages
run: |
# move custom 404 page from build/404/index.html to build/404.html
mv build/404/index.html build/404.html
# remove index.html from build/static
rm -rf build/static/index.html
du -h -d 2 build
- name: Cache Build
uses: actions/cache@v2
with:
path: ./build
key: build-${{ github.run_number }}
deploy:
name: Deploy
needs: [consolidate-build]
runs-on: ubuntu-latest
environment:
name: production
url: https://www.ancientknowledge.in
steps:
- name: Checkout ancientknowledgewebsite
uses: actions/checkout@v3
with:
repository: vedupraity/ancientknowledgewebsite
ref: main
token: ${{ secrets.PAT_GITHUB_REPO }}
- name: Cache Build
uses: actions/cache@v2
with:
path: ./build
key: build-${{ github.run_number }}
- name: Deploy
run: |
cp -rf build/* . && rm -rf build
release_version="Release-${{ github.run_number }}<br>$(TZ=Asia/Kolkata date)"
echo $release_version > version.html
git status
git config user.email "${{ secrets.GIT_USER_EMAIL }}"
git config user.name "${{ secrets.GIT_USER_NAME }}"
git add .
git commit -m "$release_version"
git push