Skip to content

Commit

Permalink
Allow to use secret file mount
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Feb 14, 2021
1 parent e5f26cd commit 2a585c8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#syntax=docker/dockerfile:1.1-experimental
#syntax=docker/dockerfile:1.2

FROM node:12 AS deps
WORKDIR /src
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ___
* [outputs](#outputs)
* [Notes](#notes)
* [Multi-line secret value](#multi-line-secret-value)
* [Secret file mount](#secret-file-mount)
* [Troubleshooting](#troubleshooting)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
* [Limitation](#limitation)
Expand Down Expand Up @@ -665,6 +666,20 @@ secrets: |

> Note: all quote signs need to be doubled for escaping.

### Secret file mount

If the value of a secret is a file that exists in the workspace then its content will be used as value:

```text
# secret.txt
bar
```

```yaml
secrets: |
MYSECRET=./secret.txt
```

## Troubleshooting

See [TROUBLESHOOTING.md](TROUBLESHOOTING.md)
Expand Down
1 change: 1 addition & 0 deletions __tests__/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('getSecret', () => {
['A_SECRET=abcdef0123456789', 'A_SECRET', 'abcdef0123456789', false],
['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789', 'GIT_AUTH_TOKEN', 'abcdefghijklmno=0123456789', false],
['MY_KEY=c3RyaW5nLXdpdGgtZXF1YWxzCg==', 'MY_KEY', 'c3RyaW5nLXdpdGgtZXF1YWxzCg==', false],
[`foo=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`, 'foo', 'bar', false],
['aaaaaaaa', '', '', true],
['aaaaaaaa=', '', '', true],
['=bbbbbbb', '', '', true]
Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures/secret.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
7 changes: 5 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ export async function getImageID(): Promise<string | undefined> {
export async function getSecret(kvp: string): Promise<string> {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
const value = kvp.substring(delimiterIndex + 1);
let value = kvp.substring(delimiterIndex + 1);
if (key.length == 0 || value.length == 0) {
throw new Error(`${kvp} is not a valid secret`);
}
if (fs.existsSync(value)) {
value = fs.readFileSync(value, {encoding: 'utf-8'});
}
const secretFile = context.tmpNameSync({
tmpdir: context.tmpDir()
});
await fs.writeFileSync(secretFile, value);
fs.writeFileSync(secretFile, value);
return `id=${key},src=${secretFile}`;
}

Expand Down

0 comments on commit 2a585c8

Please sign in to comment.