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

feat: add option pulumi-version-file #1204

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ inputs:
description: 'Install a specific version of the Pulumi CLI'
required: false
default: '^3'
pulumi-version-file:
description: 'File containing the version of the Pulumi CLI to install. Example: .pulumi.version'
required: false
work-dir:
description: 'Location of your Pulumi files. Defaults to ./'
required: false
Expand Down
25 changes: 23 additions & 2 deletions dist/index.js

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

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

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions src/__tests__/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,61 @@ describe('Config without a provided command', () => {
pulumiVersion: '^2',
});
});

it('should read version from pulumi-version-file', async () => {
jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: jest.fn((path: string) => {
expect(path).toEqual('.pulumi.version');
return '3.121.0';
}),
existsSync: jest.fn(() => {
return true;
}),
}));
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
if (name === 'pulumi-version-file') {
return '.pulumi.version';
}
return installConfig[name];
}),
}));

const { makeInstallationConfig } = require('../config');
const conf = makeInstallationConfig();
expect(conf.success).toBeTruthy();
expect(conf.value).toEqual({
command: undefined,
pulumiVersion: '3.121.0',
});
});

it('should fail if pulumi-version-file does not exist', async () => {
jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: jest.fn((path: string) => {
expect(path).toEqual('.pulumi.version');
return '3.121.0';
}),
existsSync: jest.fn(() => {
return false;
}),
}));
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
if (name === 'pulumi-version-file') {
return '.pulumi.version';
}
return installConfig[name];
}),
}));

const { makeInstallationConfig } = require('../config');
expect(() => {
makeInstallationConfig();
}).toThrow(/pulumi-version-file '\.pulumi\.version' does not exist/);
});
});

describe('main.login', () => {
Expand Down
23 changes: 21 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import { ConfigMap } from '@pulumi/pulumi/automation';
import {
getBooleanInput,
Expand All @@ -22,9 +23,20 @@ export const installationConfig = rt.Record({
export type InstallationConfig = rt.Static<typeof installationConfig>;

export function makeInstallationConfig(): rt.Result<InstallationConfig> {
let pulumiVersion = getInput('pulumi-version') || '^3';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we error out early if both pulumi-version and pulumi-version-file are given?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! I'll update it.

const versionFile = getInput('pulumi-version-file');
if (versionFile) {
if (fs.existsSync(versionFile)) {
pulumiVersion = fs
.readFileSync(versionFile, { encoding: 'utf-8' })
.trim();
} else {
throw new Error(`pulumi-version-file '${versionFile}' does not exist`);
}
}
return installationConfig.validate({
command: getInput('command') || undefined,
pulumiVersion: getInput('pulumi-version') || '^3',
pulumiVersion,
});
}

Expand All @@ -33,7 +45,14 @@ export function makeConfig() {
return {
command: getUnionInput('command', {
required: true,
alternatives: ['up', 'update', 'refresh', 'destroy', 'preview', 'output'] as const,
alternatives: [
'up',
'update',
'refresh',
'destroy',
'preview',
'output',
] as const,
}),
stackName: getInput('stack-name', { required: true }),
pulumiVersion: getInput('pulumi-version', { required: true }),
Expand Down
Loading