Skip to content

Commit

Permalink
GitHubAction workflow (#28)
Browse files Browse the repository at this point in the history
* update repo restruct
  • Loading branch information
carsten-riedel authored May 20, 2024
1 parent 5f107a3 commit ce10583
Show file tree
Hide file tree
Showing 224 changed files with 1,026 additions and 201 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*secrets*.ps1
*.log
28 changes: 28 additions & 0 deletions .github/workflows/cicd.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
param (
[string]$PAT = $args[0],
[string]$NUGET_PAT = $args[1],
[string]$NUGET_TEST_PAT= $args[2]
)

$ErrorActionPreference = 'Stop'

Start-Transcript -Path "$(Join-Path -Path "$PSScriptRoot" -ChildPath "$($(Get-ChildItem "$PSCommandPath").BaseName)-$(Get-Date -f 'yyyyMMdd_HHmmss').log")"

. "$PSScriptRoot/cicd_util.ps1"
. "$PSScriptRoot/cicd_prebuild_enviroment_requirments.ps1"
. "$PSScriptRoot/cicd_prebuild_envars_prepare.ps1"
. "$PSScriptRoot/cicd_prebuild_envars_check.ps1"
. "$PSScriptRoot/cicd_prebuild_enviroment_configure.ps1"

. "$PSScriptRoot/cicd_build_clean.ps1"
. "$PSScriptRoot/cicd_build_config.ps1"
. "$PSScriptRoot/cicd_build.ps1"

. "$PSScriptRoot/cicd_deploy.ps1"


#git status --porcelain $sourceCodeFolder

Stop-Transcript

$x=1
55 changes: 55 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
# https://github.com/actions

name: CICD

on:
pull_request:
paths:
- 'src/**'
branches:
- 'master'
- 'release'
- 'develop'
- 'feature'
- 'hotfix'
- 'master/**'
- 'release/**'
- 'develop/**'
- 'feature/**'
- 'hotfix/**'
repository_dispatch:
types:
- builddispatch
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest

env:
GH_TOKEN: ${{ github.token }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup-dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Diagnostic print enviroment vars
run: printenv

- name: Display dotnet version
run: dotnet --version

- name: Display powershell core version
run: pwsh --version

- name: Workflow Build/Deploy
run: .github/workflows/cicd.ps1 "${{ secrets.PAT }}" "${{ secrets.NUGET_PAT }}" "${{ secrets.NUGET_TEST_PAT }}"
shell: pwsh
23 changes: 23 additions & 0 deletions .github/workflows/cicd_build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
######################################################################################
Log-Block -Stage "Build" -Section "Restore" -Task "Restoreing nuget packages."

if ($null -ne $dotnet_restore_param)
{
Execute-Command "dotnet restore $topLevelPath/$sourceCodeFolder $dotnet_restore_param"
}

######################################################################################
Log-Block -Stage "Build" -Section "Build" -Task "Building the solution."

if ($null -ne $dotnet_build_param)
{
Execute-Command "dotnet build $topLevelPath/$sourceCodeFolder $dotnet_build_param"
}

######################################################################################
Log-Block -Stage "Build" -Section "Pack" -Task "Creating a nuget package."

if ($null -ne $dotnet_pack_param)
{
Execute-Command "dotnet pack $topLevelPath/$sourceCodeFolder $dotnet_pack_param"
}
10 changes: 10 additions & 0 deletions .github/workflows/cicd_build_clean.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

######################################################################################
Log-Block -Stage "Setup" -Section "Clean" -Task "Clean local binaries"

# Example of how to call the function and capture the results
$results = Find-SpecialSubfolders -Path "$topLevelPath/$sourceCodeFolder" -FolderNames @('bin', 'obj')
foreach($item in $results)
{
Remove-FilesAndDirectories -FolderPath $item -DumpDeleted $false
}
46 changes: 46 additions & 0 deletions .github/workflows/cicd_build_config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
######################################################################################
Log-Block -Stage "Resolving" -Section "Branch" -Task "Config values for branches"

# Some variables can be $null or unset indicating a skipping step.

if ($branchNameSegment -ieq "feature") {

$version = "--property:AssemblyVersion=$fullVersion --property:VersionPrefix=$fullVersion --property:VersionSuffix=$branchNameSegment"

$dotnet_restore_param = "";
$dotnet_build_param = "--no-restore --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";
$dotnet_pack_param = "--force --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";

} elseif ($branchNameSegment -ieq "develop") {

$version = "--property:AssemblyVersion=$fullVersion --property:VersionPrefix=$fullVersion --property:VersionSuffix=$branchNameSegment"

$dotnet_restore_param = "";
$dotnet_build_param = "--no-restore --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";
$dotnet_pack_param = "--force --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";

} elseif ($branchNameSegment -ieq "release") {

$version = "--property:AssemblyVersion=$fullVersion --property:VersionPrefix=$fullVersion"

$dotnet_restore_param = "";
$dotnet_build_param = "--no-restore --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";
$dotnet_pack_param = "--force --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";

} elseif ($branchNameSegment -ieq "master") {

$version = "--property:AssemblyVersion=$fullVersion --property:VersionPrefix=$fullVersion"

$dotnet_restore_param = "";
$dotnet_build_param = "--no-restore --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";
$dotnet_pack_param = "--force --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";

} elseif ($branchNameSegment -ieq "hotfix") {

$version = "--property:AssemblyVersion=$fullVersion --property:VersionPrefix=$fullVersion"

$dotnet_restore_param = "";
$dotnet_build_param = "--no-restore --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";
$dotnet_pack_param = "--force --configuration Release --property:ContinuousIntegrationBuild=true --property:WarningLevel=3 $version";

}
83 changes: 83 additions & 0 deletions .github/workflows/cicd_deploy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
######################################################################################
Log-Block -Stage "Deploy" -Section "Nuget" -Task "Nuget"

if ($branchNameSegment -ieq "feature") {

$basePath = "$topLevelPath/src/Projects/Coree.Template.Project"
$pattern = "*.nupkg"
$firstFileMatch = Get-ChildItem -Path $basePath -Filter $pattern -File -Recurse | Select-Object -First 1
Execute-Command "dotnet nuget push ""$($firstFileMatch.FullName)"" --api-key $PAT --source ""github"""

} elseif ($branchNameSegment -ieq "develop") {

$basePath = "$topLevelPath/src/Projects/Coree.Template.Project"
$pattern = "*.nupkg"
$firstFileMatch = Get-ChildItem -Path $basePath -Filter $pattern -File -Recurse | Select-Object -First 1
Execute-Command "dotnet nuget push ""$($firstFileMatch.FullName)"" --api-key $PAT --source ""github"""

} elseif ($branchNameSegment -ieq "release") {

$basePath = "$topLevelPath/src/Projects/Coree.Template.Project"
$pattern = "*.nupkg"
$firstFileMatch = Get-ChildItem -Path $basePath -Filter $pattern -File -Recurse | Select-Object -First 1
Execute-Command "dotnet nuget push ""$($firstFileMatch.FullName)"" --api-key $PAT --source ""github"""

dotnet nuget push "$($firstFileMatch.FullName)" --api-key $NUGET_TEST_PAT --source https://apiint.nugettest.org/v3/index.json

} elseif ($branchNameSegment -ieq "master") {

$basePath = "$topLevelPath/src/Projects/Coree.Template.Project"
$pattern = "*.nupkg"
$firstFileMatch = Get-ChildItem -Path $basePath -Filter $pattern -File -Recurse | Select-Object -First 1
Execute-Command "dotnet nuget push ""$($firstFileMatch.FullName)"" --api-key $PAT --source ""github"""

dotnet nuget push "$($firstFileMatch.FullName)" --api-key $NUGET_PAT --source https://api.nuget.org/v3/index.json

} elseif ($branchNameSegment -ieq "hotfix") {

$basePath = "$topLevelPath/src/Projects/Coree.Template.Project"
$pattern = "*.nupkg"
$firstFileMatch = Get-ChildItem -Path $basePath -Filter $pattern -File -Recurse | Select-Object -First 1
Execute-Command "dotnet nuget push ""$($firstFileMatch.FullName)"" --api-key $PAT --source ""github"""

dotnet nuget push "$($firstFileMatch.FullName)" --api-key $NUGET_PAT --source https://api.nuget.org/v3/index.json
}

######################################################################################
Log-Block -Stage "Post Deploy" -Section "Tag and Push" -Task ""

if ($branchNameSegment -eq "master" -OR $branchNameSegment -eq "release" -OR $branchNameSegment -eq "hostfix")
{
$tag = "v$fullVersion"
}
else {
$tag = "v$fullVersion-$branchNameSegment"
}

$gitUserLocal = git config user.name
$gitMailLocal = git config user.email

# Check if the variables are null or empty (including whitespace)
if ([string]::IsNullOrWhiteSpace($gitUserLocal) -or [string]::IsNullOrWhiteSpace($gitMailLocal)) {
$gitTempUser= "Workflow"
$gitTempMail = "carstenriedel@outlook.com" # Assuming a placeholder email
} else {
$gitTempUser= $gitUserLocal
$gitTempMail = $gitMailLocal
}

git config user.name $gitTempUser
git config user.email $gitTempMail

Execute-Command -Command "git add --all"
Execute-Command -Command "git commit -m ""Updated form Workflow [no ci]""" -ExpectedExitCodes @(0,1)
Execute-Command -Command "git push origin $branchName"
Execute-Command -Command "git tag -a ""$tag"" -m ""[no ci]"""
Execute-Command -Command "git push origin ""$tag"""
$PAT | gh auth login --with-token
Execute-Command -Command "gh release create ""$tag"" --notes ""auto create release $tag"""
Execute-Command -Command "gh release upload ""$tag"" ""$($firstFileMatch.FullName)"" --clobber"

#restore
git config user.name $gitUserLocal
git config user.email $gitMailLocal
17 changes: 17 additions & 0 deletions .github/workflows/cicd_postdeploy_clean.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

######################################################################################
Log-Block -Stage "Post Deploy" -Section "Cleanup Packagelist" -Task ""

$headers = @{
Authorization = "Bearer $PAT"
}
$uri = "https://api.github.com/users/$gitOwner/packages/nuget/$gitRepo/versions"

$GitHubNugetPackagelist = Invoke-RestMethod -Uri $uri -Headers $headers
$GitHubNugetPackagelistOld = $GitHubNugetPackagelist | Where-Object { $_.name -like "*$branchNameSegment" } | Sort-Object -Property created_at -Descending | Select-Object -Skip 2
foreach ($item in $GitHubNugetPackagelistOld)
{
$PackageId = $item.id
Invoke-RestMethod -Method Delete -Uri "https://api.github.com/users/$gitOwner/packages/nuget/$gitRepo/versions/$PackageId" -Headers $headers | Out-Null
Write-Output "Unlisted package $gitRepo $($item.name)"
}
19 changes: 19 additions & 0 deletions .github/workflows/cicd_postdeploy_run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

######################################################################################
Log-Block -Stage "Call" -Section "Dispatch" -Task "dispatching a other job"

if ($branchNameSegment -ieq "master") {
$worklowFileName = "pages.yml"
$uri = "https://api.github.com/repos/$gitOwner/$gitRepo/actions/workflows/$worklowFileName/dispatches"
$headers = @{
"Accept" = "application/vnd.github+json"
"X-GitHub-Api-Version" = "2022-11-28"
"Authorization" = "Bearer $PAT"
"Content-Type" = "application/json"
}
$body = @{
ref = "$branchName"
} | ConvertTo-Json

Invoke-WebRequest -Uri $uri -Method Post -Headers $headers -Body $body -Verbose | Out-Null
}
32 changes: 32 additions & 0 deletions .github/workflows/cicd_prebuild_envars_check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
######################################################################################
Log-Block -Stage "Prebuild envars" -Section "Check" -Task "Valid branch names."

$isValidBranchRootName = @("feature", "develop", "release", "master", "main" , "hotfix" )

if (-not($isValidBranchRootName.ToLower() -contains $branchNameSegment)) {
Write-Host "No configuration for branches $branchNameSegment. Exiting"
exit 1
}
else {
Write-Host "Configuration for branch '$branchNameSegment' will be used."
}

######################################################################################
Log-Block -Stage "Prebuild envars" -Section "Check" -Task "Variables are set."

Ensure-VariableSet -VariableName "`$branchName" -VariableValue "$branchName"
Ensure-VariableSet -VariableName "`$branchNameSegment" -VariableValue "$branchNameSegment"
Ensure-VariableSet -VariableName "`$topLevelPath" -VariableValue "$topLevelPath"
Ensure-VariableSet -VariableName "`$topLevelDirectory" -VariableValue "$topLevelDirectory"
Ensure-VariableSet -VariableName "`$gitRemoteOriginUrl" -VariableValue "$gitRemoteOriginUrl"
Ensure-VariableSet -VariableName "`$gitOwner" -VariableValue "$gitOwner"
Ensure-VariableSet -VariableName "`$gitRepo" -VariableValue "$gitRepo"
Ensure-VariableSet -VariableName "`$sourceCodeFolder" -VariableValue "$sourceCodeFolder"
Ensure-VariableSet -VariableName "`$versionMajor" -VariableValue "$versionMajor"
Ensure-VariableSet -VariableName "`$versionMinor" -VariableValue "$versionMinor"
Ensure-VariableSet -VariableName "`$versionBuild" -VariableValue "$versionBuild"
Ensure-VariableSet -VariableName "`$versionRevision" -VariableValue "$versionRevision"
Ensure-VariableSet -VariableName "`$fullVersion" -VariableValue "$fullVersion"
Ensure-VariableSet -VariableName "`$PAT" -VariableValue "$PAT"
Ensure-VariableSet -VariableName "`$NUGET_PAT" -VariableValue "$NUGET_PAT"
Ensure-VariableSet -VariableName "`$NUGET_TEST_PAT" -VariableValue "$NUGET_TEST_PAT"
46 changes: 46 additions & 0 deletions .github/workflows/cicd_prebuild_envars_prepare.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
######################################################################################
Log-Block -Stage "Prebuild envars" -Section "Prepare" -Task "Secrets"

$secretsPath = "$PSScriptRoot/cicd_secrets.ps1"
# Check if the secrets file exists before importing
if (Test-Path $secretsPath) {
. "$secretsPath"
Write-Host "Secrets loaded from file."
} else {
Write-Host "Secrets will be taken from args."
}

######################################################################################
Log-Block -Stage "Prebuild envars" -Section "Prepare" -Task "Set fixed values"

$sourceCodeFolder = "src"
$versionMajor = "0"
$versionMinor = "1"
$versionBuild = Get-BaseVersionBuild
$versionRevision = Get-BaseVersionRevision
$fullVersion = "$versionMajor.$versionMinor.$versionBuild.$versionRevision"

######################################################################################
Log-Block -Stage "Prebuild envars" -Section "Prepare" -Task "Resolving branchnames and paths."

$branchName = Get-GitBranchName
$branchNameSegment = @(Get-NormalizedPathSegments -InputPath $branchName)[0].ToLower()

$topLevelPath = Get-GitTopLevelPath
$topLevelDirectory = @(Get-NormalizedPathSegments -InputPath $topLevelPath)[-1]

$gitRemoteOriginUrl = Get-GitRemoteOriginUrl
$gitOwner = @(Get-NormalizedPathSegments -InputPath $gitRemoteOriginUrl)[1]
$gitRepo = @(Get-NormalizedPathSegments -InputPath $gitRemoteOriginUrl)[2]

Write-Host "branchName is : $branchName"
Write-Host "branchNameSegment is : $branchNameSegment"
Write-Host "topLevelPath is : $topLevelPath"
Write-Host "topLevelDirectory is : $topLevelDirectory"
Write-Host "gitRemoteOriginUrl is : $gitRemoteOriginUrl"
Write-Host "gitOwner is : $gitOwner"
Write-Host "gitRepo is : $gitRepo"
Write-Host "fullVersion is : $fullVersion"



Loading

0 comments on commit ce10583

Please sign in to comment.