mirror of https://github.com/onweru/compose.git

weru
21.53.2023 74b36c6c5c48c52a45f85e4dc045059eb503d10b
ship default github actions

Signed-off-by: weru <fromweru@gmail.com>
3 files added
241 ■■■■■ changed files
exampleSite/.github/workflows/agolia-update.yaml 99 ●●●●● patch | view | raw | blame | history
exampleSite/.github/workflows/aws-deploy.yaml 117 ●●●●● patch | view | raw | blame | history
exampleSite/content/docs/compose/github-actions.md 25 ●●●●● patch | view | raw | blame | history
exampleSite/.github/workflows/agolia-update.yaml
New file
@@ -0,0 +1,99 @@
name: Update Algolia Search Index
off: # change to `on:` to turn on
  workflow_dispatch:
    branches:
    - production
  push:
    paths:
      - content/**/*
      - config.toml
env:
  # Name of the branch in your repository which will store your generated site.
  SITE-BRANCH: master
jobs:
  build:
  # In this phase, the code is pulled from main and the site rendered in Hugo. The built site is stored as an artifact for other stages. # deploy:
    runs-on: ubuntu-20.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true  # Fetch Hugo themes (true OR recursive)
        fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod
    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: 'latest'
        extended: true
    - name: Build
      run: hugo -e "production" -d "dist" --minify
    # If build succeeds, store the dist/ dir as an artifact to be used in subsequent phases.
    - name: Upload output dist dir as artifact
      uses: actions/upload-artifact@v1
      with:
        name: dist
        path: dist/
  publish:
  # In the publish phase, the site is pushed up to a different branch which only stores the dist/ folder ("site" branch) and is also delta synchronized to the S3 bucket. CloudFront invalidation happens last.
    runs-on: ubuntu-20.04
    needs: build
    steps:
    # Check out the site branch this time since we have to ultimately commit those changes there.
    - name: Checkout site branch
      uses: actions/checkout@v3
      with:
        submodules: true
        fetch-depth: 0
        ref: ${{ env.SITE-BRANCH }}
    # Download the artifact containing the newly built site. This overwrites the dist/ dir from the check out above.
    - name: Download artifact from build stage
      uses: actions/download-artifact@v1
      with:
        name: dist
    # Add all the files/changes in dist/ that were pulled down from the build stage and then commit them.
    # The final line sets a GitHub Action output value that can be read by other steps.
    # This function cannot store mult-line values so newline chars must be stripped.
    - name: Commit files
      id: can_commit
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add -A dist/
        commit_message=$(git commit -m "Publish generated Hugo site." -a | tr -d '\n' || true)
        echo "commit_message=$commxit_message >> $GITHUB_OUTPUT"
    # Checks if previous stage had any valid commit.
    - name: Nothing to commit
      id: nothing_committed
      if: contains(steps.can_commit.outputs.commit_message, 'nothing to commit')
      run: echo "Saw that no changes were made to Hugo site."
    # Push those changes back to the site branch.
    - name: Push to site branch
      if: steps.nothing_committed.conclusion == 'skipped'
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ env.SITE-BRANCH }}
  index:
    runs-on: ubuntu-latest
    needs: publish
    name: Upload Algolia Index
    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - uses: wangchucheng/algolia-uploader@master
      with:
        # Such as `Z0U0ACGBN8`
        app_id: ${{ secrets.AGOLIA_INDEX_ID }}
        # Go to https://github.com/dimi365/website/settings/secrets/actions and set an AGOLIA_ADMIN_KEY secret key
        admin_key: ${{ secrets.AGOLIA_ADMIN_KEY }}
        # The index name.
        index_name: dimi_site
        # The index file path relative to repo root. no leading forward slash
        index_file_path: dist/index.json
exampleSite/.github/workflows/aws-deploy.yaml
New file
@@ -0,0 +1,117 @@
# PREREQUISITES:
# The following secrets must be stored in your repository where this Action runs:
# AWS_ACCESS_KEY_ID
# AWS_CLOUDFRONT_DISTRO_ID
# AWS_S3_BUCKET_NAME
# AWS_SECRET_ACCESS_KEY
name: CI
off: # change to `on:` to turn on
  workflow_dispatch:
    branches:
    - production
  push:
    paths:
      - config.toml
  # pull_request:
  #   branches:
  #   - production
env:
  # Default AWS region where S3 pushes and CloudFront invalidations will occur.
  AWS-DEFAULT-REGION: us-east-2
  # Name of the branch in your repository which will store your generated site.
  SITE-BRANCH: site
