-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add feature branch actions (#117)
- Loading branch information
Marco
authored
Aug 3, 2023
1 parent
2904a0a
commit b27b513
Showing
2 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
on: | ||
pull_request: | ||
types: ["closed"] | ||
|
||
env: | ||
BRANCH_NAME: ${{ github.head_ref || github.ref_name }} | ||
|
||
jobs: | ||
remove-test: | ||
if: contains(github.event.pull_request.labels.*.name, 'testing') | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
deployments: write | ||
|
||
name: Delete Cloudflare Page | ||
steps: | ||
- name: Set Project Name | ||
run: | | ||
export NAME=$(echo ${{ env.BRANCH_NAME }} | tr -d -c '[:alnum:]' | tr '[:upper:]' '[:lower:]') | ||
echo "PROJECT_NAME=widget-${NAME:0:10}" >> $GITHUB_ENV | ||
- name: Delete project if present | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
fetch('https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${{ env.PROJECT_NAME }}', { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ${{ secrets.CF_PAGES_API_TOKEN }}' | ||
}, | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
if (!r || !r.success || !r.result) { | ||
console.log(r) | ||
process.exit(1) | ||
} | ||
fetch('https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${{ env.PROJECT_NAME }}', { | ||
method: 'DELETE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ${{ secrets.CF_PAGES_API_TOKEN }}' | ||
}, | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
if (!r?.success) { | ||
console.log(r) | ||
process.exit(1) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
on: | ||
pull_request: | ||
|
||
env: | ||
BRANCH_NAME: ${{ github.head_ref || github.ref_name }} | ||
|
||
jobs: | ||
deploy-test: | ||
if: contains(github.event.pull_request.labels.*.name, 'testing') | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
deployments: write | ||
pull-requests: write | ||
|
||
name: Publish to Cloudflare Pages | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install dependencies | ||
uses: ./.github/actions/yarn-install | ||
|
||
- name: Build project | ||
env: | ||
NODE_OPTIONS: "--max_old_space_size=4096" | ||
run: yarn build | ||
|
||
- name: Set Project Name | ||
run: | | ||
export NAME=$(echo ${{ env.BRANCH_NAME }} | tr -d -c '[:alnum:]' | tr '[:upper:]' '[:lower:]') | ||
echo "PROJECT_NAME=widget-${NAME:0:10}" >> $GITHUB_ENV | ||
- name: Create project if not present | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
fetch('https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${{ env.PROJECT_NAME }}', { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ${{ secrets.CF_PAGES_API_TOKEN }}' | ||
}, | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
if (r.success) { | ||
// project already created | ||
return | ||
} else if (r.error) { | ||
console.log(r) | ||
process.exit(1) | ||
} | ||
fetch('https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ${{ secrets.CF_PAGES_API_TOKEN }}' | ||
}, | ||
body: JSON.stringify({ | ||
build_config: { | ||
build_command: "yarn run build", | ||
destination_dir: "packages/widget-playground/dist/", | ||
root_dir: "/", | ||
}, | ||
canonical_deployment: null, | ||
deployment_configs: { | ||
preview: { | ||
analytics_engine_datasets: { | ||
ANALYTICS_ENGINE_BINDING: { | ||
dataset: "api_analytics" | ||
} | ||
}, | ||
compatibility_date: "2022-01-01", | ||
compatibility_flags: [ | ||
"url_standard" | ||
], | ||
placement: { | ||
mode: "smart" | ||
}, | ||
}, | ||
production: { | ||
analytics_engine_datasets: { | ||
ANALYTICS_ENGINE_BINDING: { | ||
dataset: "api_analytics" | ||
} | ||
}, | ||
compatibility_date: "2022-01-01", | ||
compatibility_flags: [ | ||
"url_standard" | ||
], | ||
placement: { | ||
mode: "smart" | ||
}, | ||
} | ||
}, | ||
latest_deployment: null, | ||
name: "${{ env.PROJECT_NAME }}", | ||
production_branch: "${{ env.BRANCH_NAME }}" | ||
}) | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
if (!r?.success) { | ||
console.log(r) | ||
process.exit(1) | ||
} | ||
}) | ||
}) | ||
- name: Deploy Page | ||
uses: cloudflare/[email protected] | ||
with: | ||
apiToken: ${{ secrets.CF_PAGES_API_TOKEN }} | ||
accountId: ${{ secrets.CF_ACCOUNT_ID }} | ||
command: | | ||
pages deploy ./packages/widget-playground/dist/ --project-name ${{ env.PROJECT_NAME }} --branch ${{ env.BRANCH_NAME }} | ||
- name: Comment Test Endpoint | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
fetch("https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${{ env.PROJECT_NAME }}/deployments", { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Bearer ${{ secrets.CF_PAGES_API_TOKEN }}' | ||
} | ||
}) | ||
.then((r) => r.json()) | ||
.then((r) => { | ||
if (!r.success) { | ||
console.log(r) | ||
process.exit(1) | ||
} | ||
const obj = r.result[0] | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: `Hey! This is your new endopint: [${obj.url}](${obj.url})` | ||
}) | ||
}) |