Skip to content

Commit 8b2e2d0

Browse files
authored
Add configFile option (#269)
1 parent ede49b4 commit 8b2e2d0

File tree

12 files changed

+111
-2
lines changed

12 files changed

+111
-2
lines changed

.azure-devops/azure-pipelines.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ jobs:
9090
exit 1
9191
fi
9292
93+
- job: test_config_file
94+
displayName: Test configFile option
95+
steps:
96+
- task: DevcontainersCi@0
97+
inputs:
98+
subFolder: github-tests/Dockerfile/config-file
99+
configFile: github-tests/Dockerfile/config-file/.devcontainer/subfolder/devcontainer.json
100+
runCmd: echo $HOSTNAME && [[ $HOSTNAME == "my-host" ]]
101+
- script: |
102+
echo "'runCmdOutput' value: $runCmdOutput"
103+
if [["$runCmdOutput" = *my-host*]]; then
104+
echo "'runCmdOutput' output of test_config_file job doesn't contain expected value 'my-host'"
105+
exit 1
106+
fi
107+
93108
- job: test_build_args
94109
displayName: Test build-args
95110
steps:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"features": {
3+
"ghcr.io/devcontainers/features/github-cli:1": {
4+
"version": "1.0.11",
5+
"resolved": "ghcr.io/devcontainers/features/github-cli@sha256:464564228ccdd6028f01f8a62a3cfbaf76e9ba7953b29ac0e53ba2c262604312",
6+
"integrity": "sha256:464564228ccdd6028f01f8a62a3cfbaf76e9ba7953b29ac0e53ba2c262604312"
7+
}
8+
}
9+
}

.github/workflows/ci_common.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ jobs:
200200
- test-gh-docker-from-docker-root
201201
- test-gh-skip-user-update
202202
- test-compose-features
203+
- test-config-file
203204
- test-simple
204205
- test-no-run
205206
- test-platform-with-runcmd
@@ -452,6 +453,45 @@ jobs:
452453
env:
453454
runCmdOutput: ${{ steps.simpletest.outputs.runCmdOutput }}
454455

456+
test-config-file:
457+
name: Run test with config file
458+
runs-on: ubuntu-latest
459+
needs: build
460+
steps:
461+
- name: Checkout
462+
uses: actions/checkout@v3
463+
with:
464+
persist-credentials: false
465+
# if the following value is missing (i.e. not triggered via comment workflow)
466+
# then the default checkout will apply
467+
ref: ${{ inputs.prRef }}
468+
469+
# Published action contains compiled JS, but we need to compile it here
470+
- uses: actions/setup-node@v3
471+
with:
472+
node-version: 20
473+
- name: Compile GH action
474+
run: |
475+
(cd common && npm install && npm run build)
476+
(cd github-action/ && npm install && npm run build && npm run package)
477+
478+
- name: Run test
479+
uses: ./
480+
id: configfiletest
481+
with:
482+
subFolder: github-tests/Dockerfile/config-file
483+
configFile: github-tests/Dockerfile/config-file/.devcontainer/subfolder/devcontainer.json
484+
runCmd: echo $HOSTNAME && [[ $HOSTNAME == "my-host" ]]
485+
- name: Validate runCmdOutput output
486+
run: |
487+
echo "'runCmdOutput' value: $runCmdOutput"
488+
if [["$runCmdOutput" = *my-host*]]; then
489+
echo "'runCmdOutput' output of configfiletest step doesn't contain expected value 'my-host'"
490+
exit 1
491+
fi
492+
env:
493+
runCmdOutput: ${{ steps.configfiletest.outputs.runCmdOutput }}
494+
455495
test-gh-run-args:
456496
name: Run GitHub run-args test
457497
runs-on: ubuntu-latest

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
required: false
2222
description: Specify a child folder (containing a .devcontainer) instead of using the repository root
2323
default:
24+
configFile:
25+
required: false
26+
description: Specify the path to a devcontainer.json file instead of using `./.devcontainer/devcontainer.json` or `./.devcontainer.json`
27+
default:
2428
checkoutPath:
2529
required: false
2630
description: Specify path to checked out folder if not using default (or for testing with nektos/act)

