Skip to content

Simplify GitHub Actions workflow to use GitHub CLI for releases #11

Simplify GitHub Actions workflow to use GitHub CLI for releases

Simplify GitHub Actions workflow to use GitHub CLI for releases #11

Workflow file for this run

name: Book Build for Amazon KDP
on:
workflow_dispatch: # Manual triggering
push:
branches: [ main ] # Automatic triggering on push to main branch
# Set permissions for the workflow
permissions:
contents: write # Needed for creating releases
jobs:
# Job 1: Set up the environment and build the book files
build-book:
name: Build Book PDF
runs-on: ubuntu-latest
outputs:
pdf_path: ${{ steps.build-pdf.outputs.pdf_path }}
build_success: ${{ steps.build-pdf.outputs.build_success }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up environment
run: |
sudo apt-get update
sudo apt-get install -y asciidoctor
sudo gem install asciidoctor-pdf
asciidoctor --version
- name: Build PDF
id: build-pdf
run: |
echo "Starting PDF build..."
if asciidoctor-pdf Book.asciidoc -o book.pdf; then
echo "PDF build succeeded"
echo "build_success=true" >> $GITHUB_OUTPUT
echo "pdf_path=book.pdf" >> $GITHUB_OUTPUT
else
echo "PDF build failed"
echo "build_success=false" >> $GITHUB_OUTPUT
echo "pdf_path=" >> $GITHUB_OUTPUT
fi
# Verify the build results
if [ -f "book.pdf" ]; then
echo "PDF file exists and has size:"
ls -la book.pdf
else
echo "PDF file was not created successfully"
fi
# Upload artifacts between jobs
- name: Save book artifacts
if: steps.build-pdf.outputs.build_success == 'true'
uses: actions/upload-artifact@v3
with:
name: book-artifacts
path: book.pdf
retention-days: 1
# Job 2: Create the GitHub release with the built files
create-release:
name: Create GitHub Release
needs: build-book
runs-on: ubuntu-latest
steps:
# Need to checkout again since this is a separate job
- name: Checkout repository
uses: actions/checkout@v4
# Rebuild the PDF for the release
# Note: We're just rebuilding since it's faster than setting up artifact transfer
- name: Build PDF for release
if: needs.build-book.outputs.build_success == 'true'
run: |
echo "Building PDF for release..."
# Install dependencies
sudo apt-get update -q
sudo apt-get install -y asciidoctor > /dev/null
sudo gem install asciidoctor-pdf > /dev/null
# Build the PDF
asciidoctor-pdf Book.asciidoc -o book.pdf
# Verify the build
if [ -f "book.pdf" ]; then
echo "PDF file exists: $(du -h book.pdf)"
else
echo "ERROR: PDF file was not created successfully"
exit 1
fi
# Generate a release ID and tag
- name: Generate release ID
id: release_id
run: |
RELEASE_ID="v0.$(date +%Y%m%d.%H%M%S)"
echo "release_id=${RELEASE_ID}" >> $GITHUB_OUTPUT
echo "Created release ID: ${RELEASE_ID}"
echo "PDF exists: $(ls -la book.pdf)"
# Create GitHub release and upload PDF using GitHub CLI (simpler approach)
- name: Create GitHub Release with CLI
if: needs.build-book.outputs.build_success == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create the release
echo "Creating release ${{ steps.release_id.outputs.release_id }}..."
gh release create ${{ steps.release_id.outputs.release_id }} \
--title "T-Minus-15 Book for KDP" \
--notes "Book compiled for Amazon KDP Publishing on $(date +'%Y-%m-%d')" \
--prerelease \
book.pdf
echo "Release created successfully with PDF attached"
gh release view ${{ steps.release_id.outputs.release_id }}