Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CI/CD #6

Merged
merged 1 commit into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/scripts/glob_exec.py
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# coding: utf8

import argparse
import fnmatch
import glob
import os
import subprocess
import sys

parser = argparse.ArgumentParser(description='Executes the given command for each matching file')
parser.add_argument('command', type=str,
help='The command to execute for each matching file')
parser.add_argument('--pattern', type=str,
help='The glob pattern that is used to match the files', required=True)
parser.add_argument('--exclude', type=str, default=os.environ.get('GLOB_EXEC_EXCLUDE'),
help='An optional semicolon delimited list of glob patterns used to exclude unwanted files')

args = parser.parse_args()
exclude = None if args.exclude is None else args.exclude.split(';')

def matches(file, patterns):
if not patterns:
return False
for pattern in patterns:
if fnmatch.fnmatch(file, pattern):
return True
return False

for file in glob.iglob(args.pattern, recursive=True):
if (not args.exclude) or (not matches(file, exclude)):
print(f'Processing file \'{file}\' ...')
sys.stdout.flush()

result = subprocess.run(args.command.replace('{}', f'"{file}"'), shell=True)
if result.returncode != 0:
exit(result.returncode)
84 changes: 76 additions & 8 deletions .github/workflows/build.yml
Expand Up @@ -7,6 +7,13 @@ on:
pull_request:
types: [opened, synchronize, reopened]

env:
SONAR_ORGANIZATION: flobernd
SONAR_PROJECT: flobernd_zysharp-metaprogramming
PROJ_PATTERN: '**/*.csproj'
TEST_PATTERN: '**/*.Tests.csproj'
GLOB_EXEC_EXCLUDE: 'Dependencies/*'

jobs:
ci:
name: CI
Expand Down Expand Up @@ -56,17 +63,50 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
./.sonar/scanner/dotnet-sonarscanner begin /k:"flobernd_zysharp-metaprogramming" /o:"flobernd" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/TestResults/**/coverage.opencover.xml" -d:sonar.cs.vstest.reportsPaths="**/TestResults/*.trx"
run: >
./.sonar/scanner/dotnet-sonarscanner begin
/k:"${{ env.SONAR_PROJECT }}"
/o:"${{ env.SONAR_ORGANIZATION }}"
/d:sonar.login="${{ secrets.SONAR_TOKEN }}"
/d:sonar.host.url="https://sonarcloud.io"
/d:sonar.cs.opencover.reportsPaths="**/TestResults/**/coverage.opencover.xml"
/d:sonar.cs.vstest.reportsPaths="**/TestResults/*.trx"

- name: Cache NuGet Packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: ${{ runner.os }}-nuget-

- name: .NET Restore
run: dotnet restore
run: >
python ./.github/scripts/glob_exec.py --pattern "${{ env.PROJ_PATTERN }}"
'
dotnet restore
{}
'

- name: .NET Build
run: dotnet build --no-restore --no-incremental
run: >
python ./.github/scripts/glob_exec.py --pattern "${{ env.PROJ_PATTERN }}"
'
dotnet build
--no-restore
{}
'

- name: .NET Test
run: dotnet test --no-build --logger:"trx;LogFilePrefix=results" --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
run: >
python ./.github/scripts/glob_exec.py --pattern "${{ env.TEST_PATTERN }}"
'
dotnet test
--no-build
--logger:"trx;LogFilePrefix=results"
--collect:"XPlat Code Coverage"
{}
-- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
'

- name: SonarCloud End Analysis
env:
Expand All @@ -92,8 +132,36 @@ jobs:
with:
dotnet-version: 6.0.x

- name: Generate NuGet Packages
run: dotnet pack --configuration Release -o out /p:ContinuousIntegrationBuild=true
- name: Cache NuGet Packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: ${{ runner.os }}-nuget-

- name: .NET Restore
run: >
python ./.github/scripts/glob_exec.py --pattern "${{ env.PROJ_PATTERN }}"
'
dotnet restore
{}
'

- name: .NET Pack
run: >
python ./.github/scripts/glob_exec.py --pattern "${{ env.PROJ_PATTERN }}"
'
dotnet pack
--no-restore
--configuration Release
--output ./out
-p:ContinuousIntegrationBuild=true
{}
'

- name: Publish to NuGet
run: dotnet nuget push out/*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
run: >
dotnet nuget push ./out/*.nupkg
-k ${{secrets.NUGET_API_KEY}}
-s https://api.nuget.org/v3/index.json
--skip-duplicate
@@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>

<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<RestoreLockedMode Condition="'$(ContinuousIntegrationBuild)' == 'true'" />

<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>

<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand Down
24 changes: 24 additions & 0 deletions ZySharp.Metaprogramming.Examples/packages.lock.json
@@ -0,0 +1,24 @@
{
"version": 1,
"dependencies": {
"net6.0": {
"JetBrains.Annotations": {
"type": "Transitive",
"resolved": "2022.1.0",
"contentHash": "ASfpoFJxiRsC9Xc4TWuPM41Zb/gl64xwfMOhnOZ3RnVWGYIZchjpWQV5zshJgoc/ZxVtgjaF7b577lURj7E6ig=="
},
"zysharp.metaprogramming": {
"type": "Project",
"dependencies": {
"ZySharp.Validation": "1.1.8"
}
},
"zysharp.validation": {
"type": "Project",
"dependencies": {
"JetBrains.Annotations": "2022.1.0"
}
}
}
}
}
Expand Up @@ -6,6 +6,13 @@
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>

<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<!-- https://github.com/NuGet/Home/issues/9195 -->
<!-- https://github.com/NuGet/Home/issues/10456 -->
<!-- https://github.com/dotnet/sdk/issues/26505 -->
<!--<RestoreLockedMode Condition="'$(ContinuousIntegrationBuild)' == 'true'" />-->
<!--<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>-->

<IsPackable>false</IsPackable>

<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand All @@ -15,6 +22,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<!-- https://github.com/microsoft/vstest/issues/2469 -->
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="17.2.0" Condition="$(TargetFramework.StartsWith('net4')) AND '$(OS)' == 'Unix'" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down