From 706388959a4cec847d3c54ad488635c09d386f29 Mon Sep 17 00:00:00 2001 From: FerdiHS <92571864+FerdiHS@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:41:15 +0800 Subject: [PATCH] Setup initial CI and CD workflow files (#41) --- .github/workflows/ci.yml | 26 ++++++++++++ .github/workflows/deploy-live.yml | 28 +++++++++++++ .github/workflows/deploy-preview.yml | 28 +++++++++++++ .github/workflows/deploy.yml | 60 ++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/deploy-live.yml create mode 100644 .github/workflows/deploy-preview.yml create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7214c6f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: CI + +on: + push: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '20' + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test + + - name: Build application + run: npm run build \ No newline at end of file diff --git a/.github/workflows/deploy-live.yml b/.github/workflows/deploy-live.yml new file mode 100644 index 0000000..589e766 --- /dev/null +++ b/.github/workflows/deploy-live.yml @@ -0,0 +1,28 @@ +name: Live Deployment + +on: + push: + branches: + - main + - staging + +env: + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + +jobs: + deploy-preproduction-preview: + uses: ./.github/workflows/deploy.yml + if: github.ref_name == 'staging' + with: + environment: preproduction + preview: false + secrets: inherit + deploy-production-preview: + uses: ./.github/workflows/deploy.yml + if: github.ref_name == 'main' + with: + environment: production + preview: false + secrets: inherit + diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml new file mode 100644 index 0000000..ed75eb0 --- /dev/null +++ b/.github/workflows/deploy-preview.yml @@ -0,0 +1,28 @@ +name: Preview Deployment + +on: + pull_request: + branches: + - main + - staging + +env: + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + +jobs: + deploy-preproduction-preview: + uses: ./.github/workflows/deploy.yml + if: github.base_ref == 'staging' + with: + environment: preproduction + preview: true + secrets: inherit + deploy-production-preview: + uses: ./.github/workflows/deploy.yml + if: github.base_ref == 'main' + with: + environment: production + preview: true + secrets: inherit + diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..e6fbe4e --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,60 @@ +name: Deploy to Vercel +on: + workflow_call: + inputs: + environment: + required: true + type: string + preview: + required: false + type: boolean + default: true + +env: + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '20' + - name: Install dependencies + run: npm install + - name: Run tests + run: npm test + deploy-preview: + runs-on: ubuntu-latest + needs: test + environment: ${{ inputs.environment }} + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Set up environment variables + run: | + echo "VERCEL_ORG_ID=${{ secrets.VERCEL_ORG_ID }}" >> $GITHUB_ENV + echo "VERCEL_PROJECT_ID=${{ secrets.VERCEL_PROJECT_ID }}" >> $GITHUB_ENV + if [ ${{ inputs.preview }} = true ]; then + echo "VERCEL_ENVIRONMENT=preview" >> $GITHUB_ENV + echo "VERCEL_DEPLOYMENT_FLAG=" >> $GITHUB_ENV + else + echo "VERCEL_ENVIRONMENT=production" >> $GITHUB_ENV + echo "VERCEL_DEPLOYMENT_FLAG=--prod" >> $GITHUB_ENV + fi + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '20' + - name: Install dependencies + run: npm install + - name: Install Vercel CLI + run: npm install -g vercel + - name: Pull Vercel Environment Information + run: vercel pull --yes --environment=$VERCEL_ENVIRONMENT --token=${{ secrets.VERCEL_TOKEN }} + - name: Deploy to Vercel Preview + run: vercel $VERCEL_DEPLOYMENT_FLAG --token=${{ secrets.VERCEL_TOKEN }}