Skip to content

Commit

Permalink
Revert "Revert "Merge to prod""
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao authored Jan 8, 2025
1 parent 58c5b48 commit af6db18
Show file tree
Hide file tree
Showing 66 changed files with 3,552 additions and 1,918 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports = {
{ devDependencies: ['./*.ts'] },
],
'@typescript-eslint/no-require-imports': 'error',
"@typescript-eslint/no-shadow": "error",
"no-shadow": "off",
'import/extensions': [
'error',
'ignorePackages',
Expand Down
77 changes: 77 additions & 0 deletions .github/actions/notification/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Discord Notification
description: Send Discord Notification

inputs:
TYPE:
description: "Embed Type"
required: false
TITLE:
description: "Title"
required: false
DESCRIPTION:
description: "Description"
required: false
AUTHOR_NAME:
description: "Author Name"
required: false
AUTHOR_URL:
description: "Author URL"
required: false
AUTHOR_ICON_URL:
description: "Author Icon URL"
required: false
DISCORD_WEBHOOK_URL:
description: "Discord Webhook URL"
required: true

runs:
using: "composite"

steps:
- name: Send Discord Notification
shell: bash
run: |
if [ "${{ inputs.TYPE }}" == "success" ]; then
COLOR=5763207
elif [ "${{ inputs.TYPE }}" == "failure" ]; then
COLOR=15548997
elif [ "${{ inputs.TYPE }}" == "info" ]; then
COLOR=1752301
else
COLOR=9868950
fi
PAYLOAD=$(cat <<EOF
{
"avatar_url": "https://raw.githubusercontent.com/daodaoedu/daodao-f2e/main/public/daodao-logo.webp",
"username": "島島阿學",
"embeds": [
{
"author": {
"name": "${{ inputs.AUTHOR_NAME }}",
"url": "${{ inputs.AUTHOR_URL }}",
"icon_url": "${{ inputs.AUTHOR_ICON_URL }}"
},
"title": "${{ inputs.TITLE }}",
"description": "${{ inputs.DESCRIPTION }}",
"color": $COLOR
}
]
}
EOF
)
response=$(curl -s -w "\n%{http_code}" -H "Content-Type: application/json" \
-X POST \
-d "$PAYLOAD" \
${{ inputs.DISCORD_WEBHOOK_URL }})
status_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')
if [ "$status_code" -ge 400 ]; then
echo "Error sending Discord notification"
echo "Status code: $status_code"
echo "Response: $response_body"
exit 1
fi
153 changes: 153 additions & 0 deletions .github/workflows/ci-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
name: CI and Deploy workflow

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true

on:
pull_request:
branches:
- "**"
- "!dependabot/**"
push:
branches:
- "prod"
- "dev"
workflow_dispatch:

env:
PR_URL: ${{ github.server_url }}/${{ github.repository }}/pull/${{ github.event.number }}

jobs:
init:
name: Initial Common Steps
runs-on: ubuntu-latest

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
~/.cache/Cypress
key: deps-node-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

lint:
name: Lint
runs-on: ubuntu-latest
needs: init

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
~/.cache/Cypress
key: deps-node-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: lint
run: |
yarn lint
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
git diff --cached --quiet || echo "changes=true" >> $GITHUB_ENV
- name: Commit lint Changes
if: env.changes == 'true'
run: |
git commit -m "chore: format code"
git push
build_and_deploy:
name: Build and Deploy to Cloudflare
runs-on: ubuntu-latest
environment: production
timeout-minutes: 30
needs: lint

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Cache dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
~/.cache/Cypress
key: deps-node-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: Build
run: yarn pages:build

- name: Deploy to Cloudflare
id: deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
command: pages deploy --branch=${{ github.event.pull_request.head.ref || github.ref_name }}

outputs:
preview-url: ${{ steps.deploy.outputs.pages-deployment-alias-url }}
deploy-env: ${{ steps.deploy.outputs.pages-environment }}

send_result:
name: Send Result
runs-on: ubuntu-latest
needs: [lint, build_and_deploy]
if: always()

steps:
- uses: actions/checkout@v3

- name: Send Lint Failed Notification
if: ${{ needs.lint.result == 'failure' }}
uses: ./.github/actions/notification
with:
TYPE: failure
TITLE: "❌ Lint Failed"
DESCRIPTION: "PR Link: [PR #${{ github.event.number }}](${{ env.PR_URL }})"
DISCORD_WEBHOOK_URL: ${{ secrets.SANDBOX_DISCORD_WEBHOOK_URL }}

- name: Send Build and Deploy Failed Notification
if: ${{ needs.build_and_deploy.result == 'failure' }}
uses: ./.github/actions/notification
with:
TYPE: failure
TITLE: "❌ Build and Deploy Failed"
DESCRIPTION: "PR Link: [PR #${{ github.event.number }}](${{ env.PR_URL }})"
DISCORD_WEBHOOK_URL: ${{ secrets.SANDBOX_DISCORD_WEBHOOK_URL }}

- name: Send Build Succeeded Notification
if: ${{ needs.build_and_deploy.result == 'success' }}
uses: ./.github/actions/notification
with:
TYPE: success
TITLE: "✅ Build Succeeded"
DESCRIPTION: "Preview URL: ${{ needs.build_and_deploy.outputs.preview-url }}\\nEnvironment: ${{ needs.build_and_deploy.outputs.deploy-env }})"
DISCORD_WEBHOOK_URL: ${{ secrets.SANDBOX_DISCORD_WEBHOOK_URL }}
50 changes: 50 additions & 0 deletions .github/workflows/cleanup-cache-after-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: cleanup branch resource
on:
pull_request:
types:
- closed
workflow_dispatch:

jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Cleanup cache
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )
echo "Cache keys for PR: $cacheKeysForPR"
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Delete artifacts
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
artifacts=$(gh api /repos/${{ github.repository }}/actions/artifacts --paginate)
for artifact in $(echo "$artifacts" | jq -r ".artifacts[] | select(.name | startswith(\"PR-$PR_NUMBER-\")) | .id"); do
echo "filtered artifact: $artifact"
gh api -X DELETE /repos/${{ github.repository }}/actions/artifacts/$artifact
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/pr-notification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PR Notification

on:
pull_request:
branches: "**"

env:
TITLE: "${{ github.event.pull_request.title }}"
PR_LINK: "PR Link: [PR #${{ github.event.number }}](${{ github.server_url }}/${{ github.repository }}/pull/${{ github.event.number }})"
DESCRIPTION: "${{ github.event.pull_request.body || 'No description' }}"

jobs:
send_notification:
name: Send Notification
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Send PR Notification
uses: ./.github/actions/notification
with:
TYPE: info
TITLE: "🔔 ${{ env.TITLE }}"
DESCRIPTION: "${{ env.PR_LINK }}\\n\\n${{ env.DESCRIPTION }}"
AUTHOR_NAME: ${{ github.event.pull_request.user.name || github.event.pull_request.user.login }}
AUTHOR_URL: ${{ github.event.pull_request.user.html_url }}
AUTHOR_ICON_URL: ${{ github.event.pull_request.user.avatar_url }}
DISCORD_WEBHOOK_URL: ${{ secrets.SANDBOX_DISCORD_WEBHOOK_URL }}
51 changes: 0 additions & 51 deletions .github/workflows/push-change-to-discord-webhook-url.yml

This file was deleted.

Loading

0 comments on commit af6db18

Please sign in to comment.