Skip to content

Commit

Permalink
ci: 🎡 changes in the ci/cd
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Jan 20, 2023
1 parent 0c39a1f commit 7fd3394
Show file tree
Hide file tree
Showing 93 changed files with 568 additions and 260 deletions.
278 changes: 77 additions & 201 deletions .github/workflows/ci-cd.yml

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions .github/workflows/composite/build/reusable-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build dotnet application

on:
workflow_call:
inputs:
dotnet-version:
description: Version of dotnet to use
type: string
default: 7.X.x

runs-on:
description: Platform to execute on
type: string
default: ubuntu-latest

project-folder:
description: The folder containing the project to build
type: string
default: .

run-tests:
description: Run tests
type: boolean
default: true
200 changes: 200 additions & 0 deletions .github/workflows/composite/deploy/reusable-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
name: Reusable deployment workflow

on:
workflow_call:
inputs:
environment_name:
type: string
description: environment to deploy to
required: true
environment_tag:
type: string
description: environment tag fo publishing
required: true
secrets:
token:
required: true

jobs:
deploy:
name: Deploy to ${{ inputs.environment_name }}
runs-on: ubuntu-latest

# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif
if: ${{ contains(fromJson('["develop", "devops/ci", "main", "beta", "preview"]'), github.ref_name) && github.event_name != 'pull_request' }}

# https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment
environment:
name: ${{ inputs.environment_name }}
# url: ${{ steps.deploy-to-heroku.outputs.webapp-url }}

# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idpermissions
# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#upgrading-a-workflow-that-accesses-ghcrio
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
packages: write # for publishing packages

steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'

- name: Cache NuGet Packages
uses: actions/cache@v3
with:
key: ${{ runner.os }}-dotnet-nuget
path: ~/.nuget/packages

- name: create output dir
run: mkdir "output"

# https://github.com/actions/download-artifact#download-all-artifacts
# download artifacts without name will download all artifacts
- name: Download All Artifacts
uses: actions/download-artifact@v3
with:
path: artifacts

- name: dir
run: ls -R "${{ github.workspace }}/artifacts/build-test-artifacts"

# https://askubuntu.com/questions/86849/how-to-unzip-a-zip-file-from-the-terminal
- name: unzip artifacts
run: |
unzip "artifacts/build-test-artifacts/test-results.zip" -d "output"
- name: Ls Output Files
if: success()
run: ls -R ${{ github.workspace }}/output

# merging all coverage in a single coverage file and generating html files for downloading as artifacts
- name: Publish coverage report to coveralls.io
if: success()
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.token }}
path-to-lcov: "output/test-results/lcov.info"

# https://thecodinganalyst.github.io/knowledgebase/Basic-guide-to-Semantic-Release/
# https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b
# https://github.com/michael-wolfenden/Cake.Npx/blob/master/src/Cake.Npx.Tests/NpxAliasesTests.cs#L59
# https://medium.com/@michael.wolfenden/simplified-versioning-and-publishing-for-net-libraries-a28e5e740fa6
# https://github.com/semantic-release/semantic-release/issues/753#issuecomment-706971174
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
# will assmblies versions and push them to git
- name: Semantic Release
id: semantic
env:
GITHUB_TOKEN: ${{ secrets.token }}
# because of using none default (conventionalcommits) `preset` for `semantic-release`, we should add dependency `conventional-changelog-conventionalcommits`
run: npx -p conventional-changelog-conventionalcommits -p @semantic-release/git -p @semantic-release/changelog -p @semantic-release/exec semantic-release

- name: Semantic Release Outputs
run: |
echo ${{ steps.semantic.outputs.semantic_nextRelease_version }}
echo ${{ steps.semantic.outputs.semantic_nextRelease_channel }}
echo ${{ steps.semantic.outputs.semantic_nextRelease_gitTag }}
echo ${{ steps.semantic.outputs.semantic_lastRelease_version }}
echo ${{ steps.semantic.outputs.semantic_lastRelease_channel }}
echo ${{ steps.semantic.outputs.semantic_lastRelease_gitTag }}
- name: Restore NuGet packages
continue-on-error: false
run: dotnet restore

