🚀 Enhance CI/CD: Add Docker build and push to DockerHub #160
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Pipeline For Playwright Tests and Vercel Deployment | |
on: | |
push: | |
branches: [main, "feature/**", "fix-**"] | |
pull_request: | |
branches: [main, "feature/**", "fix-**"] | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 18 | |
- name: Cache dependencies | |
uses: actions/[email protected] | |
with: | |
path: ~/.npm | |
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.OS }}-node- | |
- name: Install dependencies | |
run: npm ci | |
- name: Run linter | |
run: npm run lint | |
- name: Cache Playwright browsers | |
uses: actions/[email protected] | |
with: | |
path: ~/.cache/ms-playwright | |
key: ${{ runner.OS }}-playwright-${{ hashFiles('**/package-lock.json') }} | |
- name: Install Playwright Browsers | |
run: npx playwright install --with-deps | |
- name: Run Playwright tests | |
run: npm test | |
- uses: actions/upload-artifact@v4 | |
if: always() | |
with: | |
name: playwright-report | |
path: playwright-report/ | |
retention-days: 30 | |
deploy-preview: | |
needs: test | |
if: github.event_name == 'push' && (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/fix-')) | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 18 | |
- name: Install Vercel CLI | |
run: npm install --global vercel@latest | |
- name: Pull Vercel Environment Information | |
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} | |
- name: Deploy to Vercel Preview | |
run: vercel deploy --token=${{ secrets.VERCEL_TOKEN }} | |
deploy-production: | |
needs: test | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 18 | |
- name: Install Vercel CLI | |
run: npm install --global vercel@latest | |
- name: Pull Vercel Environment Information | |
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }} | |
- name: Deploy to Vercel Production | |
run: vercel deploy --prod --token=${{ secrets.VERCEL_TOKEN }} | |
build-and-push-docker: | |
needs: [test, deploy-production] | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/[email protected] | |
- name: Set up Docker Buildx | |
uses: docker/[email protected] | |
- name: Login to DockerHub | |
uses: docker/[email protected] | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push | |
uses: docker/[email protected] | |
with: | |
context: . | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/pft:latest |