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

Feature: Allow for cli version specification #253

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"name": "devcontainers-ci",
"dockerFile": "Dockerfile",
"build": {
"cacheFrom": "ghcr.io/devcontainers/ci-devcontainer:latest"
"cacheFrom": "ghcr.io/devcontainers/ci-devcontainer:latest",
"args": {
// This is a temporary workaround when developing from host -> remote host -> devcontainer
// see: https://github.com/microsoft/vscode-remote-release/issues/7958
Copy link
Collaborator

Choose a reason for hiding this comment

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

It was mentioned that updating Docker helped. Is this still needed?

Copy link
Author

Choose a reason for hiding this comment

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

I was unable to get the devcontainer to build without this even after updating to the latest docker extension for vscode so I have left this as is.

"BUILDKIT_INLINE_CACHE": "0"
}
},
"mounts": [
// Keep command history
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ inputs:
required: false
default: false
description: Builds the image with `--no-cache` (takes precedence over `cacheFrom`)
cliVersion:
required: false
default: latest
description: The version of the devcontainer CLI to use
outputs:
runCmdOutput:
description: The output of the command specified in the runCmd input
Expand Down
13 changes: 11 additions & 2 deletions azdo-task/DevcontainersCi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DevContainerCliBuildArgs,
DevContainerCliExecArgs,
DevContainerCliUpArgs,
MAJOR_VERSION_FALLBACK
} from '../../../common/src/dev-container-cli';

import {isDockerBuildXInstalled, pushImage} from './docker';
Expand All @@ -23,10 +24,18 @@ export async function runMain(): Promise<void> {
);
return;
}
const devContainerCliInstalled = await devcontainer.isCliInstalled(exec);
const specifiedDevContainerCliVersion =
task.getInput('cliVersion') ?? MAJOR_VERSION_FALLBACK;
const devContainerCliInstalled = await devcontainer.isCliInstalled(
exec,
specifiedDevContainerCliVersion,
);
if (!devContainerCliInstalled) {
console.log('Installing @devcontainers/cli...');
const success = await devcontainer.installCli(exec);
const success = await devcontainer.installCli(
exec,
specifiedDevContainerCliVersion,
);
if (!success) {
task.setResult(
task.TaskResult.Failed,
Expand Down
3 changes: 2 additions & 1 deletion azdo-task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ In the example above, the devcontainer-build-run will perform the following step
| skipContainerUserIdUpdate | false | For non-root Dev Containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| cliVersion | false | The version of the [devcontainers CLI](https://github.com/devcontainers/cli) to use |

## Outputs

Expand Down
14 changes: 6 additions & 8 deletions common/src/dev-container-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {env} from 'process';
import {promisify} from 'util';
import {ExecFunction} from './exec';

const cliVersion = "0"; // Use 'latest' to get latest CLI version, or pin to specific version e.g. '0.14.1' if required
export const MAJOR_VERSION_FALLBACK = '0';

export interface DevContainerCliError {
outcome: 'error';
Expand All @@ -25,18 +25,16 @@ function getSpecCliInfo() {
};
}

async function isCliInstalled(exec: ExecFunction): Promise<boolean> {
async function isCliInstalled(exec: ExecFunction, cliVersion: string): Promise<boolean> {
try {
const {exitCode} = await exec(getSpecCliInfo().command, ['--help'], {
silent: true,
});
return exitCode === 0;
const {exitCode, stdout} = await exec(getSpecCliInfo().command, ['--version'], {});
return exitCode === 0 && stdout.trim() === cliVersion;
} catch (error) {
return false;
}
}
const fstat = promisify(fs.stat);
async function installCli(exec: ExecFunction): Promise<boolean> {
async function installCli(exec: ExecFunction, cliVersion: string): Promise<boolean> {
// if we have a local 'cli' folder, then use that as we're testing a private cli build
let cliStat = null;
try {
Expand All @@ -52,7 +50,7 @@ async function installCli(exec: ExecFunction): Promise<boolean> {
}
return exitCode === 0;
}
console.log('** Installing @devcontainers/cli');
console.log(`** Installing @devcontainers/cli@${cliVersion}`);
const {exitCode, stdout, stderr} = await exec('bash', ['-c', `npm install -g @devcontainers/cli@${cliVersion}`], {});
if (exitCode != 0) {
console.log(stdout);
Expand Down
3 changes: 2 additions & 1 deletion docs/azure-devops-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ In the example above, the devcontainer-build-run will perform the following step
| skipContainerUserIdUpdate | false | For non-root Dev Containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| cliVersion | false | The version of the [devcontainers CLI](https://github.com/devcontainers/cli) to use |

## Outputs

Expand Down
3 changes: 2 additions & 1 deletion docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
| skipContainerUserIdUpdate | false | For non-root Dev Containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |
| noCache | false | Builds the image with `--no-cache` (takes precedence over `cacheFrom`) |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. |
| cliVersion | false | The version of the [devcontainers CLI](https://github.com/devcontainers/cli) to use |

## Outputs

Expand Down
13 changes: 11 additions & 2 deletions github-action/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DevContainerCliBuildArgs,
DevContainerCliExecArgs,
DevContainerCliUpArgs,
MAJOR_VERSION_FALLBACK
} from '../../common/src/dev-container-cli';

import {isDockerBuildXInstalled, pushImage} from './docker';
Expand All @@ -33,10 +34,18 @@ export async function runMain(): Promise<void> {
);
return;
}
const devContainerCliInstalled = await devcontainer.isCliInstalled(exec);
const specifiedDevContainerCliVersion =
core.getInput('cliVersion') ?? MAJOR_VERSION_FALLBACK;
const devContainerCliInstalled = await devcontainer.isCliInstalled(
exec,
specifiedDevContainerCliVersion,
);
if (!devContainerCliInstalled) {
core.info('Installing @devcontainers/cli...');
const success = await devcontainer.installCli(exec);
const success = await devcontainer.installCli(
exec,
specifiedDevContainerCliVersion,
);
if (!success) {
core.setFailed('@devcontainers/cli install failed!');
return;
Expand Down