Skip to content

Commit e848ef8

Browse files
committed
Switch from curl | sh style to a direct download for linux
Signed-off-by: Jason Frey <fryguy9@gmail.com>
1 parent d8ac2c2 commit e848ef8

5 files changed

Lines changed: 141 additions & 8 deletions

File tree

dist/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/install-linux.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 detectLinuxPlatform() {
9+
const arch = process.arch
10+
const platform = process.platform
11+
12+
if (platform !== 'linux') {
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': 'linux64',
19+
'ia32': 'linux32',
20+
'ppc64': 'ppc64le',
21+
's390x': 's390x',
22+
'arm64': 'linux-arm64'
23+
}
24+
25+
const mappedArch = archMap[arch]
26+
if (!mappedArch) {
27+
throw new Error(`Unsupported architecture: ${arch}`)
28+
}
29+
30+
return mappedArch
31+
}
32+
33+
async function installLinux() {
34+
const platform = detectLinuxPlatform()
35+
core.info(`Detected platform: ${platform}`)
36+
37+
core.info('Fetching version metadata...')
38+
const { version, installer, sha256 } = await getVersionMetadata(platform)
39+
core.info(`Latest version: ${version}`)
40+
core.info(`Download URL: ${installer}`)
41+
42+
const tmpDir = tmpdir()
43+
const tarballPath = join(tmpDir, 'IBM_Cloud_CLI.tar.gz')
44+
core.info('Downloading IBM Cloud CLI...')
45+
await downloadFile(installer, tarballPath)
46+
47+
core.info('Verifying checksum...')
48+
const actualSha256 = await calculateSha256(tarballPath)
49+
if (actualSha256 !== sha256) {
50+
fs.unlinkSync(tarballPath)
51+
throw new Error(`Checksum mismatch! Expected: ${sha256}, Got: ${actualSha256}`)
52+
}
53+
core.info('Checksum verified successfully')
54+
55+
const extractDir = join(tmpDir, 'Bluemix_CLI')
56+
core.info('Extracting installer...')
57+
await exec.exec('mkdir', ['-p', extractDir])
58+
await exec.exec('tar', ['-xzf', tarballPath, '-C', extractDir, '--strip-components=1'])
59+
60+
core.info('Running installer...')
61+
const installScript = join(extractDir, 'install')
62+
await exec.exec('chmod', ['755', installScript])
63+
await exec.exec(installScript, ['-q'])
64+
65+
core.info('Cleaning up temporary files...')
66+
fs.unlinkSync(tarballPath)
67+
await exec.exec('rm', ['-rf', extractDir])
68+
69+
core.info('IBM Cloud CLI installed successfully')
70+
}
71+
72+
export { installLinux }

src/install-util.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import fs from 'fs'
2+
import crypto from 'crypto'
3+
import { writeFile } from 'fs/promises'
4+
5+
async function downloadFile(url, destPath) {
6+
const response = await fetch(url)
7+
8+
if (!response.ok) {
9+
throw new Error(`Failed to download: ${response.status} ${response.statusText}`)
10+
}
11+
12+
const buffer = await response.arrayBuffer()
13+
await writeFile(destPath, Buffer.from(buffer))
14+
}
15+
16+
async function fetchJson(url) {
17+
const response = await fetch(url)
18+
19+
if (!response.ok) {
20+
throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`)
21+
}
22+
23+
return response.json()
24+
}
25+
26+
async function calculateSha256(filePath) {
27+
const hash = crypto.createHash('sha256')
28+
const stream = fs.createReadStream(filePath)
29+
30+
for await (const chunk of stream) {
31+
hash.update(chunk)
32+
}
33+
34+
return hash.digest('hex')
35+
}
36+
37+
async function getVersionMetadata(platform) {
38+
const infoUrl = 'https://download.clis.cloud.ibm.com/ibm-cloud-cli-metadata-dn/info.json'
39+
const info = await fetchJson(infoUrl)
40+
const latestVersion = info.latestVersion
41+
42+
const allVersionsUrl = 'https://download.clis.cloud.ibm.com/ibm-cloud-cli-metadata-dn/all_versions.json'
43+
const allVersions = await fetchJson(allVersionsUrl)
44+
45+
// Find the version object matching latestVersion
46+
const versionData = allVersions.find(v => v.version === latestVersion)
47+
if (!versionData) {
48+
throw new Error(`Version ${latestVersion} not found in metadata`)
49+
}
50+
51+
// Get installer from binaries section
52+
const platformData = versionData.binaries?.[platform]
53+
if (!platformData) {
54+
throw new Error(`No download available for platform: ${platform}`)
55+
}
56+
57+
return {
58+
version: latestVersion,
59+
installer: platformData.url,
60+
sha256: platformData.sha256_checksum
61+
}
62+
}
63+
64+
export { downloadFile, fetchJson, calculateSha256, getVersionMetadata }

src/install.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core'
22
import * as exec from '@actions/exec'
33
import * as io from '@actions/io'
4+
import { installLinux } from './install-linux.js'
45

56
async function installWindows() {
67
await exec.exec(`powershell -command "iex (New-Object Net.WebClient).DownloadString('https://clis.cloud.ibm.com/install/powershell')"`)
@@ -14,10 +15,6 @@ async function installMacOS() {
1415
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/osx | sh"')
1516
}
1617

17-
async function installLinux() {
18-
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/linux | sh"')
19-
}
20-
2118
async function install() {
2219
core.startGroup('Installing IBM Cloud CLI')
2320
const cliPath = await io.which("ibmcloud")

0 commit comments

Comments
 (0)