feat: sonar main #34
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: Frontend CI/CD | |
on: | |
push: | |
branches: | |
- main | |
- production | |
pull_request: | |
branches: | |
- main | |
- production | |
jobs: | |
# Gradnja frontenda | |
build-frontend: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Cache node_modules | |
uses: actions/cache@v3 | |
with: | |
path: 04_DevOps_Deploying/primer/frontend/node_modules | |
key: ${{ runner.os }}-frontend-${{ hashFiles('04_DevOps_Deploying/primer/frontend/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-frontend- | |
- name: Install dependencies | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm ci | |
- name: Build application | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm run build | |
- name: Upload build artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: frontend-build | |
path: 04_DevOps_Deploying/primer/frontend/build/ | |
# Testiranje frontenda | |
test-frontend: | |
needs: build-frontend | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Cache node_modules | |
uses: actions/cache@v3 | |
with: | |
path: 04_DevOps_Deploying/primer/frontend/node_modules | |
key: ${{ runner.os }}-frontend-${{ hashFiles('04_DevOps_Deploying/primer/frontend/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-frontend- | |
- name: Install dependencies | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm install | |
- name: Run tests with coverage | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm run test:ci | |
- name: Upload coverage report | |
uses: actions/upload-artifact@v4 | |
with: | |
name: frontend-coverage | |
path: 04_DevOps_Deploying/primer/frontend/coverage/ | |
# Gradnja Docker slike in nalaganje na Docker Hub - Development | |
dockerize-and-push-dev: | |
needs: test-frontend | |
runs-on: ubuntu-latest | |
environment: Development | |
if: github.ref == 'refs/heads/main' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_TOKEN }} | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/frontend:dev ./04_DevOps_Deploying/primer/frontend | |
docker tag ${{ secrets.DOCKER_USERNAME }}/frontend:dev ${{ secrets.DOCKER_USERNAME }}/frontend:${{ github.sha }} | |
- name: Push Docker image to Docker Hub | |
run: | | |
docker push ${{ secrets.DOCKER_USERNAME }}/frontend:dev | |
docker push ${{ secrets.DOCKER_USERNAME }}/frontend:${{ github.sha }} | |
# SonarCloud analiza | |
sonarcloud-analysis: | |
needs: test-frontend | |
runs-on: ubuntu-latest | |
environment: | |
name: Development | |
if: github.ref == 'refs/heads/main' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: Setup Java 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Install dependencies | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm ci | |
- name: Collect coverage (React-specific) | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm run test:ci -- --coverage | |
env: | |
CI: true | |
- name: Upload coverage report | |
run: | | |
cp 04_DevOps_Deploying/primer/frontend/coverage/lcov.info . | |
- name: Run SonarCloud analysis | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
run: | | |
npx sonar-scanner \ | |
-Dsonar.projectKey=${{ secrets.PROJECT_KEY }} \ | |
-Dsonar.organization=${{ secrets.ORGANIZATION_KEY }} \ | |
-Dsonar.sources=./04_DevOps_Deploying/primer/frontend \ | |
-Dsonar.host.url=https://sonarcloud.io \ | |
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info | |
# Gradnja Docker slike in nalaganje na Docker Hub - Production | |
dockerize-and-push-prod: | |
needs: [test-frontend, sonarcloud-analysis] | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/production' | |
environment: | |
name: Production | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Check SonarCloud Quality Gate | |
run: | | |
curl -u ${{ secrets.SONAR_TOKEN }}: \ | |
"https://sonarcloud.io/api/project_badges/measure?project=${{ secrets.PROJECT_KEY }}&metric=alert_status" | grep -q "OK" || exit 1 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_TOKEN }} | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/frontend:prod ./04_DevOps_Deploying/primer/frontend | |
docker tag ${{ secrets.DOCKER_USERNAME }}/frontend:prod ${{ secrets.DOCKER_USERNAME }}/frontend:${{ github.sha }} | |
- name: Push Docker image to Docker Hub | |
run: | | |
docker push ${{ secrets.DOCKER_USERNAME }}/frontend:prod | |
docker push ${{ secrets.DOCKER_USERNAME }}/frontend:${{ github.sha }} | |
# Deploy na Firebase Hosting | |
deploy-to-firebase: | |
needs: test-frontend | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Download build artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: frontend-build | |
path: 04_DevOps_Deploying/primer/frontend/build/ | |
- name: Setup Firebase CLI | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
npm install -g firebase-tools | |
- name: Deploy to Firebase Hosting | |
run: | | |
cd 04_DevOps_Deploying/primer/frontend | |
firebase deploy --only hosting --token ${{ secrets.FIREBASE_TOKEN }} | |
# Uvedba na GitHub Pages | |
deploy-pages: | |
needs: test-frontend | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Deploy to GitHub Pages | |
uses: peaceiris/actions-gh-pages@v3 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_dir: ./docs |