name: 'Cloudflare Pages GitHub Action'
description: 'Publish to Cloudflare Pages'

branding:
  color: 'orange'
  icon: 'upload-cloud'

inputs:
  run:
    description: 'The command to execute before publishing'
    required: false
    default: pnpm build
  workingDirectory:
    description: 'Specifies the working directory where the command is run.'
    required: false
  CLOUDFLARE_API_TOKEN:
    description: 'Cloudflare API Token'
    required: true
  CLOUDLFARE_ACCOUNT_ID:
    description: 'Cloudflare Account ID'
    required: true
  deploymentName:
    description: 'The name of the GitHub deployment'
    required: false
  projectName:
    description: 'The name of the Pages project to upload to'
    required: true
  directory:
    description: 'The directory of static assets to upload'
    required: true
  GITHUB_TOKEN:
    description: 'GitHub Token'
    required: false
  branch:
    description: 'The name of the branch you want to deploy to'
    required: true

outputs:
  id:
    description: 'The ID of the pages deployment'
    value: ${{ steps.deploy.outputs.id }}
  url:
    description: 'The URL of the pages deployment'
    value: ${{ steps.deploy.outputs.url }}
  environment:
    description: 'The environment that was deployed to'
    value: ${{ steps.deploy.outputs.environment }}
  branch:
    description: 'The branch url of the pages deployment'
    value: ${{ steps.deploy.outputs.alias }}
  version:
    description: 'The version url of the pages deployment'
    value: ${{ steps.version.outputs.alias }}

runs:
  using: 'composite'
  steps:
    # deploy sites using the published package versions
    - name: 🏗️  Build ${{ inputs.workingDirectory }}
      id: build
      shell: bash
      env:
        # https://github.com/actions/runner-images/issues/70
        NODE_OPTIONS: '--max_old_space_size=8192'
      # Retry 3 times before the steps actually fails
      run: |
        (${{ inputs.run }}) || (${{ inputs.run }}) || (${{ inputs.run }})
        echo version="`jq '.version' package.json`" >> $GITHUB_OUTPUT
      working-directory: ${{ inputs.workingDirectory }}

    - name: 🚀  Deploy ${{ inputs.workingDirectory }} as ${{ inputs.branch }}
      id: deploy
      uses: sastan/pages-action@walshy/general-improvements
      with:
        apiToken: ${{ inputs.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ inputs.CLOUDLFARE_ACCOUNT_ID }}
        # only create github deployments for main and next
        gitHubToken: ${{ ((inputs.branch == 'main' || inputs.branch == 'next') && inputs.GITHUB_TOKEN) || '' }}
        deploymentName: ${{ inputs.deploymentName }}
        projectName: ${{ inputs.projectName }}
        directory: ${{inputs.workingDirectory}}/${{inputs.directory}}
        branch: ${{ inputs.branch }}
        commitDirty: true

    - name: 🚀  Deploy ${{ inputs.workingDirectory }} as v${{ steps.build.outputs.version }}
      id: version
      uses: sastan/pages-action@walshy/general-improvements
      with:
        apiToken: ${{ inputs.CLOUDFLARE_API_TOKEN }}
        accountId: ${{ inputs.CLOUDLFARE_ACCOUNT_ID }}
        # do NOT create a GitHub deployment for version urls
        # gitHubToken: ${{ inputs.GITHUB_TOKEN }}
        projectName: ${{ inputs.projectName }}
        directory: ${{inputs.workingDirectory}}/${{inputs.directory}}
        # Branch name aliases are lowercased and non-alphanumeric characters are replaced with a hyphen
        # v1.0.0.next.39 -> v1-0-0-next-39
        branch: v${{steps.build.outputs.version}}
        commitDirty: true

    - name: 💡  Deployment Info
      shell: bash
      run: |
        echo "## Deployed with [![Cloudflare](https://img.shields.io/badge/Cloudflare%20Pages-F38020?style=for-the-badge&logo=Cloudflare&logoColor=white)](https://pages.dev)" >> $GITHUB_STEP_SUMMARY
        echo "<strong>Environment:</strong> ${{ steps.deploy.outputs.environment }}" >> $GITHUB_STEP_SUMMARY
        echo "<strong>ID:</strong> ${{ steps.deploy.outputs.id }}" >> $GITHUB_STEP_SUMMARY
        echo "<strong>Deployment URL:</strong> <a href='${{ steps.deploy.outputs.url }}'>${{ steps.deploy.outputs.url }}</a>" >> $GITHUB_STEP_SUMMARY
        echo "<strong>Branch URL:</strong> <a href='${{ steps.deploy.outputs.alias }}'>${{ steps.deploy.outputs.alias }}</a>" >> $GITHUB_STEP_SUMMARY
        echo "<strong>Version URL:</strong> <a href='${{ steps.version.outputs.alias }}'>${{ steps.version.outputs.alias }}</a>" >> $GITHUB_STEP_SUMMARY
