Skip to content

Commit

Permalink
chore(ci/cd): add actions for pr, coverage, and package publication
Browse files Browse the repository at this point in the history
  • Loading branch information
willianantunes committed Sep 15, 2024
1 parent 735ec7e commit 39d6d63
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Validate PR

on:
pull_request:
branches:
- main
paths:
- 'src/**'
- 'tests/**'
- '*.sln'
- 'Dockerfile'

jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Shallow clones should be disabled for a better relevancy of analysis
fetch-depth: 0
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8'
- name: Check if the project is well formatted
run: |
./scripts/start-check-formatting.sh
- name: Install dotnet-sonarscanner
run: |
dotnet tool install --global dotnet-sonarscanner
- name: Build the project, run all tests, and publish the test results
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
PR_SOURCE_BRANCH: ${{ github.head_ref }}
PR_TARGET_BRANCH: ${{ github.base_ref }}
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
run: |
set -e
docker compose up -d db
./scripts/start-sonarcloud.sh
49 changes: 49 additions & 0 deletions .github/workflows/publish-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Publish package and coverage report

on:
push:
branches:
- main
paths:
- 'src/**'
- 'tests/**'
- '*.sln'
- 'Directory.Build.props'

jobs:
publish-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Shallow clones should be disabled for a better relevancy of analysis
fetch-depth: 0
- name: Install dotnet-sonarscanner
run: |
dotnet tool install --global dotnet-sonarscanner
- name: Build the project, run all tests and publish to SonarCloud
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
PROJECT_VERSION: ${{ github.sha }}
SOURCE_BRANCH_NAME: ${{ github.ref_name }}
run: |
set -e
docker compose up -d db
./scripts/start-sonarcloud.sh
publish-package:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Generate package
run: |
dotnet pack ./src -c Release -o out -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg
- name: Push nuget
run: |
dotnet nuget push out/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source ${{ secrets.NUGET_SERVER }} --skip-duplicate
- name: Publish to NuGet
run: |
./scripts/start-nuget.sh ${{ secrets.NUGET_API_KEY }}
29 changes: 29 additions & 0 deletions .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish coverage report

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+*'
workflow_dispatch:

permissions:
contents: read

jobs:
publish-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Generate package
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
dotnet pack -o out -p:PackageVersion=$TAG_NAME
- name: Push package
run: |
dotnet nuget push out/*.nupkg \
--api-key ${{ secrets.PUBLIC_NUGET_API_KEY }} \
--source "https://api.nuget.org/v3/index.json" \
--skip-duplicate
33 changes: 33 additions & 0 deletions scripts/start-sonarcloud.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -eu -o pipefail

# You should start the scanner prior building your project and running your tests
if [ -n "${PR_SOURCE_BRANCH:-}" ]; then
dotnet sonarscanner begin \
/d:sonar.login="$SONAR_TOKEN" \
/k:"juntossomosmais_AspNetCore.RestFramework" \
/o:"juntossomosmais" \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.cs.opencover.reportsPaths="**/*/coverage.opencover.xml" \
/d:sonar.cs.vstest.reportsPaths="**/*/*.trx" \
/d:sonar.pullrequest.base="$PR_TARGET_BRANCH" \
/d:sonar.pullrequest.branch="$PR_SOURCE_BRANCH" \
/d:sonar.pullrequest.key="$GITHUB_PR_NUMBER"
else
dotnet sonarscanner begin \
/d:sonar.login="$SONAR_TOKEN" \
/v:"$PROJECT_VERSION" \
/k:"juntossomosmais_AspNetCore.RestFramework" \
/o:"juntossomosmais" \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.cs.opencover.reportsPaths="**/*/coverage.opencover.xml" \
/d:sonar.cs.vstest.reportsPaths="**/*/*.trx" \
/d:sonar.branch.name="$SOURCE_BRANCH_NAME"
fi

# Now we can run our tests as usual
./scripts/start-tests.sh

# Now we can collect the results 👍
dotnet sonarscanner end /d:sonar.login="$SONAR_TOKEN"
2 changes: 1 addition & 1 deletion scripts/start-tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

Expand Down

0 comments on commit 39d6d63

Please sign in to comment.