diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3209ce2..63713c9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -61,3 +61,73 @@ jobs: skip-purge: true force: true chunk-size: 10 + + test-functions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # Create test functions + - name: Prepare test functions + run: | + # Create directories + mkdir -p functions + mkdir -p build + + # Create an auth function + cat > functions/auth.js << 'EOL' + export default async function auth(request) { + return { authenticated: true }; + } + EOL + + # Create a filter function + cat > functions/filter.js << 'EOL' + export default async function filter(request) { + return request; + } + EOL + + # Create an edge function + cat > functions/function.js << 'EOL' + export default async function handler(request) { + return new Response("Hello from edge function!"); + } + EOL + + # Create functions config file + cat > functions/config.json << EOL + [ + { + "type": "auth", + "path": "./functions/auth.js", + "description": "Test auth function", + "uuid": "11111111-1111-4111-a111-111111111111" + }, + { + "type": "filter", + "path": "./functions/filter.js", + "description": "Test filter function", + "uuid": "22222222-2222-4222-a222-222222222222" + }, + { + "type": "function", + "path": "./functions/function.js", + "description": "Test edge function", + "uuid": "33333333-3333-4333-a333-333333333333" + } + ] + EOL + + # Create a dummy file in build directory + echo "dummy file" > build/index.html + + # Test function deployment with config file + - name: Test function deployment + uses: ./ + with: + customer: quant + project: github-actions + token: ${{ secrets.QUANT_TOKEN }} + dir: build + functions: "./functions/config.json" diff --git a/README.md b/README.md index 9c466ba..fffd6d2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # Deploy to QuantCDN Action -This GitHub Action deploys your static site or assets to QuantCDN using the Quant CLI v5. +This GitHub Action deploys your static site and/or functions to QuantCDN using the Quant CLI v5. ## Usage +### Deploy Assets ```yaml - uses: quantcdn/action-deploy@v5 with: @@ -13,6 +14,16 @@ This GitHub Action deploys your static site or assets to QuantCDN using the Quan dir: build ``` +### Deploy Functions +```yaml +- uses: quantcdn/action-deploy@v5 + with: + customer: your-customer-id + project: your-project-name + token: ${{ secrets.QUANT_TOKEN }} + functions: './functions.json' +``` + ## Inputs | Input | Description | Required | Default | @@ -20,7 +31,8 @@ This GitHub Action deploys your static site or assets to QuantCDN using the Quan | `customer` | Your QuantCDN customer account name | Yes | - | | `project` | Your QuantCDN project name | Yes | - | | `token` | Your QuantCDN API token | Yes | - | -| `dir` | The directory to deploy | Yes | - | +| `dir` | The directory to deploy | No | - | +| `functions` | Path to JSON file containing functions configuration | No | - | | `skip-unpublish` | Skip automatic unpublishing of assets | No | `false` | | `skip-unpublish-regex` | Skip automatic unpublishing of assets matching regex pattern | No | - | | `skip-purge` | Skip automatic purge of cached assets in CDN | No | `false` | @@ -29,12 +41,10 @@ This GitHub Action deploys your static site or assets to QuantCDN using the Quan | `endpoint` | Specify the QuantCDN API endpoint | No | `https://api.quantcdn.io/v1` | | `revision-log` | Specify a location for the local revision log file | No | `false` | | `enable-index-html` | Enable index.html creation in Quant | No | `false` | -| `functions` | JSON array of functions to deploy. Each object should contain: type (auth|filter|edge), description, path, and uuid | No | - | -| `functions-file` | Path to JSON file containing functions configuration | No | - | ## Example Workflows -### Basic Deployment +### Basic Asset Deployment ```yaml name: Deploy to QuantCDN @@ -62,10 +72,35 @@ jobs: dir: build ``` -### Advanced Deployment +### Functions Deployment + +Create a functions configuration file (e.g., `functions.json`): +```json +[ + { + "type": "auth", + "path": "./functions/auth.js", + "description": "Authentication function", + "uuid": "11111111-1111-4111-a111-111111111111" + }, + { + "type": "filter", + "path": "./functions/filter.js", + "description": "Filter function", + "uuid": "22222222-2222-4222-a222-222222222222" + }, + { + "type": "function", + "path": "./functions/function.js", + "description": "Edge function", + "uuid": "33333333-3333-4333-a333-333333333333" + } +] +``` +Then deploy using: ```yaml -name: Deploy to QuantCDN +name: Deploy Functions to QuantCDN on: push: branches: [ main ] @@ -76,27 +111,19 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Build site - run: | - npm install - npm run build - - - name: Deploy to QuantCDN + - name: Deploy Functions uses: quantcdn/action-deploy@v5 with: customer: your-customer-id project: your-project-name token: ${{ secrets.QUANT_TOKEN }} - dir: build - skip-unpublish: false - chunk-size: 20 - force: true + functions: './functions.json' ``` -### Multiple Functions Deployment Example +### Combined Deployment ```yaml -name: Deploy to QuantCDN with Multiple Functions +name: Deploy Everything to QuantCDN on: push: branches: [ main ] @@ -119,102 +146,9 @@ jobs: project: your-project-name token: ${{ secrets.QUANT_TOKEN }} dir: build - functions: | - [ - { - "type": "auth", - "path": "./functions/auth.js", - "description": "Custom authentication function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c34" - }, - { - "type": "edge", - "path": "./functions/edge.js", - "description": "Custom edge function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c35" - }, - { - "type": "filter", - "path": "./functions/filter.js", - "description": "Custom filter function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c36" - } - ] -``` - -The `functions` input accepts a JSON array where each object must contain: -- `type`: Either "auth", "filter", or "edge" -- `path`: Path to the function file (e.g., "./functions/auth.js") -- `description`: Description of the function -- `uuid`: Valid UUID for the function - -Functions will automatically be deployed to `/fn/{uuid}`. - -### Functions Configuration - -You can configure functions either directly in the workflow or via a JSON file: - -#### Option 1: Direct Configuration - -```yaml - - name: Deploy to QuantCDN - uses: quantcdn/action-deploy@v5 - with: - customer: your-customer-id - project: your-project-name - token: ${{ secrets.QUANT_TOKEN }} - dir: build - functions: | - [ - { - "type": "auth", - "path": "./functions/auth.js", - "description": "Custom authentication function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c34" - } - ] -``` - -#### Option 2: JSON File Configuration - -Create a JSON file (e.g., `functions.json`): -```json -[ - { - "type": "auth", - "path": "./functions/auth.js", - "description": "Custom authentication function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c34" - }, - { - "type": "edge", - "path": "./functions/edge.js", - "description": "Custom edge function", - "uuid": "019361ae-2516-788a-8f50-e803ff561c35" - } -] -``` - -Then reference it in your workflow: -```yaml - - name: Deploy to QuantCDN - uses: quantcdn/action-deploy@v5 - with: - customer: your-customer-id - project: your-project-name - token: ${{ secrets.QUANT_TOKEN }} - dir: build - functions-file: './functions.json' + functions: './functions.json' ``` -The function configuration requires: -- `type`: Either "auth", "filter", or "edge" -- `path`: Path to the function file (e.g., "./functions/auth.js") -- `description`: Description of the function -- `uuid`: Valid UUID for the function - -Functions will automatically be deployed to `/fn/{uuid}`. - ## Notes - This action uses Quant CLI v5 diff --git a/action.yml b/action.yml index 9b2d2a1..6736c8c 100644 --- a/action.yml +++ b/action.yml @@ -13,8 +13,7 @@ inputs: required: true dir: description: "The directory to deploy" - required: true - default: "" + required: false skip-unpublish: description: "Skip automatic unpublishing of assets" required: false @@ -48,117 +47,31 @@ inputs: required: false default: false functions: - description: "JSON array of functions to deploy. Each object should contain: type (auth|filter|edge), description, and uuid" - required: false - functions-file: description: "Path to JSON file containing functions configuration" required: false - auth-function: - description: "Path to auth function file" - required: false - auth-description: - description: "Description for the auth function" - required: false - auth-uuid: - description: "UUID for the auth function" - required: false - filter-function: - description: "Path to filter function file" - required: false - filter-description: - description: "Description for the filter function" - required: false - filter-uuid: - description: "UUID for the filter function" - required: false - edge-function: - description: "Path to edge function file" - required: false - edge-description: - description: "Description for the edge function" - required: false - edge-uuid: - description: "UUID for the edge function" - required: false runs: using: "composite" steps: - - name: Validate and Deploy Functions - uses: "docker://quantcdn/cli:5.0.3" + - name: Deploy Functions + if: inputs.functions != '' + uses: "docker://quantcdn/cli:5.1.1" env: CUSTOMER: ${{ inputs.customer }} TOKEN: ${{ inputs.token }} PROJECT: ${{ inputs.project }} ENDPOINT: ${{ inputs.endpoint }} with: - entrypoint: /bin/bash - args: -c " - if [ ! -z '${{ inputs.functions }}' ]; then - echo '${{ inputs.functions }}' | jq -c '.[]' | while read -r fn; do - type=$(echo $fn | jq -r '.type') - path=$(echo $fn | jq -r '.path') - desc=$(echo $fn | jq -r '.description') - uuid=$(echo $fn | jq -r '.uuid') - - if [ ! -f \"$path\" ]; then - echo \"Error: Function file not found: $path\" - exit 1 - fi - - case $type in - 'auth') - quant auth \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - 'filter') - quant filter \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - 'edge') - quant function \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - *) - echo \"Error: Invalid function type: $type\" - exit 1 - ;; - esac - done - fi - - if [ ! -z '${{ inputs.functions-file }}' ]; then - if [ ! -f '${{ inputs.functions-file }}' ]; then - echo 'Error: Functions file not found: ${{ inputs.functions-file }}' - exit 1 - fi - cat '${{ inputs.functions-file }}' | jq -c '.[]' | while read -r fn; do - type=$(echo $fn | jq -r '.type') - path=$(echo $fn | jq -r '.path') - desc=$(echo $fn | jq -r '.description') - uuid=$(echo $fn | jq -r '.uuid') - - if [ ! -f \"$path\" ]; then - echo \"Error: Function file not found: $path\" - exit 1 - fi + args: > + functions + ${{ inputs.functions }} + -c ${{ inputs.customer }} + -t ${{ inputs.token }} + -p ${{ inputs.project }} + -e ${{ inputs.endpoint }} - case $type in - 'auth') - quant auth \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - 'filter') - quant filter \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - 'edge') - quant function \"$path\" \"$desc\" \"$uuid\" -c $CUSTOMER -t $TOKEN -p $PROJECT -e $ENDPOINT - ;; - *) - echo \"Error: Invalid function type: $type\" - exit 1 - ;; - esac - done - fi - " - name: Deploy Assets - uses: "docker://quantcdn/cli:5.0.3" + if: inputs.dir != '' + uses: "docker://quantcdn/cli:5.1.1" with: args: > deploy