Skip to content

CI: Optimize CI workflow to run tests and linting only on relevant file changes #12

CI: Optimize CI workflow to run tests and linting only on relevant file changes

CI: Optimize CI workflow to run tests and linting only on relevant file changes #12

Workflow file for this run

name: Frontend Test & Lint
on:
push:
paths:
- 'frontend/**'
- '.yarn'
pull_request:
paths:
- 'frontend/**'
- '.yarn'
jobs:
frontend-test:
name: Test Frontend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Cache Yarn dependencies
- name: Cache Yarn
uses: actions/cache@v2
with:
path: ~/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-
# Set up Docker Buildx for caching
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# Cache Docker layers
- name: Cache Docker layers
id: cache-docker-layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: |
${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}
${{ runner.os }}-docker
- name: Build Frontend Docker image
run: |
docker build --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,dest=/tmp/.buildx-cache -t muscle-frontend-image frontend
- name: Run Frontend Tests
run: sudo docker-compose --env-file .env-github-actions run client yarn test:ci
frontend-coverage-badge:
name: Generate Frontend Coverage Badge
needs: frontend-test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
steps:
- uses: actions/checkout@v3
# Cache Yarn dependencies
- name: Cache Yarn
uses: actions/cache@v2
with:
path: ~/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-
# Set up Docker Buildx for caching
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# Cache Docker layers
- name: Cache Docker layers
id: cache-docker-layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: |
${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}
${{ runner.os }}-docker
- name: Build Frontend Docker image
run: |
docker build --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,dest=/tmp/.buildx-cache -t muscle-frontend-image frontend
- name: Generate Frontend Coverage Report (XML) and Badge
run: |
sudo docker-compose --env-file .env-github-actions run client yarn test:ci
sudo docker-compose --env-file .env-github-actions run client yarn coverage-badges -s public/coverage/coverage-summary.json -o public/coverage/coverage-frontend-badge-new.svg --label 'Frontend Code Coverage'
- name: Push Frontend Coverage Badge to separate branch
continue-on-error: true
run: |
if git ls-remote --heads origin code-coverage-badges; then
git fetch origin code-coverage-badges
git checkout code-coverage-badges
else
git checkout -b code-coverage-badges
git push origin code-coverage-badges
fi
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
rm frontend/coverage-frontend-badge.svg -f
sudo mv frontend/public/coverage/coverage-frontend-badge-new.svg frontend/coverage-frontend-badge.svg
git add frontend/coverage-frontend-badge.svg
git commit -m "Add frontend coverage badge for commit $GITHUB_SHA"
git push origin code-coverage-badges
# Check if there are any changes
if git diff --staged --quiet; then
echo "No changes in coverage badge. Skipping commit and push."
else
git commit -m "Add frontend coverage badge for commit $GITHUB_SHA"
git push origin code-coverage-badges
fi
frontend-lint:
name: Lint Frontend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: sudo docker-compose --env-file .env-github-actions run client yarn lint
# Cache Yarn dependencies
- name: Cache Yarn
uses: actions/cache@v2
with:
path: ~/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-
# Set up Docker Buildx for caching
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# Cache Docker layers
- name: Cache Docker layers
id: cache-docker-layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}-${{ hashFiles('frontend/yarn.lock') }}
restore-keys: |
${{ runner.os }}-docker-${{ hashFiles('frontend/Dockerfile') }}
${{ runner.os }}-docker
- name: Build Frontend Docker image
run: |
docker build --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,dest=/tmp/.buildx-cache -t muscle-frontend-image frontend
- run: sudo docker-compose --env-file .env-github-actions run client yarn lint