jobs:
  build:
  # In this phase, the code is pulled from main and the site rendered in Hugo. The built site is stored as an artifact for other stages. # deploy:
    runs-on: ubuntu-20.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true  # Fetch Hugo themes (true OR recursive)
        fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod
    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: 'latest'
        extended: true
    - name: Build
      run: hugo -e "production" --minify
    # If build succeeds, store the public/ dir as an artifact to be used in subsequent phases.
    - name: Upload output public dir as artifact
      uses: actions/upload-artifact@v1
      with:
        name: public
        path: public/
  publish:
  # In the publish phase, the site is pushed up to a different branch which only stores the public/ folder ("site" branch) and is also delta synchronized to the S3 bucket. CloudFront invalidation happens last.
    runs-on: ubuntu-20.04
    needs: build
    steps:
    # Check out the site branch this time since we have to ultimately commit those changes there.
    - name: Checkout site branch
      uses: actions/checkout@v3
      with:
        submodules: true
        fetch-depth: 0
        ref: ${{ env.SITE-BRANCH }}
    # Download the artifact containing the newly built site. This overwrites the public/ dir from the check out above.
    - name: Download artifact from build stage
      uses: actions/download-artifact@v1
      with:
        name: public
    # Add all the files/changes in public/ that were pulled down from the build stage and then commit them.
    # The final line sets a GitHub Action output value that can be read by other steps.
    # This function cannot store mult-line values so newline chars must be stripped.
    - name: Commit files
      id: can_commit
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add -A public/
        commit_message=$(git commit -m "Publish generated Hugo site." -a | tr -d '\n' || true)
        echo "commit_message=$commit_message >> $GITHUB_OUTPUT"
    # Checks if previous stage had any valid commit.
    - name: Nothing to commit
      id: nothing_committed
      if: contains(steps.can_commit.outputs.commit_message, 'nothing to commit')
      run: echo "Saw that no changes were made to Hugo site."
    # Push those changes back to the site branch.
    - name: Push to site branch
      if: steps.nothing_committed.conclusion == 'skipped'
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ env.SITE-BRANCH }}
    # Store the AWS credentials on the runner.
    - name: Configure AWS credentials
      if: steps.nothing_committed.conclusion == 'skipped'
      uses: aws-actions/configure-aws-credentials@v1-node16
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{ env.AWS-DEFAULT-REGION }}
    - name: Delta sync site to S3 with aws cli
      if: steps.nothing_committed.conclusion == 'skipped'
      run: aws s3 sync --size-only --delete --exclude "/authors/*/page/*" --cache-control max-age=2592000 public/ s3://${{ secrets.AWS_S3_BUCKET_NAME }}
    # Use s5cmd to perform only a delta sync to the destination S3 bucket. This minimizes transfer traffic since it only uploads changed files.
    # - name: Delta sync site to S3 bucket
    #   if: steps.nothing_committed.conclusion == 'skipped'
    #   run: |
    #     curl -sLO https://github.com/peak/s5cmd/releases/download/v1.0.0/s5cmd_1.0.0_Linux-64bit.tar.gz
    #     tar -xzf s5cmd_1.0.0_Linux-64bit.tar.gz
    #     chmod +x s5cmd
    #     sudo mv s5cmd /usr/local/bin/
    #     echo "****Showing working dir and listing files.****"
    #     pwd && ls -lah
    #     echo "****Running delta sync against S3.****"
    #     s5cmd cp -s -n -u public/ s3://${{ secrets.AWS_S3_BUCKET_NAME }}
    # Use the aws cli tool to perform a glob invalidation of the entire site against CloudFront.
    - name: Invalidate cache on CloudFront
      if: steps.nothing_committed.conclusion == 'skipped'
      run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRO_ID }} --paths "/*"
exampleSite/content/docs/compose/github-actions.md
New file
@@ -0,0 +1,25 @@
+++
description = "Use github actions with compose theme"
title = "Leverage Github actions to publish with compose theme"
weight = 11
+++
This theme ships with 2 github actions inside the exampleSite folder:
1. AWS CI
    This helps you to autodeploy your hugo website from github to an AWS s3 bucket. Set the secrets in the action accordingly and voila.
2. Algolia CI
    When [algolia search integration](https://github.com/onweru/compose/issues/98) is rolls out (soon), this action will automatically update your algolia search index. No extra npm manual setup will be needed.
These actions are customizable to fire off under your specified set of circumstances.
By default, the actions will be off, so be sure to turn them on
```shell
name: Update Algolia Search Index
off: # change to `on:` to turn on
```