-
Notifications
You must be signed in to change notification settings - Fork 377
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
Allow configuring persist-credentials
for GitHub Actions checkout step
#1533
Labels
Comments
Made it work! [SemanticReleaseWorkflow(
"release",
GitHubActionsImage.UbuntuLatest,
OnPushBranches = ["main"],
InvokedTargets = [nameof(Release)],
ImportSecrets = [nameof(SemanticReleaseGitHubPat)],
CacheKeyFiles =
[
// NuGet lock files
"**/packages.lock.json", "!**/ bin/**", "!**/obj/**",
// npm lock file
"package-lock.json"
],
CacheIncludePatterns = [ ".nuke/temp", "~/.nuget/packages", "~/.npm" ])]
partial class Build
{
[Secret]
[Parameter("Fine-grained GitHub personal access token (PAT). " +
"semantic-release requires `contents: write` permissions to push tags. " +
"Optionally add `issues: write` and `pull-requests: write` permissions to allow " +
"semantic-release to comment on released issues and PRs.")]
readonly string SemanticReleaseGitHubPat;
Target RestoreNpm => _ => _
.Executes(() => NpmTasks.NpmCi());
Target Release => _ => _
.DependsOn(RestoreNpm, Test)
.Executes(() =>
{
Npx("semantic-release",
environmentVariables: EnvironmentInfo.Variables
.ToDictionary(env => env.Key, env => env.Value) // TODO(refactor): obsolete?
.SetKeyValue("GH_TOKEN", SemanticReleaseGitHubPat)
.AsReadOnly());
});
} I implemented the following custom attribute. It's a bit hacky, but the most NUKEsque way I could come up with: public class SemanticReleaseWorkflowAttribute
: GitHubActionsAttribute
{
public SemanticReleaseWorkflowAttribute(
string name, GitHubActionsImage image,
params GitHubActionsImage[] images) : base(name, image, images)
{
// treeless clone
FetchDepth = 0;
Filter = "tree:0";
}
protected override GitHubActionsJob GetJobs(
GitHubActionsImage image,
IReadOnlyCollection<ExecutableTarget> relevantTargets)
{
var jobs = base.GetJobs(image, relevantTargets);
// TODO: override GetSteps()
var checkoutStep = jobs.Steps.OfType<GitHubActionsCheckoutStep>().Single();
var checkoutStepIndex = Array.IndexOf(jobs.Steps, checkoutStep);
jobs.Steps[checkoutStepIndex] = new CheckoutStepWithCredentialPersistence
{
PersistCredentials = false, // 🪨🧑💻️
FetchDepth = checkoutStep.FetchDepth,
Filter = checkoutStep.Filter,
Lfs = checkoutStep.Lfs,
Progress = checkoutStep.Progress,
Submodules = checkoutStep.Submodules
};
return jobs;
}
private class CheckoutStepWithCredentialPersistence : GitHubActionsCheckoutStep
{
public bool? PersistCredentials { get; init; }
// TODO: refactor base class implementation detail 👇 🏞
private bool WithKeyAlreadyWritten =>
Submodules.HasValue || Lfs.HasValue || FetchDepth.HasValue || Progress.HasValue ||
!Filter.IsNullOrWhiteSpace();
public override void Write(CustomFileWriter writer)
{
base.Write(writer);
if (!PersistCredentials.HasValue) return;
using (writer.Indent())
{
if (!WithKeyAlreadyWritten)
writer.WriteLine("with:");
using (writer.Indent())
{
writer.WriteLine($"persist-credentials: {PersistCredentials.ToString().ToLowerInvariant()}");
}
}
}
}
} This could be easily added to the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
Allow generating the following via
PersistCredentials = false
option on theGitHubActionsAttribute
:From the
actions/checkout
readme:I currently use
GitHubActionsAttribute
s to generate two GH workflows:v*
tags, and will deploy a website, NuGets, some zips etc.So the release workflow pushes release tags, which should trigger the deploy workflow. But using the platform-provided
GITHUB_TOKEN
to push tags will not trigger any other workflows. This is a basic security measure to protect users from defining recursive workflows.The well-known workaround is using a Personal Access Token (PAT) to push tags, which allows a workflow to trigger another workflow.
I tried the following workaround:
I tried everything, fine-grained PATs, classic ones, using
NpmTasks.RunNpm()
. But the only thing that helps is adding settingpersist-credentials: false
.At first I thought that this is what
EnableGitHubToken = false
should do, but that's not the case.Usage Example
Alternative
No response
Could you help with a pull-request?
Yes
The text was updated successfully, but these errors were encountered: