Skip to content

Commit

Permalink
refactor: optimize CI/CD workflows
Browse files Browse the repository at this point in the history
- Simplify artifact handling to fix upload issues
- Add quality checks (lint and test) to both workflows
- Make preview checks non-blocking for PRs
- Enforce strict checks for production
- Remove unnecessary complexity
- Improve error handling and feedback
  • Loading branch information
aliakbar-deriv committed Jan 8, 2025
1 parent aab860c commit 5156da9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 130 deletions.
107 changes: 19 additions & 88 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,46 @@ on:
push:
branches: ['main']
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: true
default: 'production'
type: choice
options:
- production
- staging

permissions:
contents: read
pages: write
id-token: write
deployments: write

# Prevent concurrent deployments
concurrency:
group: "pages"
cancel-in-progress: false # Don't cancel production deployments

env:
NODE_ENV: production
cancel-in-progress: false

jobs:
validate:
name: Validate Production Build
quality:
name: Code Quality
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'

- name: Install dependencies
run: |
npm ci
id: npm-cache
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-prod-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-prod-${{ env.cache-name }}-
${{ runner.os }}-prod-
${{ runner.os }}-
- name: Production Build Test
run: npm run build
env:
VITE_APP_ENV: production

- name: Run Tests
run: npm test -- --coverage
- name: Install dependencies
run: npm ci

- name: Cache build validation
uses: actions/cache@v4
with:
path: dist
key: ${{ runner.os }}-prod-build-${{ github.sha }}
- name: Lint
run: npm run lint

- name: Run tests
run: npm test

deploy:
name: Deploy to Production
needs: validate
needs: quality
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.environment || 'production' }}
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -96,51 +61,17 @@ jobs:
run: npm run build
env:
VITE_APP_ENV: production
NODE_ENV: production

GITHUB_PAGES: true
BASE_URL: /copy-trading/

- name: Setup Pages
uses: actions/configure-pages@v4

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './dist'
path: dist

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v3

- name: Create Deployment Status
if: always()
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const environment = '${{ github.event.inputs.environment || 'production' }}';
const run_url = `https://github.com/${owner}/${repo}/actions/runs/${context.runId}`;
const status = `🚀 Deployment to ${environment} ${steps.deployment.outcome}`;
await github.rest.repos.createDeploymentStatus({
owner: owner,
repo: repo,
deployment_id: context.payload.deployment?.id,
state: steps.deployment.outcome === 'success' ? 'success' : 'failure',
environment_url: '${{ steps.deployment.outputs.page_url }}',
log_url: run_url,
description: status
});
- name: Notify on Failure
if: failure()
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const run_url = `https://github.com/${owner}/${repo}/actions/runs/${context.runId}`;
const issue = await github.rest.issues.create({
owner: owner,
repo: repo,
title: '❌ Production Deployment Failed',
body: `Deployment to production failed.\n\n[View Details](${run_url})`,
labels: ['deployment', 'bug']
});
58 changes: 16 additions & 42 deletions .github/workflows/preview-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ permissions:
pages: write
id-token: write
pull-requests: write
security-events: write

# Cancel in-progress runs for PRs
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
group: "preview-${{ github.ref }}"
cancel-in-progress: true

jobs:
Expand All @@ -24,36 +22,29 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint
continue-on-error: true

- name: Type check
run: |
npm run typecheck || echo "Type check failed but continuing"
continue-on-error: true

- name: Run unit tests
- name: Run tests
run: npm test
continue-on-error: true

preview_and_test:
name: Preview Deploy & E2E
deploy-preview:
needs: quality
runs-on: ubuntu-latest
environment:
name: pr-preview
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
Expand All @@ -73,41 +64,25 @@ jobs:
run: npm run build
env:
VITE_APP_ENV: preview
GITHUB_PAGES: true
BASE_URL: /copy-trading/

- name: Cache build output
uses: actions/cache@v4
with:
path: dist
key: ${{ runner.os }}-build-${{ github.sha }}

- name: Setup Pages
uses: actions/configure-pages@v4

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './dist'
path: dist

- name: Deploy PR Preview
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v3
with:
preview: true

- name: Handle Deploy Failure
if: failure() && steps.deployment.outcome == 'failure'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '❌ Preview deployment failed. Check the workflow logs for details.'
});
core.setFailed('Preview deployment failed');

- name: Comment PR
if: success()
uses: actions/github-script@v7
with:
script: |
Expand All @@ -116,7 +91,7 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 Preview URL: ${url}`
body: `🚀 Preview deployed to: ${url}`
});
- name: Check required secrets
Expand All @@ -129,7 +104,6 @@ jobs:
echo "::error::Missing required secret: AUTOMAGICALLY_TEST_TARGET_ID"
exit 1
fi
- name: Run E2E tests
id: e2e
uses: OctoMind-dev/automagically-action-execute@v2
Expand Down Expand Up @@ -159,4 +133,4 @@ jobs:
owner: owner,
repo: repo,
body: body
});
});

0 comments on commit 5156da9

Please sign in to comment.