From e1a7863be630ea22c506f848a0762cb17e0534f8 Mon Sep 17 00:00:00 2001 From: Sergey Ukustov Date: Fri, 28 May 2021 22:40:45 +0300 Subject: [PATCH] feat: get linux version from os-release file if available (#594) --- .../tool-cache/__tests__/manifest.test.ts | 44 ++++++++++++++++++- packages/tool-cache/src/manifest.ts | 20 ++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/packages/tool-cache/__tests__/manifest.test.ts b/packages/tool-cache/__tests__/manifest.test.ts index 51707694..37146b89 100644 --- a/packages/tool-cache/__tests__/manifest.test.ts +++ b/packages/tool-cache/__tests__/manifest.test.ts @@ -116,7 +116,7 @@ describe('@actions/tool-cache-manifest', () => { expect(file?.filename).toBe('sometool-1.2.3-linux-x64.tar.gz') }) - it('can match with linux platform version spec', async () => { + it('can match with linux platform version spec from lsb-release', async () => { os.platform = 'linux' os.arch = 'x64' @@ -150,6 +150,48 @@ describe('@actions/tool-cache-manifest', () => { expect(file?.filename).toBe('sometool-1.2.4-ubuntu1804-x64.tar.gz') }) + it('can match with linux platform version spec from os-release', async () => { + os.platform = 'linux' + os.arch = 'x64' + + readLsbSpy.mockImplementation(() => { + return `NAME="Ubuntu" + VERSION="18.04.5 LTS (Bionic Beaver)" + ID=ubuntu + ID_LIKE=debian + PRETTY_NAME="Ubuntu 18.04.5 LTS" + VERSION_ID="18.04" + HOME_URL="https://www.ubuntu.com/" + SUPPORT_URL="https://help.ubuntu.com/" + BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" + PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" + VERSION_CODENAME=bionic + UBUNTU_CODENAME=bionic` + }) + + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( + owner, + repo, + fakeToken + ) + const release: tc.IToolRelease | undefined = await tc.findFromManifest( + '1.2.4', + true, + manifest + ) + expect(release).toBeDefined() + expect(release?.version).toBe('1.2.4') + expect(release?.files.length).toBe(1) + const file = release?.files[0] + expect(file).toBeDefined() + expect(file?.arch).toBe('x64') + expect(file?.platform).toBe('linux') + expect(file?.download_url).toBe( + 'https://github.com/actions/sometool/releases/tag/1.2.4-20200402.6/sometool-1.2.4-ubuntu1804-x64.tar.gz' + ) + expect(file?.filename).toBe('sometool-1.2.4-ubuntu1804-x64.tar.gz') + }) + it('can match with darwin platform version spec', async () => { os.platform = 'darwin' os.arch = 'x64' diff --git a/packages/tool-cache/src/manifest.ts b/packages/tool-cache/src/manifest.ts index a85e771c..e52819b4 100644 --- a/packages/tool-cache/src/manifest.ts +++ b/packages/tool-cache/src/manifest.ts @@ -135,8 +135,15 @@ export function _getOsVersion(): string { const lines = lsbContents.split('\n') for (const line of lines) { const parts = line.split('=') - if (parts.length === 2 && parts[0].trim() === 'DISTRIB_RELEASE') { - version = parts[1].trim() + if ( + parts.length === 2 && + (parts[0].trim() === 'VERSION_ID' || + parts[0].trim() === 'DISTRIB_RELEASE') + ) { + version = parts[1] + .trim() + .replace(/^"/, '') + .replace(/"$/, '') break } } @@ -147,11 +154,14 @@ export function _getOsVersion(): string { } export function _readLinuxVersionFile(): string { - const lsbFile = '/etc/lsb-release' + const lsbReleaseFile = '/etc/lsb-release' + const osReleaseFile = '/etc/os-release' let contents = '' - if (fs.existsSync(lsbFile)) { - contents = fs.readFileSync(lsbFile).toString() + if (fs.existsSync(lsbReleaseFile)) { + contents = fs.readFileSync(lsbReleaseFile).toString() + } else if (fs.existsSync(osReleaseFile)) { + contents = fs.readFileSync(osReleaseFile).toString() } return contents