Skip to content

Instantly share code, notes, and snippets.

@explorer14
Created December 29, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save explorer14/2dbf03a59dfdc5ae11e0787164e66b74 to your computer and use it in GitHub Desktop.
Save explorer14/2dbf03a59dfdc5ae11e0787164e66b74 to your computer and use it in GitHub Desktop.
#tool "nuget:?package=OctopusTools"
#addin nuget:?package=Cake.SemVer
#addin nuget:?package=semver&version=2.0.4
using System;
using System.Net.Http;
using Newtonsoft.Json;
using System.IO;
var target = Argument("target", "Build");
var artifactDirPath = "./artifacts/";
var packagePublishDirPath = "./publish/";
var packageId = "cicdapp";
var semVer = CreateSemVer(1,0,0);
var octopusApiKey = EnvironmentVariable("OctopusApiKey");
var octopusServerUrl = EnvironmentVariable("OctopusServerUrl");
var octopusProjectName = EnvironmentVariable("OctopusProjectName");
var solutionFilePath = "./src/cicdapp.sln";
var releaseEnvironment = Argument("releaseTo", "Testing");
Setup(ctx=>
{
var buildNumber = EnvironmentVariable("BUILD_BUILDNUMBER");
if(!string.IsNullOrWhiteSpace(buildNumber))
{
Information($"The build number was {buildNumber}");
semVer = buildNumber;
}
else
{
Information($"The build number was empty, using the default semantic version of {semVer.ToString()}");
}
});
Teardown(ctx=>
{
});
Task("Restore")
.Does(() => {
DotNetCoreRestore(solutionFilePath);
});
Task("Build")
.IsDependentOn("Restore")
.Does(()=>{
var config = new DotNetCoreBuildSettings
{
Configuration = "Release",
NoRestore = true
};
DotNetCoreBuild(solutionFilePath, config);
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
Information("In a real app this step will run the tests...");
});
Task("Publish")
.IsDependentOn("Test")
.Does(()=>
{
Information("Publishing the web service...");
var publishSettings = new DotNetCorePublishSettings
{
Configuration = "Release",
OutputDirectory = artifactDirPath,
NoRestore = true
};
DotNetCorePublish("./src/cicdapp/cicdapp.csproj", publishSettings);
System.IO.File.Copy("./build.ps1", artifactDirPath+"build.ps1", true);
System.IO.File.Copy("./build.cake", artifactDirPath+"build.cake", true);
});
Task("OctoPack")
.IsDependentOn("Publish")
.Does(()=>
{
var octoPackSettings = new OctopusPackSettings()
{
BasePath = artifactDirPath,
OutFolder = packagePublishDirPath,
Overwrite = true,
Version = semVer.ToString()
};
OctoPack(packageId, octoPackSettings);
});
Task("OctoPush")
.IsDependentOn("OctoPack")
.Does(()=>
{
Information($"The Octopus API Key was {octopusApiKey}");
var octoPushSettings = new OctopusPushSettings()
{
ReplaceExisting =true
};
var currentDir = System.IO.Directory.GetCurrentDirectory();
var physicalFilePath = System.IO.Path.Combine(
currentDir,
Directory(packagePublishDirPath),
$"{packageId}.{semVer}.nupkg");
if (System.IO.File.Exists(physicalFilePath))
{
Information($"Verified SUCCESSFULLY that the published package file exists at: {physicalFilePath}");
}
else
{
var directoryWherePackageWasExpected = new FilePath(physicalFilePath);
throw new FileNotFoundException(
$"The published nuget package was not found at {directoryWherePackageWasExpected.GetDirectory()}!");
}
OctoPush(octopusServerUrl,
octopusApiKey,
physicalFilePath,
octoPushSettings);
});
Task("OctoCreateRelease")
.IsDependentOn("OctoPush")
.Does(()=>
{
Information($"The environment variable was :{octopusApiKey}");
Information($"The version of the package is {semVer.ToString()}");
var createReleaseSettings = new CreateReleaseSettings
{
Server = octopusServerUrl,
ApiKey = octopusApiKey,
DeploymentProgress = true,
Packages = new Dictionary<string, string>
{
{packageId, semVer.ToString()}
}
};
OctoCreateRelease(octopusProjectName, createReleaseSettings);
});
Task("OctoDeploy")
.Does(()=>
{
var octoDeploySettings = new OctopusDeployReleaseDeploymentSettings
{
ShowProgress = true,
WaitForDeployment= true
};
Information($"Deploying Traxpense version {semVer.ToString()}");
Information($"Octopus URL {octopusServerUrl}");
Information($"Octopus API KEY {octopusApiKey}");
Information($"Octopus Project {octopusProjectName}");
Information($"Release Environment {releaseEnvironment}");
OctoDeployRelease(
octopusServerUrl,
octopusApiKey,
octopusProjectName,
releaseEnvironment,
semVer.ToString(),
octoDeploySettings);
});
RunTarget(target);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment