Skip to content

Commit

Permalink
Add bicep build task and add support ro run multiple Npm run scripts (#…
Browse files Browse the repository at this point in the history
…23)

Co-authored-by: philo <[email protected]>
  • Loading branch information
Philo and philo authored Jan 26, 2021
1 parent 0ca0b87 commit ad944fc
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 351 deletions.
60 changes: 60 additions & 0 deletions .cake/Build-Bicep.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#load "Configuration.cake"

public partial class Configuration {

public Configuration RegisterBicepPath(string bicepPath)
{
context.Tools.RegisterFile(bicepPath);

return this;
}

public Configuration WithBicepFile(params string[] bicepFilePattern)
{
var files = bicepFilePattern
.ToList()
.SelectMany(context.GetFiles)
.ToArray();

this.TaskParameters.Add("Bicep:Files", files);

return this;
}
}

Task("Bicep:Build")
.IsDependeeOf("Build")
.Does<Configuration>(config =>
{
var settings = new ProcessSettings()
{
RedirectStandardOutput = false
};

var bicepExe = Context.Tools.Resolve("bicep.exe");

if(bicepExe != null)
{
var bicepFiles = config.GetTaskParameter<FilePath[]>("Bicep:Files", new FilePath[] { "" });

Verbose("Building {0} bicep files", bicepFiles.Count());
foreach(var bicepFile in bicepFiles.Where(f => FileExists(f)))
{
Information("Building {0}...", bicepFile);
settings.Arguments = string.Format("build {0}", bicepFile);

using(var process = StartAndReturnProcess(bicepExe, settings))
{
process.WaitForExit();
if(process.GetExitCode() != 0)
{
Error("Failed");
}
}
}
}
else
{
Error("Unable to resolve bicep tool location");
}
});
107 changes: 87 additions & 20 deletions .cake/Npm-RunScript.cake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ public partial class Configuration
{
public Configuration RunNpmScript(string scriptName, string workingDirectory = "./")
{
Npm = new NpmConfiguration(context, scriptName, workingDirectory);
if(Npm == null)
{
Npm = new NpmConfiguration(context, scriptName, workingDirectory);
}
else
{
Npm.AddScript(scriptName, workingDirectory);
}
return this;
}

Expand All @@ -13,21 +20,48 @@ public partial class Configuration

public class NpmConfiguration
{
public string WorkingDirectory { get; } = "./";
public string ScriptName { get; } = "ci";
private readonly IList<NpmScriptEntry> _scriptEntries = new List<NpmScriptEntry>();
public IEnumerable<NpmScriptEntry> ScriptEntries => _scriptEntries.ToList();

private readonly ICakeContext cakeContext;

public NpmConfiguration(ICakeContext cakeContext, string scriptName, string workingDirectory)
{
this.cakeContext = cakeContext;
ScriptName = scriptName;
WorkingDirectory = workingDirectory ?? "./";
_scriptEntries.Add(new NpmScriptEntry(cakeContext, scriptName, workingDirectory));
}

public void AddScript(string scriptName, string workingDirectory)
{
_scriptEntries.Add(new NpmScriptEntry(cakeContext, scriptName, workingDirectory));
}

public bool CanExecuteNpm {
get {
return cakeContext.FileExists($"{WorkingDirectory.TrimEnd('/')}/package.json");
get
{
return ScriptEntries.Any(s => s.CanExecuteNpm);
}
}

public class NpmScriptEntry
{
public string ScriptName { get; }
public string WorkingDirectory { get; }

private readonly ICakeContext cakeContext;

public NpmScriptEntry(ICakeContext cakeContext, string scriptName, string workingDirectory)
{
this.cakeContext = cakeContext;
ScriptName = scriptName;
WorkingDirectory = workingDirectory ?? "./";
}

public bool CanExecuteNpm {
get
{
return cakeContext.FileExists($"{WorkingDirectory.TrimEnd('/')}/package.json");
}
}
}
}
Expand All @@ -38,24 +72,57 @@ Task("Npm:Install")
.WithCriteria<Configuration>((ctx, config) => config.Npm.CanExecuteNpm)
.Does<Configuration>(config =>
{
var settings = new NpmInstallSettings();
settings.WorkingDirectory = config.Npm.WorkingDirectory;
settings.LogLevel = NpmLogLevel.Silent;
settings.RedirectStandardError = false;
settings.RedirectStandardOutput = false;
NpmInstall(settings);
foreach(var script in config.Npm.ScriptEntries)
{
if(script.CanExecuteNpm)
{
Information($"Running Install from {script.WorkingDirectory}");
var settings = new NpmInstallSettings();
settings.WorkingDirectory = script.WorkingDirectory;
settings.LogLevel = NpmLogLevel.Silent;
settings.RedirectStandardError = false;
settings.RedirectStandardOutput = false;
NpmInstall(settings);
}
}
});

Task("Npm:Install")
.WithCriteria<Configuration>((ctx, config) => config.Npm.CanExecuteNpm)
.Does<Configuration>(config =>
{
foreach(var script in config.Npm.ScriptEntries)
{
if(script.CanExecuteNpm)
{
Information($"Running CI from {script.WorkingDirectory}");
var settings = new NpmCiSettings();
settings.WorkingDirectory = script.WorkingDirectory;
settings.LogLevel = NpmLogLevel.Silent;
settings.RedirectStandardError = false;
settings.RedirectStandardOutput = false;
NpmCi(settings);
}
}
});

Task("Npm:Build")
.IsDependentOn("Npm:Install")
.WithCriteria<Configuration>((ctx, config) => config.Npm.CanExecuteNpm)
.Does<Configuration>(config =>
{
var settings = new NpmRunScriptSettings();
settings.WorkingDirectory = config.Npm.WorkingDirectory;
settings.LogLevel = NpmLogLevel.Silent;
settings.RedirectStandardError = false;
settings.RedirectStandardOutput = false;
settings.ScriptName = config.Npm.ScriptName;
NpmRunScript(settings);
foreach(var script in config.Npm.ScriptEntries)
{
if(script.CanExecuteNpm)
{
Information($"Running script {script.ScriptName} from {script.WorkingDirectory}");
var settings = new NpmRunScriptSettings();
settings.WorkingDirectory = script.WorkingDirectory;
settings.LogLevel = NpmLogLevel.Silent;
settings.RedirectStandardError = false;
settings.RedirectStandardOutput = false;
settings.ScriptName = script.ScriptName;
NpmRunScript(settings);
}
}
});
12 changes: 0 additions & 12 deletions .vsts-ci.yml

This file was deleted.

6 changes: 4 additions & 2 deletions GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
assembly-versioning-scheme: MajorMinorPatchTag
assembly-file-versioning-scheme: MajorMinorPatchTag
assembly-informational-format: '{MajorMinorPatch}+{BranchName}+{CommitDate}'
assembly-informational-format: '{MajorMinorPatch}+{BranchName}+{ShortSha}'
mode: Mainline
continuous-delivery-fallback-tag:
branches:
release:
mode: ContinuousDelivery
tag: ''
master:
regex: (^master$|^origin\/master$|^main$|^origin\/main$)
mode: ContinuousDeployment
tag: ci
is-mainline: true
pull-request:
mode: ContinuousDeployment
tag: pr
Expand Down
103 changes: 58 additions & 45 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
trigger:
batch: true
branches:
include:
- master
- release/*
jobs:
- job: Build
pool:
name: Storm-Build-Production
variables:
cake.buildTarget: 'CI'
build.configuration: 'Release'
cake.verbosity: 'Verbose'
steps:
- checkout: self
clean: true

- task: ms-codeanalysis.vss-microsoft-security-code-analysis.build-task-credscan.CredScan@2
displayName: 'Run CredScan'
continueOnError: true

- powershell: ./build.ps1 -Target "$(cake.buildTarget)" -Configuration "$(build.configuration)" -Verbosity "$(cake.verbosity)"
displayName: 'Cake Build'

- task: ms-codeanalysis.vss-microsoft-security-code-analysis.build-task-report.SdtReport@1
displayName: 'Create Security Analysis Report'
inputs:
AllTools: true
ToolLogsNotFoundAction: Error

- task: ms-codeanalysis.vss-microsoft-security-code-analysis.build-task-publishsecurityanalysislogs.PublishSecurityAnalysisLogs@2
displayName: 'Publish Security Analysis Logs'

- powershell: Write-Host '##vso[build.addbuildtag]PR'
displayName: 'Mark as PR Build'
condition: and(succeeded(), in(variables['Build.Reason'], 'PullRequest'))

- powershell: Write-Host '##vso[build.addbuildtag]Release'
displayName: 'Mark as Release Build'
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))

- task: ms-codeanalysis.vss-microsoft-security-code-analysis.build-task-postanalysis.PostAnalysis@1
displayName: 'Post Analysis'
inputs:
AllTools: true
- main
- release/*
stages:
- stage: build
displayName: Execute Build
jobs:
- job: BuildArtifacts
displayName: Build Project Artifacts
variables:
NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages
frontend.src: 'src/frontend'
npm_config_cache: $(Pipeline.Workspace)/.npm
bicep.version: 'v0.2.212'
bicep.dir: '$(System.DefaultWorkingDirectory)/tools/bicep'
bicep.exists: false
pool:
vmImage: 'windows-latest'
steps:
- checkout: self
persistCredentials: true
- task: UseDotNet@2
inputs:
packageType: "sdk"
useGlobalJson: true
- task: UseNode@1
inputs:
version: 12.x
- task: Cache@2
inputs:
key: '"$(bicep.version)" | "$(Agent.OS)""'
path: '$(bicep.dir)'
cacheHitVar: 'bicep.exists'
- task: PowerShell@2
condition: ne(variables['bicep.exists'], 'true')
displayName: Acquire Bicep
inputs:
targetType: 'inline'
script: 'mkdir -Path $(bicep.dir) -Force ; Invoke-WebRequest -Uri https://github.com/Azure/bicep/releases/download/$(bicep.version)/bicep-win-x64.exe -OutFile $(bicep.dir)/bicep.exe'
failOnStderr: true
pwsh: false
workingDirectory: '$(System.DefaultWorkingDirectory)'
- task: DotNetCoreCLI@2
displayName: Restore dotnet tools
inputs:
command: "custom"
custom: "tool"
arguments: "restore"
- script: dotnet cake"
displayName: Run Cake
- task: GitTag@5
displayName: "Tag Artifacts"
condition: and(Succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'))
inputs:
workingdir: '$(SYSTEM.DEFAULTWORKINGDIRECTORY)'
tagUser: 'pipeline'
tagEmail: '[email protected]'
tag: '$(build.buildNumber)'
useLightweightTags: true
28 changes: 28 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#load ".cake/Configuration.cake"

/**********************************************************/
Setup<Configuration>(ctx =>
Configuration
.Create(ctx)
.RunNpmScript("ci", "./src/frontend")
.WithBicepFile("./deploy/*.bicep")
.IncludeArtifactCopyTarget("./deploy")
.IncludeAsEfDbContext(p => p.AssemblyName.EndsWith(".Web"))
);
/**********************************************************/

#load ".cake/CI.cake"

// -- DotNetCore
#load ".cake/Npm-RunScript.cake"
#load ".cake/Build-Bicep.cake"
#load ".cake/Restore-DotNetCore.cake"
#load ".cake/Build-DotNetCore.cake"
#load ".cake/Test-DotNetCore.cake"
#load ".cake/Publish-Zip-DotNetCore.cake"
#load ".cake/Publish-Pack-DotNetCore.cake"
#load ".cake/Artifacts-DotNetCore-Ef.cake"
#load ".cake/Artifacts-Copy.cake"
// -------------

RunTarget(Argument("target", Argument("Target", "Default")));
15 changes: 0 additions & 15 deletions build.cmd

This file was deleted.

Loading

0 comments on commit ad944fc

Please sign in to comment.