diff --git a/.github/workflows/build-image.yml b/.github/workflows/docker-build-push.yml similarity index 50% rename from .github/workflows/build-image.yml rename to .github/workflows/docker-build-push.yml index d585c67..98cb137 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/docker-build-push.yml @@ -1,9 +1,7 @@ name: Create and publish a Docker image on: - push: - branches: - - main + workflow_call: env: REGISTRY: ghcr.io @@ -26,20 +24,33 @@ jobs: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Figure out branch name + shell: bash + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT + id: extract_branch + - name: Determine docker image tag + shell: bash + id: determine_tag + run: | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then + echo "tag=latest" >> $GITHUB_OUTPUT + else + echo "tag=${{ steps.extract_branch.outputs.branch }}" >> $GITHUB_OUTPUT + fi - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - tags: | - type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} - + # Buildx for caching + - uses: docker/setup-buildx-action@v3 - name: Build and push Docker image - uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + uses: docker/build-push-action@v5 with: context: . push: true - tags: ${{ steps.meta.outputs.tags }} + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.determine_tag.outputs.tag }} labels: ${{ steps.meta.outputs.labels }} - + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/release-website.yml b/.github/workflows/release-book-website.yml similarity index 52% rename from .github/workflows/release-website.yml rename to .github/workflows/release-book-website.yml index 84ad60f..2de890a 100644 --- a/.github/workflows/release-website.yml +++ b/.github/workflows/release-book-website.yml @@ -1,10 +1,18 @@ -name: Release the website +name: Release the book and website + on: push: branches: - main +env: + IMAGE: ghcr.io/${{ github.repository }}:latest + jobs: - build_and_release: + build-and-push-image: + uses: ./.github/workflows/docker-build-push.yml + + release-book-website: + needs: build-and-push-image runs-on: ubuntu-latest steps: - name: Set up git repository @@ -12,7 +20,7 @@ jobs: - name: Print dependency versions uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -20,7 +28,7 @@ jobs: - name: Print build variables uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -28,7 +36,23 @@ jobs: - name: Bake the book uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} + options: -v ${{ github.workspace }}:/app + run: | + cd /app/book + make -j bake + - name: Release baked book to S3 + uses: shallwefootball/s3-upload-action@master + with: + aws_key_id: ${{ secrets.AWS_KEY_ID }} + aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY}} + aws_bucket: ${{ secrets.AWS_BUCKET_BOOK }} + source_dir: book/release + destination_dir: release + - name: Bake the website + uses: addnab/docker-run-action@v3 + with: + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -41,6 +65,18 @@ jobs: aws_bucket: ${{ secrets.AWS_BUCKET_WEBSITE }} source_dir: website/static_website_html destination_dir: static_html_root + invalidate-book-website-cache: + needs: release-book-website + runs-on: ubuntu-latest + steps: + - name: Invalidate Cloudfront book cache + uses: chetan/invalidate-cloudfront-action@v2 + env: + DISTRIBUTION: ${{ secrets.CLOUDFRONT_DISTRIBUTION_BOOK }} + PATHS: "/*" + AWS_REGION: "us-east-1" + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - name: Invalidate Cloudfront website cache uses: chetan/invalidate-cloudfront-action@v2 env: diff --git a/.github/workflows/release-book.yml b/.github/workflows/release-book.yml deleted file mode 100644 index 65738ea..0000000 --- a/.github/workflows/release-book.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Release the book -on: - push: - branches: - - main -jobs: - build_and_release: - runs-on: ubuntu-latest - steps: - - name: Set up git repository - uses: actions/checkout@v3 - - name: Print dependency versions - uses: addnab/docker-run-action@v3 - with: - image: ghcr.io/hendricius/the-sourdough-framework:latest - options: -v ${{ github.workspace }}:/app - run: | - cd /app/book - make show_tools_version - - name: Print build variables - uses: addnab/docker-run-action@v3 - with: - image: ghcr.io/hendricius/the-sourdough-framework:latest - options: -v ${{ github.workspace }}:/app - run: | - cd /app/book - make printvars - - name: Bake the book - uses: addnab/docker-run-action@v3 - with: - image: ghcr.io/hendricius/the-sourdough-framework:latest - options: -v ${{ github.workspace }}:/app - run: | - cd /app/book - make -j bake - - name: Release baked book to S3 - uses: shallwefootball/s3-upload-action@master - with: - aws_key_id: ${{ secrets.AWS_KEY_ID }} - aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY}} - aws_bucket: ${{ secrets.AWS_BUCKET_BOOK }} - source_dir: book/release - destination_dir: release - - name: Invalidate Cloudfront book cache - uses: chetan/invalidate-cloudfront-action@v2 - env: - DISTRIBUTION: ${{ secrets.CLOUDFRONT_DISTRIBUTION_BOOK }} - PATHS: "/*" - AWS_REGION: "us-east-1" - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/.github/workflows/validate-book-build.yml b/.github/workflows/test-book-website.yml similarity index 76% rename from .github/workflows/validate-book-build.yml rename to .github/workflows/test-book-website.yml index 22dc3c1..8afd33f 100644 --- a/.github/workflows/validate-book-build.yml +++ b/.github/workflows/test-book-website.yml @@ -1,13 +1,17 @@ -name: Validate LaTeX Document +name: Test building book and website + on: - push: - branches: - - '**' # All branches - - '!main' # But main as will do it anyway and more in release_book pull_request: +env: + IMAGE: ghcr.io/${{ github.repository }}:${{ github.event.pull_request.head.ref }} + jobs: - test_building_book: + build-and-push-image: + uses: ./.github/workflows/docker-build-push.yml + + test-building-book-website: + needs: build-and-push-image runs-on: ubuntu-latest steps: - name: Set up git repository @@ -15,7 +19,7 @@ jobs: - name: Print dependency versions uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -23,7 +27,7 @@ jobs: - name: Print build variables uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -31,7 +35,7 @@ jobs: - name: Test baking the release versions uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book @@ -39,7 +43,7 @@ jobs: - name: Test building website uses: addnab/docker-run-action@v3 with: - image: ghcr.io/hendricius/the-sourdough-framework:latest + image: ${{ env.IMAGE }} options: -v ${{ github.workspace }}:/app run: | cd /app/book