Add write permissions to workflow for creating releases #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
run: | | |
mkdir -p book-outputs | |
cp book.pdf book-outputs/ | |
echo "Book artifacts saved to book-outputs directory" | |
ls -la book-outputs/ | |
# 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 | |
# Get the build artifacts from the previous job | |
- name: Download build artifacts | |
if: needs.build-book.outputs.build_success == 'true' | |
run: | | |
# In a real workflow with upload-artifact support, we'd download artifacts here | |
# For this simplified approach, we'll rebuild the files quickly | |
sudo apt-get update -q | |
sudo apt-get install -y asciidoctor > /dev/null | |
sudo gem install asciidoctor-pdf > /dev/null | |
asciidoctor-pdf Book.asciidoc -o book.pdf | |
# Generate a release ID without pushing tags | |
- name: Generate release ID | |
id: release_id | |
run: | | |
RELEASE_ID="kdp-build-$(date +%Y%m%d-%H%M%S)" | |
echo "release_id=${RELEASE_ID}" >> $GITHUB_OUTPUT | |
echo "Created release ID: ${RELEASE_ID}" | |
# Create the GitHub release | |
- name: Create GitHub Release | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: ${{ steps.release_id.outputs.release_id }} | |
release_name: T-Minus-15 Book for KDP | |
draft: false | |
prerelease: true | |
body: | | |
Book compiled for Amazon KDP Publishing | |
This automated build contains: | |
- PDF for print publishing on Amazon KDP | |
# Upload the PDF as a release asset if it exists | |
- name: Upload PDF Release Asset | |
if: needs.build-book.outputs.build_success == 'true' | |
uses: actions/upload-release-asset@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ./book.pdf | |
asset_name: T-Minus-15.pdf | |
asset_content_type: application/pdf |