azdo-task/DevcontainersCi/src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export async function runMain(): Promise<void> {
4141
const imageTag = task.getInput('imageTag');
4242
const platform = task.getInput('platform');
4343
const subFolder = task.getInput('subFolder') ?? '.';
44+
const relativeConfigFile = task.getInput('configFile');
4445
const runCommand = task.getInput('runCmd');
4546
const envs = task.getInput('env')?.split('\n') ?? [];
4647
const inputEnvsWithDefaults = populateDefaults(envs);
@@ -62,6 +63,8 @@ export async function runMain(): Promise<void> {
6263

6364
const log = (message: string): void => console.log(message);
6465
const workspaceFolder = path.resolve(checkoutPath, subFolder);
66+
const configFile =
67+
relativeConfigFile && path.resolve(checkoutPath, relativeConfigFile);
6568

6669
const resolvedImageTag = imageTag ?? 'latest';
6770
const imageTagArray = resolvedImageTag.split(',');
@@ -91,6 +94,7 @@ export async function runMain(): Promise<void> {
9194
}
9295
const buildArgs: DevContainerCliBuildArgs = {
9396
workspaceFolder,
97+
configFile,
9498
imageName: fullImageNameArray,
9599
platform,
96100
additionalCacheFroms: cacheFrom,
@@ -120,6 +124,7 @@ export async function runMain(): Promise<void> {
120124
console.log('***');
121125
const upArgs: DevContainerCliUpArgs = {
122126
workspaceFolder,
127+
configFile,
123128
additionalCacheFroms: cacheFrom,
124129
skipContainerUserIdUpdate,
125130
env: inputEnvsWithDefaults,
@@ -141,6 +146,7 @@ export async function runMain(): Promise<void> {
141146
console.log('***');
142147
const execArgs: DevContainerCliExecArgs = {
143148
workspaceFolder,
149+
configFile,
144150
command: ['bash', '-c', runCommand],
145151
env: inputEnvsWithDefaults,
146152
};

azdo-task/DevcontainersCi/task.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
"label": "Specify a child folder (containing a .devcontainer) instead of using the repository root",
5353
"required": false
5454
},
55+
{
56+
"name": "configFile",
57+
"type": "string",
58+
"label": "Specify the path to a devcontainer.json file instead of using `./.devcontainer/devcontainer.json` or `./.devcontainer.json`",
59+
"required": false
60+
},
5561
{
5662
"name": "env",
5763
"type": "multiLine",

azdo-task/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ In the example above, the devcontainer-build-run will perform the following step
7171
| imageName | true | Image name to use when building the dev container image (including registry) |
7272
| imageTag | false | One or more comma-separated image tags (defaults to `latest`) |
7373
| subFolder | false | Use this to specify the repo-relative path to the folder containing the dev container (i.e. the folder that contains the `.devcontainer` folder). Defaults to repo root |
74+
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
7475
| runCmd | true | The command to run after building the dev container image |
7576
| env | false | Specify environment variables to pass to the dev container when run |
7677
| push | false | One of: `never`, `filter`, `always`. When set to `filter`, the image if pushed if the `sourceBranchFilterForPush`, `buildReasonsForPush`, and `pushOnFailedBuild` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. |

common/src/dev-container-cli.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface DevContainerCliBuildResult
152152
extends DevContainerCliSuccessResult {}
153153
export interface DevContainerCliBuildArgs {
154154
workspaceFolder: string;
155+
configFile: string | undefined;
155156
imageName?: string[];
156157
platform?: string;
157158
additionalCacheFroms?: string[];
@@ -168,6 +169,9 @@ async function devContainerBuild(
168169
'--workspace-folder',
169170
args.workspaceFolder,
170171
];
172+
if (args.configFile) {
173+
commandArgs.push('--config', args.configFile);
174+
}
171175
if (args.imageName) {
172176
args.imageName.forEach(iName =>
173177
commandArgs.push('--image-name', iName),
@@ -203,6 +207,7 @@ export interface DevContainerCliUpResult extends DevContainerCliSuccessResult {
203207
}
204208
export interface DevContainerCliUpArgs {
205209
workspaceFolder: string;
210+
configFile: string | undefined;
206211
additionalCacheFroms?: string[];
207212
skipContainerUserIdUpdate?: boolean;
208213
env?: string[];
@@ -220,6 +225,9 @@ async function devContainerUp(
220225
args.workspaceFolder,
221226
...remoteEnvArgs,
222227
];
228+
if (args.configFile) {
229+
commandArgs.push('--config', args.configFile);
230+
}
223231
if (args.additionalCacheFroms) {
224232
args.additionalCacheFroms.forEach(cacheFrom =>
225233
commandArgs.push('--cache-from', cacheFrom),
@@ -245,6 +253,7 @@ async function devContainerUp(
245253

246254
export interface DevContainerCliExecArgs {
247255
workspaceFolder: string;
256+
configFile: string | undefined;
248257
command: string[];
249258
env?: string[];
250259
userDataFolder?: string;
@@ -255,12 +264,15 @@ async function devContainerExec(
255264
): Promise<number | null> {
256265
// const remoteEnvArgs = args.env ? args.env.flatMap(e=> ["--remote-env", e]): []; // TODO - test flatMap again
257266
const remoteEnvArgs = getRemoteEnvArray(args.env);
258-
const commandArgs = ["exec", "--workspace-folder", args.workspaceFolder, ...remoteEnvArgs, ...args.command];
267+
const commandArgs = ["exec", "--workspace-folder", args.workspaceFolder, ...remoteEnvArgs];
268+
if (args.configFile) {
269+
commandArgs.push('--config', args.configFile);
270+
}
259271
if (args.userDataFolder) {
260272
commandArgs.push("--user-data-folder", args.userDataFolder);
261273
}
262274
return await runSpecCliNonJsonCommand({
263-
args: commandArgs,
275+
args: commandArgs.concat(args.command),
264276
log,
265277
env: {DOCKER_BUILDKIT: '1', COMPOSE_DOCKER_CLI_BUILD: '1'},
266278
});

docs/azure-devops-task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ In the example above, the devcontainer-build-run will perform the following step
7171
| imageName | true | Image name to use when building the dev container image (including registry) |
7272
| imageTag | false | One or more comma-separated image tags (defaults to `latest`) |
7373
| subFolder | false | Use this to specify the repo-relative path to the folder containing the dev container (i.e. the folder that contains the `.devcontainer` folder). Defaults to repo root |
74+
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
7475
| runCmd | true | The command to run after building the dev container image |
7576
| env | false | Specify environment variables to pass to the dev container when run |
7677
| push | false | One of: `never`, `filter`, `always`. When set to `filter`, the image if pushed if the `sourceBranchFilterForPush`, `buildReasonsForPush`, and `pushOnFailedBuild` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. |

docs/github-action.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
130130
| imageName | true | Image name to use when building the dev container image (including registry) |
131131
| imageTag | false | One or more comma-separated image tags (defaults to `latest`) |
132132
| subFolder | false | Use this to specify the repo-relative path to the folder containing the dev container (i.e. the folder that contains the `.devcontainer` folder). Defaults to repo root |
133+
| configFile | false | Use this to specify the repo-relative path to the devcontainer.json file. Defaults to `./.devcontainer/devcontainer.json` and `./.devcontainer.json`. |
133134
| runCmd | true | The command to run after building the dev container image |
134135
| env | false | Specify environment variables to pass to the dev container when run |
135136
| checkoutPath | false | Only used for development/testing |

0 commit comments

Comments
 (0)