Update spendmanagement-identity-api.yml #136
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: build_and_run_tests | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
VERSION_UPDATE_TYPE: "value" | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: 8.0.x | |
- name: Restore dependencies | |
run: dotnet restore src/Identity.Api/Identity.Api.csproj | |
- name: Build | |
run: dotnet build --no-restore --configuration Release src/Identity.Api/Identity.Api.csproj | |
unit-tests: | |
runs-on: ubuntu-latest | |
needs: [build] | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: 8.0.x | |
- name: Restore dependencies for unit tests | |
run: dotnet restore tests/Identity.UnitTests/Identity.UnitTests.csproj | |
- name: Build project | |
run: dotnet build --no-restore --configuration Release tests/Identity.UnitTests/Identity.UnitTests.csproj | |
- name: Run unit tests | |
run: dotnet test --verbosity normal tests/Identity.UnitTests/Identity.UnitTests.csproj | |
integration-tests: | |
runs-on: ubuntu-latest | |
needs: [unit-tests] | |
steps: | |
- name: Setup PostgreSQL | |
run: | | |
docker run -d --name postgres -p 5432:5432 \ | |
-e POSTGRES_PASSWORD=postgres \ | |
--health-cmd pg_isready \ | |
--health-interval 10s \ | |
--health-timeout 5s \ | |
--health-retries 5 \ | |
postgres | |
- name: Wait for PostgreSQL to start | |
run: | | |
until docker exec postgres pg_isready; do | |
sleep 1; | |
done | |
- uses: actions/checkout@v4 | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v4 | |
with: | |
dotnet-version: 8.0.x | |
- name: Run integration tests | |
run: | | |
dotnet restore tests/Identity.IntegrationTests/Identity.IntegrationTests.csproj | |
dotnet build --no-restore --configuration Release tests/Identity.IntegrationTests/Identity.IntegrationTests.csproj | |
dotnet test --verbosity normal tests/Identity.IntegrationTests/Identity.IntegrationTests.csproj | |
bump-project-version: | |
runs-on: ubuntu-latest | |
needs: [unit-tests, integration-tests] | |
if: github.ref == 'refs/heads/main' | |
name: Build and bump version | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Determinar Tipo de Mudança | |
id: determine_change_type | |
run: | | |
LAST_COMMIT_MESSAGE=$(git log -1 --pretty=%B) | |
if echo "$LAST_COMMIT_MESSAGE" | grep -qiE "feat"; then | |
echo "VERSION_UPDATE_TYPE=MINOR" >> $GITHUB_ENV | |
fi | |
if echo "$LAST_COMMIT_MESSAGE" | grep -qiE "fix"; then | |
echo "VERSION_UPDATE_TYPE=REVISION" >> $GITHUB_ENV | |
fi | |
- name: Print Update type | |
run: | | |
echo "Tipo de Mudança: $VERSION_UPDATE_TYPE" | |
- name: Bump build version - Minor | |
if: env.VERSION_UPDATE_TYPE == 'MINOR' | |
id: bump-minor | |
uses: vers-one/dotnet-project-version-updater@v1.5 | |
with: | |
file: src/Identity.Api/Identity.Api.csproj | |
version: "*.^.0" | |
- name: Bump build version - Revision | |
if: env.VERSION_UPDATE_TYPE == 'REVISION' | |
id: bump-revision | |
uses: vers-one/dotnet-project-version-updater@v1.5 | |
with: | |
file: src/Identity.Api/Identity.Api.csproj | |
version: "*.*.^" | |
- name: Commit and push changes | |
if: env.VERSION_UPDATE_TYPE == 'REVISION' || env.VERSION_UPDATE_TYPE == 'MINOR' | |
run: | | |
git config user.name "Build - Incrementing version | Github action" | |
git config user.email "deploy@spendmanagement.com" | |
git add . | |
git commit -m "CI: Updating application version" | |
git push | |
publish-docker-image: | |
runs-on: ubuntu-latest | |
needs: [bump-project-version] | |
if: github.ref == 'refs/heads/main' | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Get csproj version | |
id: get_version | |
run: | | |
version=$(grep -oP '<Version>([^<]+)</Version>' src/Identity.Api/Identity.Api.csproj | grep -oP '(\d+\.\d+\.\d+)') | |
echo "csproj_version=$version" >> $GITHUB_ENV | |
echo "The csproj version is $csproj_version" | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Display appsettings before | |
run: cat src/Identity.Api/appsettings.Development.json | |
- name: Change connection string | |
run: | | |
sed -i 's/localhost/postgresql/g' src/Identity.Api/appsettings.Development.json | |
working-directory: ${{ github.workspace }} | |
- name: Display appsettings after | |
run: cat src/Identity.Api/appsettings.Development.json | |
- name: Build Docker image | |
run: docker build -t "fmattioli/spendmanagement-identity-api:$csproj_version" . | |
- name: Log in to Docker Hub | |
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | |
- name: Push Docker image to Docker Hub | |
run: | | |
docker push "fmattioli/spendmanagement-identity-api:$csproj_version" | |
docker tag "fmattioli/spendmanagement-identity-api:$csproj_version" "fmattioli/spendmanagement-identity-api:latest" | |
docker push "fmattioli/spendmanagement-identity-api:latest" | |