Skip to content

Commit

Permalink
(build) use git cmd instead of cake.git for push
Browse files Browse the repository at this point in the history
cleaning up the credentials, keeping only token were needed
  • Loading branch information
arturcic committed Aug 17, 2021
1 parent 08b777c commit 62a0df8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build/.run/Publish Docs.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Publish Docs" type="DotNetProject" factoryName=".NET Project" folderName="Docs">
<option name="EXE_PATH" value="$PROJECT_DIR$/../run/docs.exe" />
<option name="PROGRAM_PARAMETERS" value="--target=PublishDocs" />
<option name="PROGRAM_PARAMETERS" value="--target=PublishDocs --force" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.." />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion build/common/Utilities/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Common.Utilities
{
public class Constants
{
public const string RepoOwner = "gittools";
public const string RepoOwner = "GitTools";
public const string Repository = "GitVersion";

public const string Version50 = "5.0";
Expand Down
22 changes: 16 additions & 6 deletions build/common/Utilities/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ namespace Common.Utilities
{
public static class ContextExtensions
{
private static IEnumerable<string> ExecuteCommand(this ICakeContext context, FilePath exe, string? args)
private static IEnumerable<string> ExecuteCommand(this ICakeContext context, FilePath exe, string? args, DirectoryPath? workDir = null)
{
var processSettings = new ProcessSettings { Arguments = args, RedirectStandardOutput = true };
if (workDir is not null)
{
processSettings.WorkingDirectory = workDir;
}
context.StartProcess(exe, processSettings, out var redirectedOutput);
return redirectedOutput.ToList();
}

private static IEnumerable<string> ExecGitCmd(this ICakeContext context, string? cmd)
private static IEnumerable<string> ExecGitCmd(this ICakeContext context, string? cmd, DirectoryPath? workDir = null)
{
var gitExe = context.Tools.Resolve(context.IsRunningOnWindows() ? "git.exe" : "git");
return context.ExecuteCommand(gitExe, cmd);
return context.ExecuteCommand(gitExe, cmd, workDir);
}

public static void GitPushBranch(this ICakeContext context, DirectoryPath repositoryDirectoryPath, string username, string token, string branchName)
{
var pushUrl = $"https://{username}:{token}@github.com/{Constants.RepoOwner}/{Constants.Repository}";
context.ExecGitCmd($"push {pushUrl} {branchName}", workDir: repositoryDirectoryPath);
}

public static bool IsOriginalRepo(this ICakeContext context)
Expand All @@ -49,7 +59,7 @@ public static bool IsOriginalRepo(this ICakeContext context)
public static bool IsMainBranch(this ICakeContext context)
{
var buildSystem = context.BuildSystem();
string repositoryBranch = ExecGitCmd(context, "rev-parse --abbrev-ref HEAD").Single();
string repositoryBranch = context.ExecGitCmd("rev-parse --abbrev-ref HEAD").Single();
if (buildSystem.IsRunningOnAppVeyor)
{
repositoryBranch = buildSystem.AppVeyor.Environment.Repository.Branch;
Expand All @@ -70,8 +80,8 @@ public static bool IsMainBranch(this ICakeContext context)

public static bool IsTagged(this ICakeContext context)
{
var sha = ExecGitCmd(context, "rev-parse --verify HEAD").Single();
var isTagged = ExecGitCmd(context, "tag --points-at " + sha).Any();
var sha = context.ExecGitCmd("rev-parse --verify HEAD").Single();
var isTagged = context.ExecGitCmd("tag --points-at " + sha).Any();

return isTagged;
}
Expand Down
4 changes: 2 additions & 2 deletions build/common/Utilities/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public record CodeCovCredentials(string Token);

public record GitHubCredentials(string Token, string? UserName = null);

public record NugetCredentials(string ApiKey, string ApiUrl);
public record NugetCredentials(string ApiKey);

public record ChocolateyCredentials(string ApiKey, string ApiUrl);
public record ChocolateyCredentials(string ApiKey);

public record BuildVersion(GitVersion GitVersion, string? Version, string? Milestone, string? SemVersion, string? NugetVersion)
{
Expand Down
1 change: 0 additions & 1 deletion build/docs/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class BuildContext : BuildContextBase
{
public bool ForcePublish { get; set; }
public Credentials? Credentials { get; set; }

public WyamSettings? WyamSettings { get; set; }

public BuildContext(ICakeContext context) : base(context)
Expand Down
3 changes: 2 additions & 1 deletion build/docs/Tasks/PublishDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private static void PublishToGitHub(BuildContext context, DirectoryPath publishF
}

context.Information("Pushing all changes...");
context.GitPush(publishFolder, username, token, publishBranchName);

context.GitPushBranch(publishFolder, username, token, publishBranchName);
}
}
}
6 changes: 3 additions & 3 deletions build/docs/docs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<RootNamespace>Docs</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\common\common.csproj"/>
<ProjectReference Include="..\common\common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Cake.Git" Version="1.1.0"/>
<PackageReference Include="Cake.Wyam" Version="2.2.12"/>
<PackageReference Include="Cake.Git" Version="1.1.0" />
<PackageReference Include="Cake.Wyam" Version="2.2.12" />
</ItemGroup>
</Project>
3 changes: 1 addition & 2 deletions build/publish/Tasks/PublishNuget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ private static void PublishToNugetRepo(BuildContext context, string apiKey, stri
}
}
// TODO check package is already published
private static bool IsPackagePublished(BuildContext context, string? packageName, string? nugetVersion)
private static bool IsPackagePublished(BuildContext context, string? packageName, string? nugetVersion, string apiUrl)
{
var apiUrl = context.Credentials?.Nuget?.ApiUrl;
var publishedPackages = context.NuGetList($"packageId:{packageName} version:{nugetVersion}", new NuGetListSettings
{
Source = new[]
Expand Down
10 changes: 2 additions & 8 deletions build/publish/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ public class Credentials
public static Credentials GetCredentials(ICakeContext context) => new()
{
GitHub = new GitHubCredentials(context.EnvironmentVariable("GITHUB_TOKEN")),

Nuget = new NugetCredentials(
context.EnvironmentVariable("NUGET_API_KEY"),
context.EnvironmentVariable("NUGET_API_URL")),

Chocolatey = new ChocolateyCredentials(
context.EnvironmentVariable("CHOCOLATEY_API_KEY"),
context.EnvironmentVariable("CHOCOLATEY_API_URL")),
Nuget = new NugetCredentials(context.EnvironmentVariable("NUGET_API_KEY")),
Chocolatey = new ChocolateyCredentials(context.EnvironmentVariable("CHOCOLATEY_API_KEY")),
};
}
}

0 comments on commit 62a0df8

Please sign in to comment.