From 6b91e537001ee69d2d73b3c379f282cde7fb6f03 Mon Sep 17 00:00:00 2001 From: EresDev Date: Tue, 4 Jun 2024 23:50:11 +0500 Subject: [PATCH 1/4] feat: support cloudflare pages functions --- .github/cloudflare-deploy.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/cloudflare-deploy.sh b/.github/cloudflare-deploy.sh index c4d3921..92d9ddd 100755 --- a/.github/cloudflare-deploy.sh +++ b/.github/cloudflare-deploy.sh @@ -29,8 +29,25 @@ else --data "{\"name\":\"$REPOSITORY_NAME\",\"production_branch\":\"$DEFAULT_BRANCH\",\"build_config\":{\"build_command\":\"\",\"destination_dir\":\"$DIST\"}}" fi +if [ -d "$DIST/functions" ]; then + echo "Found functions directory. Wrangler will deplooy it as backend." + # If the functions directory is present + # $DIST/functions directory is expected to contain Cloudflare Pages Functions. + # $DIST/$DIST directory is expected to contain static files for Cloudflare Pages. + + # If there is no functions directory, everything in "$DIST" is + # expected to be static files for Cloudflare Pages. + # This ensures backward compatibility for existing static-only projects. + + cd "$DIST" +fi + +if [ -f "package.json" ]; then + yarn install --ignore-scripts +else + yarn add wrangler +fi -yarn add wrangler output=$(yarn wrangler pages deploy "$DIST" --project-name "$REPOSITORY_NAME" --branch "$CURRENT_BRANCH" --commit-dirty=true) output="${output//$'\n'/ }" # Extracting URL from output only From ffe833cfae6148b5fc229ee2261bda16666b3461 Mon Sep 17 00:00:00 2001 From: EresDev Date: Sat, 8 Jun 2024 04:12:29 +0500 Subject: [PATCH 2/4] refactor: add statics_directory input --- .github/cloudflare-deploy.sh | 25 +++++++++---------------- action.yml | 5 ++++- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/.github/cloudflare-deploy.sh b/.github/cloudflare-deploy.sh index 92d9ddd..3ad6a85 100755 --- a/.github/cloudflare-deploy.sh +++ b/.github/cloudflare-deploy.sh @@ -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]}" @@ -29,26 +30,18 @@ else --data "{\"name\":\"$REPOSITORY_NAME\",\"production_branch\":\"$DEFAULT_BRANCH\",\"build_config\":{\"build_command\":\"\",\"destination_dir\":\"$DIST\"}}" fi -if [ -d "$DIST/functions" ]; then - echo "Found functions directory. Wrangler will deplooy it as backend." - # If the functions directory is present - # $DIST/functions directory is expected to contain Cloudflare Pages Functions. - # $DIST/$DIST directory is expected to contain static files for Cloudflare Pages. - - # If there is no functions directory, everything in "$DIST" is - # expected to be static files for Cloudflare Pages. - # This ensures backward compatibility for existing static-only projects. - +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 -if [ -f "package.json" ]; then - yarn install --ignore-scripts -else - yarn add wrangler -fi +yarn install --ignore-scripts +yarn add wrangler --ignore-scripts -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') diff --git a/action.yml b/action.yml index 8258e25..51e3470 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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 }} From c27804cefa7bd72b7f3fdb744a29f78400f53a0d Mon Sep 17 00:00:00 2001 From: EresDev Date: Sat, 8 Jun 2024 04:35:52 +0500 Subject: [PATCH 3/4] docs: add instruction to use action --- README.md | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f5bdeee..6305073 100644 --- a/README.md +++ b/README.md @@ -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 artificat. + +```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 From e377b68652b851ebb486cc20d6cb15d1b30e7f9a Mon Sep 17 00:00:00 2001 From: EresDev Date: Sat, 8 Jun 2024 04:41:01 +0500 Subject: [PATCH 4/4] chore: fix spells --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6305073..06fadca 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ jobs: ## 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 artificat. +For backward compatibility, the action also works for static-only project with following artifact. ```yml - name: Upload build artifact