Skip to content

Commit

Permalink
Merge pull request #200 from daodaoedu/feat/web-ci-and-cd
Browse files Browse the repository at this point in the history
feat: web ci and cd
  • Loading branch information
vincentxuu authored Jan 4, 2025
2 parents 17a91c2 + 5e8efa0 commit 4f705a3
Show file tree
Hide file tree
Showing 9 changed files with 2,228 additions and 112 deletions.
133 changes: 133 additions & 0 deletions .github/workflows/ci-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: CI and Deploy workflow

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

on:
pull_request:
branches: "**"
workflow_dispatch:

env:
DEPLOY_OUTPUT: null

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: Build and 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 out

- name: Save Deploy Output
run: |
echo "DEPLOY_OUTPUT=$(${{ steps.deploy.outputs.command-output }})" >> $GITHUB_ENV
notify_discord:
name: Notify Discord
runs-on: ubuntu-latest
needs: [lint, build_and_deploy]
if: always()

steps:
- name: Send Discord Notification
run: |
if [ "${{ needs.lint.result }}" == "failure" ]; then
STATUS="❌ Lint Failed"
COLOR=15158332
elif [ "${{ needs.build_and_deploy.result }}" == "failure" ]; then
STATUS="❌ Build and Deploy Failed"
COLOR=15158332
else
STATUS="✅ Build Succeeded"
COLOR=3066993
fi
PAYLOAD=$(cat <<EOF
{
"embeds": [
{
"title": "$STATUS",
"description": "Repository: [${{ github.repository }}](https://github.com/${{ github.repository }})\nBranch: ${{ github.ref_name }}\nWorkflow: ${{ github.workflow }}\n\n${{ env.DEPLOY_OUTPUT }}",
"color": $COLOR
}
]
}
EOF
)
curl -H "Content-Type: application/json" \
-X POST \
-d "$PAYLOAD" \
${{ 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 }}
30 changes: 30 additions & 0 deletions .github/workflows/update-i18n.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Update i18n

on:
workflow_dispatch:

jobs:
update_i18n:
name: Update i18n Translations
runs-on: ubuntu-latest

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

- name: Fetch i18n Translations
run: node fetchTranslations.js

- name: Check for i18n changes
id: check_changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add public/locales
git diff --cached --quiet || echo "changes=true" >> $GITHUB_ENV
- name: Commit i18n Changes
if: env.changes == 'true'
run: |
git commit -m "i18n: update translations"
git push
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ yarn-error.log*
/public/sw.js.map
/public/workbox-*.js
/public/workbox-*.js.map

.wrangler
certificates
78 changes: 4 additions & 74 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const withPWA = require('next-pwa')({
dest: 'public',
});

module.exports = withPWA({
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: false,
staticPageGenerationTimeout: 600,
transpilePackages: ['@mdxeditor/editor'],
Expand All @@ -30,77 +31,6 @@ module.exports = withPWA({
},
}
: {}),
// async redirects() {
// return [
// {
// source: "/learn/:title",
// destination: "/resource/:title",
// permanent: true,
// },
// {
// source: "/list",
// destination: "/search",
// permanent: true,
// },
// {
// source: "/category/learn/:cat",
// destination: "/search",
// permanent: true,
// },
// {
// source: "/developer",
// destination: "/about",
// permanent: true,
// },
// {
// source: "/tag:tag",
// destination: "/search?q=:tag",
// permanent: true,
// },
// {
// source: "/privacy-policy",
// destination: "/about",
// permanent: true,
// },
// {
// source: "/terms-of-service",
// destination: "/about",
// permanent: true,
// },
// ];
// },
};

// mode: "development",
// entry: path.resolve(__dirname, "src"),
// resolve: {
// extensions: ["", ".js", ".jsx"],
// },
// i18n: {
// /**
// * Provide the locales you want to support in your application
// */
// // TODO: Recover zh locale after Bing's review
// locales: ["zh"],
// /**
// * This is the default locale you want to be used when visiting
// * a non-locale prefixed path.
// */
// defaultLocale: "zh",
// ignoreRoutes: ["/api/"],
// localDetection: false,
// },
// async headers() {
// return [
// {
// source: "/:all*(svg|jpg|png|webp)",
// locale: false,
// headers: [
// {
// key: "Cache-Control",
// value: "public, max-age=2592000, must-revalidate",
// },
// ],
// },
// ];
// },
});
module.exports = withPWA(config);
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"export": "next build && next export",
"start": "next start",
"sitemap": "next-sitemap",
"lint": "eslint \"{pages,components,shared,constants}/**/*.{ts,tsx,js,jsx}\" --fix"
"lint": "eslint \"{pages,components,shared,constants}/**/*.{ts,tsx,js,jsx}\" --fix",
"pages:build": "npx @cloudflare/next-on-pages",
"preview": "npm run pages:build && wrangler pages dev",
"deploy": "npm run pages:build && wrangler pages deploy"
},
"dependencies": {
"@emotion/css": "^11.9.0",
Expand Down Expand Up @@ -68,10 +71,12 @@
"tailwind-merge": "^2.5.4",
"use-image": "^1.0.10",
"uuid": "^11.0.3",
"wrangler": "^3.99.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@babel/preset-typescript": "^7.26.0",
"@cloudflare/next-on-pages": "^1.13.7",
"@emotion/babel-plugin": "^11.9.2",
"@next/eslint-plugin-next": "^13.2.1",
"@tailwindcss/typography": "^0.5.15",
Expand All @@ -93,6 +98,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.14",
"typescript": "5.7.2"
"typescript": "5.7.2",
"vercel": "^39.2.4"
}
}
4 changes: 0 additions & 4 deletions shared/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
}

@layer base {
body {
@apply !bg-teal-50;
}

.prose li[role=checkbox] {
@apply indent-1 relative before:translate-y-1 after:absolute after:top-1/2 after:-translate-y-2/3 after:rotate-45;
}
Expand Down
6 changes: 6 additions & 0 deletions wrangler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "daodao-test",
"compatibility_date": "2024-07-29",
"compatibility_flags": ["nodejs_compat"],
"pages_build_output_dir": ".vercel/output/static"
}
Loading

0 comments on commit 4f705a3

Please sign in to comment.