Skip to content

Commit

Permalink
Add a preview-deploy action (#41)
Browse files Browse the repository at this point in the history
* Add a preview-deploy action

* add example workflow demonstrating branch previews

* Add preview example and description to readme

* Update example-workflow.yml

* use 10.2.4

* use new cli flag to simplify logic
  • Loading branch information
JakeChampion authored Jul 31, 2023
1 parent 28caf98 commit c2f38da
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ Alternatively, you can manually run the individual GitHub Actions for Compute@Ed
- [fastly/compute-actions/setup](setup/index.js) - Download the Fastly CLI if not already installed
- [fastly/compute-actions/build](build/index.js) - Build a Compute@Edge project. Equivalent to `fastly compute build`
- [fastly/compute-actions/deploy](deploy/index.js) - Deploy a Compute@Edge project. Equivalent to `fastly compute deploy`
- [fastly/compute-actions/preview](preview/action.yml) - Deploy a Compute@Edge project to a new Fastly Service, which is deleted when the pull-request is merged or closed.

#### Deploy to Fastly when push to `main`

```yml
name: Deploy Application
on:
push:
branches: [master]
branches: [main]
jobs:
deploy:
Expand Down Expand Up @@ -102,6 +105,29 @@ jobs:
FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
```

#### Preview on Fastly for each pull-request

```yml
name: Fastly Compute@Edge Branch Previews
concurrency:
group: ${{ github.head_ref || github.run_id }}-${{ github.workflow}}
on:
pull_request:
types: [opened, synchronize, reopened, closed]
jobs:
deploy:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- uses: fastly/compute-actions/preview@v5
with:
fastly-api-token: ${{ secrets.FASTLY_API_KEY }}
github-token: ${{ secrets.GITHUB_TOKEN }}
```

### Inputs

The following inputs can be used as `with` keys for the actions in this repository; none of them are required:
Expand Down
63 changes: 63 additions & 0 deletions preview/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: 'Branch Preview'
description: 'Preview Fastly Service'

inputs:
fastly-api-token:
description: 'The Fastly API token to use for interacting with Fastly API'
required: true
github-token:
description: 'The GitHub token to use for downloading the Fastly CLI'
required: true

outputs:
domain:
description: "Preview domain for Fastly Compute@Edge Service"
value: ${{ steps.domain.outputs.DOMAIN }}
service_name:
description: "Preview service name for Fastly Compute@Edge Service"
value: ${{ steps.service-name.outputs.SERVICE_NAME }}

runs:
using: "composite"
steps:
- uses: actions/checkout@v3
# Download Fastly CLI
- name: Set up Fastly CLI
uses: fastly/compute-actions/setup@v4
with:
token: ${{ inputs.github-token }}
cli_version: '10.2.4'
- run: yarn
shell: bash

# Create a new Fastly Service name with the PR number appended
- name: Set service-name
id: service-name
run: echo "SERVICE_NAME=$(yq '.name' fastly.toml)-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
shell: bash

# Delete the Fastly Service
- if: github.event.action == 'closed'
run: fastly service delete --quiet --service-name ${{ steps.service-name.outputs.SERVICE_NAME }} --force --token ${{ inputs.fastly-api-token }} || true
shell: bash

# Deploy to Fastly and let Fastly choose a subdomain of edgecompute.app to attach to the service
- if: github.event.action != 'closed'
run: |
fastly compute publish --verbose -i --token ${{ inputs.fastly-api-token }} --service-name ${{ steps.service-name.outputs.SERVICE_NAME }}
shell: bash
# Retrieve the newly created domain for the service and add it to the pull-request summary
- if: github.event.action != 'closed'
run: fastly domain list --quiet --version latest --json --service-name="${{ steps.service-name.outputs.SERVICE_NAME }}" --token ${{ inputs.fastly-api-token }} | jq -r '.[0].Name'
shell: bash

- if: github.event.action != 'closed'
name: Set domain
shell: bash
id: domain
run: echo "DOMAIN=$(fastly domain list --quiet --version latest --json --service-name="${{ steps.service-name.outputs.SERVICE_NAME }}" --token ${{ inputs.fastly-api-token }} | jq -r '.[0].Name')" >> "$GITHUB_OUTPUT"

- if: github.event.action != 'closed'
shell: bash
name: Add domain to summary
run: echo 'This pull-request has been deployed to Fastly and is available at <https://${{ steps.domain.outputs.DOMAIN }}> 🚀' >> $GITHUB_STEP_SUMMARY
18 changes: 18 additions & 0 deletions preview/example-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Fastly Compute@Edge Branch Previews
concurrency:
group: ${{ github.head_ref || github.run_id }}-${{ github.workflow}}
on:
pull_request:
types: [opened, synchronize, reopened, closed]
jobs:
deploy:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3
- uses: fastly/compute-actions/preview@v5
with:
fastly-api-token: ${{ secrets.FASTLY_API_KEY }}
github-token: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit c2f38da

Please sign in to comment.