|
| 1 | +import * as core from '@actions/core' |
| 2 | +import * as exec from '@actions/exec' |
| 3 | +import fs from 'fs' |
| 4 | +import { tmpdir } from 'os' |
| 5 | +import { join } from 'path' |
| 6 | +import { downloadFile, calculateSha256, getVersionMetadata } from './install-util.js' |
| 7 | + |
| 8 | +function detectWindowsPlatform() { |
| 9 | + const arch = process.arch |
| 10 | + const platform = process.platform |
| 11 | + |
| 12 | + if (platform !== 'win32') { |
| 13 | + throw new Error(`Unsupported platform: ${platform}`) |
| 14 | + } |
| 15 | + |
| 16 | + // Map Node.js arch to IBM Cloud CLI platform identifiers |
| 17 | + const archMap = { |
| 18 | + 'x64': 'win64', |
| 19 | + 'ia32': 'win32' |
| 20 | + } |
| 21 | + |
| 22 | + const mappedArch = archMap[arch] |
| 23 | + if (!mappedArch) { |
| 24 | + throw new Error(`Unsupported architecture: ${arch}`) |
| 25 | + } |
| 26 | + |
| 27 | + return mappedArch |
| 28 | +} |
| 29 | + |
| 30 | +async function installWindows() { |
| 31 | + const platform = detectWindowsPlatform() |
| 32 | + core.info(`Detected platform: ${platform}`) |
| 33 | + |
| 34 | + core.info('Fetching version metadata...') |
| 35 | + const { version, installer, sha256 } = await getVersionMetadata(platform) |
| 36 | + core.info(`Latest version: ${version}`) |
| 37 | + core.info(`Download URL: ${installer}`) |
| 38 | + |
| 39 | + const tmpDir = tmpdir() |
| 40 | + const installerPath = join(tmpDir, 'IBM_Cloud_CLI.exe') |
| 41 | + core.info('Downloading IBM Cloud CLI...') |
| 42 | + await downloadFile(installer, installerPath) |
| 43 | + |
| 44 | + core.info('Verifying checksum...') |
| 45 | + const actualSha256 = await calculateSha256(installerPath) |
| 46 | + if (actualSha256 !== sha256) { |
| 47 | + fs.unlinkSync(installerPath) |
| 48 | + throw new Error(`Checksum mismatch! Expected: ${sha256}, Got: ${actualSha256}`) |
| 49 | + } |
| 50 | + core.info('Checksum verified successfully') |
| 51 | + |
| 52 | + core.info('Running installer...') |
| 53 | + await exec.exec('powershell', ['-command', `& "${installerPath}" /s /v"REBOOT=ReallySuppress /qn"`]) |
| 54 | + |
| 55 | + core.info('Cleaning up temporary files...') |
| 56 | + fs.unlinkSync(installerPath) |
| 57 | + |
| 58 | + // Add to PATH for the current step |
| 59 | + process.env.PATH += ';C:\\Program Files\\IBM\\Cloud\\bin' |
| 60 | + // Add to GITHUB_PATH for future steps |
| 61 | + await exec.exec('powershell', ['-command', `Add-Content $env:GITHUB_PATH 'C:\\Program Files\\IBM\\Cloud\\bin'`]) |
| 62 | + |
| 63 | + core.info('IBM Cloud CLI installed successfully') |
| 64 | +} |
| 65 | + |
| 66 | +export { installWindows } |
0 commit comments