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 549e7ce commit c6e73af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 145 deletions.
121 changes: 26 additions & 95 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,52 @@ 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@v3
with:
fetch-depth: 0
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
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@v3
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@v3
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'
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@v3

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

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

- name: Create Deployment Status
if: always()
uses: actions/github-script@v6
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@v6
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']
});
uses: actions/deploy-pages@v3
75 changes: 25 additions & 50 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 @@ -23,45 +21,38 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all history for all branches and tags
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
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:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'
Expand All @@ -73,50 +64,34 @@ jobs:
run: npm run build
env:
VITE_APP_ENV: preview
GITHUB_PAGES: true
BASE_URL: /copy-trading/

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

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

uses: actions/configure-pages@v4
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
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@v2
uses: actions/deploy-pages@v3
with:
preview: true

- name: Handle Deploy Failure
if: failure() && steps.deployment.outcome == 'failure'
uses: actions/github-script@v6
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
uses: actions/github-script@v6
if: success()
uses: actions/github-script@v7
with:
script: |
const url = `${{ steps.deployment.outputs.page_url }}`;
github.rest.issues.createComment({
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 All @@ -142,7 +116,7 @@ jobs:

- name: Report Workflow Status
if: always()
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
Expand All @@ -160,3 +134,4 @@ jobs:
repo: repo,
body: body
});

0 comments on commit c6e73af

Please sign in to comment.