# https://garywoodfine.com/how-to-use-github-actions-to-build-deploy-github-nuget-packages/
# https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-7.0#the-dockerfile
# or calling dotnet publish on solution - we marked `publishable` and `packable` projects in their .csproj
- name: dotnet publish
continue-on-error: false
id: publish-services
if: success()
run: |
dotnet publish -c Release --no-restore ${{ env.CATALOG_SVC_PATH }} -o ${{ github.workspace }}/output/publish-services/Catalogs /p:Version=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:AssemblyVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:InformationalVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }}
dotnet publish -c Release --no-restore ${{ env.CUSTOMERS_SVC_PATH }} -o ${{ github.workspace }}/output/publish-services/Customers /p:Version=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:AssemblyVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:InformationalVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }}
dotnet publish -c Release --no-restore ${{ env.IDENTITY_SVC_PATH }} -o ${{ github.workspace }}/output/publish-services/Identity /p:Version=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:AssemblyVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }} /p:InformationalVersion=${{ steps.semantic.outputs.semantic_nextRelease_version }}
# https://unix.stackexchange.com/questions/57013/zip-all-files-in-directory
# https://limeii.github.io/2022/11/deploy-to-azure-appservice-with-github-actions/
# https://stackoverflow.com/questions/68470162/how-to-archive-files-in-artifact-for-github-workflow-actions-in-order-to-fix-thi
# note: we should zip inner (publish-services) folder and for doing this we use `cd output` first then zip
# `publish-services/*` folder, otherwise with zip `output/publish-services` it zip output folder and inner `publish-services`folder together
- name: Zip publish-services Artifacts
run: |
cd output
zip -r publish-services.zip publish-services/*
# https://github.com/actions/runner/issues/946#issuecomment-900193569
# https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context
- name: Ls Output Files
if: success()
run: ls -R
working-directory: ${{ github.workspace }}/output

- name: Upload Publish Services Artifacts For Deployment Jobs
uses: actions/upload-artifact@v3
if: (steps.publish-services.outcome == 'success')
with:
name: publish-services
retention-days: 1
path: |
output/publish-services.zip
# https://docs.docker.com/engine/reference/commandline/build/
# https://dev.to/willvelida/pushing-container-images-to-github-container-registry-with-github-actions-1m6b
# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#upgrading-a-workflow-that-accesses-ghcrio
- name: docker build
run: |
docker build . --tag ${{ env.REGISTRY }}/${{ github.repository }}/catalogs-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }} -f "${{ github.workspace }}/src/Services/Catalogs/Dockerfile"
docker build . --tag ${{ env.REGISTRY }}/${{ github.repository }}/customers-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }} -f "${{ github.workspace }}/src/Services/Customers/Dockerfile"
docker build . --tag ${{ env.REGISTRY }}/${{ github.repository }}/identity-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }} -f "${{ github.workspace }}/src/Services/Identity/Dockerfile"
- name: 'Login to GitHub Container Registry'
uses: docker/login-action@v1
if: success()
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.token }}

- name: docker publish
if: success()
run: |
docker push ghcr.io/${{ github.repository }}/catalogs-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }}
docker push ghcr.io/${{ github.repository }}/customers-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }}
docker push ghcr.io/${{ github.repository }}/identity-service:${{ steps.semantic.outputs.semantic_nextRelease_version }}${{ inputs.environment_tag }}
# release-production:
# if: ${{ contains(fromJson('["main", "beta", "preview"]'), github.ref_name) }}
# runs-on: ubuntu-latest
# # https://docs.github.com/en/actions/using-workflows/about-workflows#creating-dependent-jobs
# needs: build-test
# environment: production
#
# steps:
# # https://github.com/actions/download-artifact#download-all-artifacts
# - name: Download Artifacts
# uses: actions/download-artifact@v3
# with:
# path: artifacts
#
# # merging all coverage in a single coverage file and generating html files for downloading as artifacts
# - name: Publish coverage report to coveralls.io
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.token }}
# path-to-lcov: 'artifacts/test-results/lcov.info'
#
# # - name: publish docker image
# # run: |
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": "file:///d%3A/Github/ecommerce-microservices-sample/.github/workflows/composite/deploy/deploy.yml"
}
}
15 changes: 15 additions & 0 deletions ecommerce.sln
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{10D42B
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Shared", "tests\Shared\Tests.Shared\Tests.Shared.csproj", "{CBF89154-C73E-4A22-9F89-2AA4A7EC5333}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "composite", "composite", "{5D7678E0-36F9-453A-9211-39BD4ACD0BBB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "deploy", "deploy", "{D029D027-C381-42EF-A289-8B3AB82758AC}"
ProjectSection(SolutionItems) = preProject
.github\workflows\composite\deploy\reusable-deploy.yml = .github\workflows\composite\deploy\reusable-deploy.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{D04AB414-7955-49E1-91CC-891247CD780E}"
ProjectSection(SolutionItems) = preProject
.github\workflows\composite\build\reusable-build.yml = .github\workflows\composite\build\reusable-build.yml
EndProjectSection
EndProject

Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -301,6 +313,9 @@ Global
{8E56D5F0-07CC-46D5-86E4-599060198971} = {CFC90B50-E918-4ABD-B27B-CA87F12BAF89}
{10D42B11-A3EA-4969-9782-EE60EA7A8448} = {9EEA45F9-0190-4CCD-908F-ED883FE66DE0}
{CBF89154-C73E-4A22-9F89-2AA4A7EC5333} = {10D42B11-A3EA-4969-9782-EE60EA7A8448}
{5D7678E0-36F9-453A-9211-39BD4ACD0BBB} = {3CC5AECD-915C-4CCB-90AA-EF29EBD9A3E5}
{D029D027-C381-42EF-A289-8B3AB82758AC} = {5D7678E0-36F9-453A-9211-39BD4ACD0BBB}
{D04AB414-7955-49E1-91CC-891247CD780E} = {5D7678E0-36F9-453A-9211-39BD4ACD0BBB}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9063E326-1CDF-4CA7-8D72-CA15AB532EEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
12 changes: 6 additions & 6 deletions gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ tasks:
dotnet restore
dotnet build
#https://www.gitpod.io/docs/configure/workspaces/ports#exposing-ports
#https://github.com/gitpod-io/gitpod/issues/1867
ports:
- port: 2000-9000
onOpen: ignore
visibility: private
##https://www.gitpod.io/docs/configure/workspaces/ports#exposing-ports
##https://github.com/gitpod-io/gitpod/issues/1867
#ports:
# - port: 2000-9000
# onOpen: ignore
# visibility: private
2 changes: 2 additions & 0 deletions src/BuildingBlocks/BuildingBlocks.Caching/CacheKey.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Caching;

Expand Down
2 changes: 2 additions & 0 deletions src/BuildingBlocks/BuildingBlocks.Caching/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Ardalis.GuardClauses;
using BuildingBlocks.Abstractions.Caching;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection;
using BuildingBlocks.Core.Utils;
using BuildingBlocks.Core.Web.Extenions;
using EasyCaching.Redis;
using Microsoft.AspNetCore.Builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using BuildingBlocks.Abstractions.Messaging;
using BuildingBlocks.Core.CQRS.Events.Internal;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Core.CQRS.Events;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BuildingBlocks.Abstractions.Scheduler;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Types.Extensions;
using MediatR;
using Newtonsoft.Json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using BuildingBlocks.Abstractions.Domain.EventSourcing;
using BuildingBlocks.Core.Domain.Exceptions;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Core.Domain.EventSourcing;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BuildingBlocks.Abstractions.Messaging.PersistMessage;
using BuildingBlocks.Abstractions.Types;
using BuildingBlocks.Core.Messaging.MessagePersistence;
using BuildingBlocks.Core.Web.Extenions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -12,6 +13,7 @@ public class MessagePersistenceBackgroundService : BackgroundService
{
private readonly ILogger<MessagePersistenceBackgroundService> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IHostApplicationLifetime _lifetime;
private readonly MessagePersistenceOptions _options;
private readonly IMachineInstanceInfo _machineInstanceInfo;

Expand All @@ -21,24 +23,29 @@ public class MessagePersistenceBackgroundService : BackgroundService
ILogger<MessagePersistenceBackgroundService> logger,
IOptions<MessagePersistenceOptions> options,
IServiceProvider serviceProvider,
IHostApplicationLifetime lifetime,
IMachineInstanceInfo machineInstanceInfo)
{
_logger = logger;
_serviceProvider = serviceProvider;
_lifetime = lifetime;
_options = options.Value;
_machineInstanceInfo = machineInstanceInfo;
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!await _lifetime.WaitForAppStartup(stoppingToken))
{
return;
}

_logger.LogInformation(
"MessagePersistence Background Service is starting on client '{@ClientId}' and group '{@ClientGroup}'",
_machineInstanceInfo.ClientId,
_machineInstanceInfo.ClientGroup);

_executingTask = ProcessAsync(stoppingToken);

return _executingTask;
await ProcessAsync(stoppingToken);
}

public override Task StopAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Core.Messaging.Extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BuildingBlocks.Abstractions.Messaging;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Core.Messaging.Extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Reflection;
using BuildingBlocks.Abstractions.Messaging;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.Reflection.Extensions;
using BuildingBlocks.Core.Types.Extensions;

namespace BuildingBlocks.Core.Messaging.Extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BuildingBlocks.Abstractions.CQRS.Events.Internal;
using BuildingBlocks.Abstractions.Persistence.EventStore;
using BuildingBlocks.Core.Reflection;
using BuildingBlocks.Core.Utils;

namespace BuildingBlocks.Core.Persistence.EventStore;
Expand Down

0 comments on commit 7fd3394

Please sign in to comment.