Skip to content
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

set GIT_AUTH_TOKEN secret if Git context used #284

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ jobs:
name: Build
uses: ./
with:
source: .
files: |
./test/config.hcl
allow: network.host
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export function sanitizeInputs(inputs: Inputs) {
return res;
}

export function getGitAuthToken(inputs: Inputs): string {
return process.env.BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs['github-token'];
}

export async function getArgs(inputs: Inputs, definition: BakeDefinition, toolkit: Toolkit): Promise<Array<string>> {
// prettier-ignore
return [
Expand Down Expand Up @@ -97,6 +101,15 @@ async function getBakeArgs(inputs: Inputs, definition: BakeDefinition, toolkit:
await Util.asyncForEach(inputs.set, async set => {
args.push('--set', set);
});
if (await toolkit.buildx.versionSatisfies('<0.20.0')) {
// For buildx versions < 0.20.0, we need to set GIT_AUTH_TOKEN secret as it
// doesn't infer BUILDX_BAKE_GIT_AUTH_TOKEN environment variable for build
// request: https://github.com/docker/buildx/pull/2905
const gitAuthToken = getGitAuthToken(inputs);
if (gitAuthToken && !Bake.hasGitAuthTokenSecret(definition) && inputs.source.startsWith(Context.gitContext())) {
args.push('--set', `*.secrets=${Build.resolveSecretString(`GIT_AUTH_TOKEN=${gitAuthToken}`)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, this does not overwrite existing secrets, right?

How does this looks as a raw value? The actual secret value should not be argument for --set.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, this does not overwrite existing secrets, right?

Yes it doesn't, !Bake.hasGitAuthTokenSecret(definition) checks if GIT_AUTH_TOKEN is set first.

How does this looks as a raw value? The actual secret value should not be argument for --set.

It creates a temp file setting the secret value: https://github.com/docker/bake-action/actions/runs/12707842078/job/35423580416#step:4:231

image

And is removed when job is completed: https://github.com/docker/bake-action/actions/runs/12707842078/job/35423580416#step:6:35

image

This is the same behavior in build-push-action repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it doesn't, !Bake.hasGitAuthTokenSecret(definition) checks if GIT_AUTH_TOKEN is set first.

I meant that if user has own secrets (not git) defined in HCL then this doesn't overwrite it. I don't remember how the merge logic was in this case.

It creates a temp file setting the secret value:

Temp file shouldn't be needed as buildx can just read the value from env, but this doesn't need to be part of this PR.

Copy link
Member Author

@crazy-max crazy-max Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that if user has own secrets (not git) defined in HCL then this doesn't overwrite it. I don't remember how the merge logic was in this case.

Yes we check and skip if defined either through override or within the bake definition because we solve the definition first and look at existing secrets from there: https://github.com/docker/actions-toolkit/blob/54bdcf6c08a9b43d37df4156506648c978b372fe/src/buildx/bake.ts#L411-L423

image

Temp file shouldn't be needed as buildx can just read the value from env, but this doesn't need to be part of this PR.

Yes we could improve that agree, env was not a thing before. Will open a follow-up.

}
}
if (await toolkit.buildx.versionSatisfies('>=0.6.0')) {
args.push('--metadata-file', toolkit.buildxBake.getMetadataFilePath());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ actionsToolkit.run(
stateHelper.setInputs(inputs);

const toolkit = new Toolkit();
const gitAuthToken = process.env.BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs['github-token'];
const gitAuthToken = context.getGitAuthToken(inputs);

await core.group(`GitHub Actions runtime token ACs`, async () => {
try {
Expand Down
Loading