Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: support cloudflare pages functions #13

Merged
merged 4 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/cloudflare-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PROJECT=$1
DEFAULT_BRANCH=$2
DIST=$3
CURRENT_BRANCH=$4
STATICS_DIRECTORY=$5

IFS='/' read -ra fields <<<"$PROJECT"
REPOSITORY_NAME="${fields[1]}"
Expand All @@ -29,9 +30,18 @@ else
--data "{\"name\":\"$REPOSITORY_NAME\",\"production_branch\":\"$DEFAULT_BRANCH\",\"build_config\":{\"build_command\":\"\",\"destination_dir\":\"$DIST\"}}"
fi

if [[ -z "${STATICS_DIRECTORY}" ]]; then
# if STATICS_DIRECTORY input is unspecified then use $DIST as
# STATICS_DIRECTORY treating entire artifact as static-only
STATICS_DIRECTORY=$DIST
else
cd "$DIST"
fi

yarn install --ignore-scripts
yarn add wrangler --ignore-scripts

yarn add wrangler
output=$(yarn wrangler pages deploy "$DIST" --project-name "$REPOSITORY_NAME" --branch "$CURRENT_BRANCH" --commit-dirty=true)
output=$(yarn wrangler pages deploy "$STATICS_DIRECTORY" --project-name "$REPOSITORY_NAME" --branch "$CURRENT_BRANCH" --commit-dirty=true)
output="${output//$'\n'/ }"
# Extracting URL from output only
url=$(echo "$output" | grep -o 'https://[^ ]*' | sed 's/ //g')
Expand Down
84 changes: 78 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
# `@ubiquity/ts-template`
# `ubiquity/cloudflare-deploy-action`

A Github action to automate the deployment of static or full-stack app to Cloudflare pages.

## How to build & upload artifact

```yml
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: full-stack-app
path: |
static
functions
package.json
yarn.lock
```


## How to use the action in a workflow

```yml
jobs:
deploy-to-cloudflare:
name: Automatic Cloudflare Deploy
runs-on: ubuntu-22.04
steps:
- name: Deploy to Cloudflare
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: ubiquity/cloudflare-deploy-action@main
with:
repository: ${{ github.repository }}
production_branch: ${{ github.event.repository.default_branch }}
build_artifact_name: "full-stack-app"
output_directory: "full-stack-app"
current_branch: ${{ github.event.workflow_run.head_branch }}
cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
commit_sha: ${{ github.event.workflow_run.head_sha }}
workflow_run_id: ${{ github.event.workflow_run.id }}
statics_directory: "static"
```


## Artifact for static-only apps
This method is not recommended. All new static-only and full-stack apps should use artifact directory structure given above. The old apps should also transition to the new directory structure of the artifact.
For backward compatibility, the action also works for static-only project with following artifact.

```yml
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: static
path: static
```

Using the action in workflow with static-only artifact:

```yml
jobs:
deploy-to-cloudflare:
name: Automatic Cloudflare Deploy
runs-on: ubuntu-22.04
steps:
- name: Deploy to Cloudflare
if: ${{ github.event.workflow_run.conclusion == 'success' }}
uses: ubiquity/cloudflare-deploy-action@main
with:
repository: ${{ github.repository }}
production_branch: ${{ github.event.repository.default_branch }}
build_artifact_name: "static"
output_directory: "static"
current_branch: ${{ github.event.workflow_run.head_branch }}
cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
commit_sha: ${{ github.event.workflow_run.head_sha }}
workflow_run_id: ${{ github.event.workflow_run.id }}
```

This template repository includes support for the following:

- TypeScript
- Environment Variables
- Conventional Commits
- Automatic deployment to Cloudflare Pages
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ inputs:
workflow_run_id:
description: "Workflow run id which called the action, used for fetching the build artifact"
required: true
statics_directory:
description: "Directory that contains static files"
required: false

runs:
using: "composite"
Expand Down Expand Up @@ -66,7 +69,7 @@ runs:
run_id: ${{ inputs.workflow_run_id }}

- name: Deploy to Cloudflare
run: bash ../../_actions/ubiquity/cloudflare-deploy-action/main/.github/cloudflare-deploy.sh "${{ inputs.repository }}" "${{ inputs.production_branch }}" "${{ inputs.output_directory }}" "${{ fromJSON(steps.pr.outputs.result).forcePreviewDeploy && format('{0}/{1}', github.repository_owner, inputs.current_branch) || inputs.current_branch }}"
run: bash ../../_actions/ubiquity/cloudflare-deploy-action/main/.github/cloudflare-deploy.sh "${{ inputs.repository }}" "${{ inputs.production_branch }}" "${{ inputs.output_directory }}" "${{ fromJSON(steps.pr.outputs.result).forcePreviewDeploy && format('{0}/{1}', github.repository_owner, inputs.current_branch) || inputs.current_branch }}" "${{ inputs.statics_directory }}"
shell: bash
env:
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.cloudflare_account_id }}
Expand Down