|
1 | 1 | import * as core from '@actions/core' |
| 2 | +import * as github from '@actions/github' |
2 | 3 | import { arch } from 'os' |
3 | 4 | import { platform } from 'process' |
4 | 5 | import fs from 'fs'; |
5 | 6 | import { mkdir } from 'fs/promises'; |
6 | 7 | import { Readable } from 'stream'; |
7 | 8 | import { finished } from 'stream/promises'; |
8 | 9 | import path from 'path'; |
| 10 | +import { createReadStream } from 'fs'; |
| 11 | +import { createWriteStream } from 'fs'; |
| 12 | +import { pipeline } from 'stream'; |
| 13 | +import { promisify } from 'util'; |
| 14 | +import unzipper from 'unzipper'; |
| 15 | +import execa from 'execa'; |
9 | 16 |
|
10 | 17 | export async function run(): Promise<void> { |
11 | 18 | try { |
12 | | - // const ms: string = core.getInput('milliseconds') |
| 19 | + const action = core.getInput('action'); |
| 20 | + const project = core.getInput('project'); |
| 21 | + let pipelabVersion = core.getInput('pipelab-version'); |
13 | 22 |
|
14 | | - // // Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true |
15 | | - // core.debug(`Waiting ${ms} milliseconds ...`) |
| 23 | + console.log(`Action: ${action}`); |
| 24 | + console.log(`Project: ${project}`); |
| 25 | + console.log(`Pipelab Version: ${pipelabVersion}`); |
16 | 26 |
|
17 | | - // // Log the current timestamp, wait, then log the new timestamp |
18 | | - // core.debug(new Date().toTimeString()) |
19 | | - // await wait(parseInt(ms, 10)) |
20 | | - // core.debug(new Date().toTimeString()) |
21 | | - |
22 | | - // // Set outputs for other workflow steps to use |
23 | | - // core.setOutput('time', new Date().toTimeString()) |
| 27 | + if (!pipelabVersion) { |
| 28 | + console.log('No Pipelab version specified, fetching the latest version...'); |
| 29 | + const res = await fetch('https://api.github.com/repos/CynToolkit/pipelab/releases/latest'); |
| 30 | + const latestRelease = await res.json(); |
| 31 | + pipelabVersion = latestRelease.tag_name.replace('v', ''); |
| 32 | + console.log(`Latest Pipelab Version: ${pipelabVersion}`); |
| 33 | + } |
24 | 34 |
|
25 | | - const currentArch = arch() |
26 | | - const currentPlatform = platform |
| 35 | + const currentArch = arch(); |
| 36 | + const currentPlatform = platform; |
27 | 37 |
|
28 | | - console.log(`Current Arch: ${currentArch}`) |
29 | | - console.log(`Current Platform: ${currentPlatform}`) |
| 38 | + console.log(`Current Arch: ${currentArch}`); |
| 39 | + console.log(`Current Platform: ${currentPlatform}`); |
30 | 40 |
|
31 | 41 | const downloadFile = (async (url: string, fileName: string) => { |
| 42 | + console.log(`Downloading file from ${url} to ${fileName}`); |
32 | 43 | const res = await fetch(url); |
33 | 44 | if (!res.body) { |
34 | 45 | throw new Error('Response body is undefined'); |
35 | 46 | } |
36 | | - if (!fs.existsSync("downloads")) await mkdir("downloads"); //Optional if you already have downloads directory |
| 47 | + if (!fs.existsSync("downloads")) { |
| 48 | + console.log('Creating downloads directory...'); |
| 49 | + await mkdir("downloads"); |
| 50 | + } |
37 | 51 | const destination = path.resolve("./downloads", fileName); |
38 | 52 | const fileStream = fs.createWriteStream(destination, { flags: 'wx' }); |
39 | 53 | await finished(Readable.fromWeb(res.body).pipe(fileStream)); |
| 54 | + console.log(`Downloaded file to ${destination}`); |
40 | 55 | }); |
41 | 56 |
|
| 57 | + const extractZip = async (filePath: string, extractTo: string) => { |
| 58 | + console.log(`Extracting zip file from ${filePath} to ${extractTo}`); |
| 59 | + await pipeline( |
| 60 | + createReadStream(filePath), |
| 61 | + unzipper.Extract({ path: extractTo }) |
| 62 | + ); |
| 63 | + console.log(`Extracted zip file to ${extractTo}`); |
| 64 | + }; |
42 | 65 |
|
43 | 66 | if (currentArch === 'x64' && currentPlatform === 'win32') { |
44 | | - core.setFailed('You are using a 64-bit Windows machine') |
| 67 | + core.setFailed('You are using a 64-bit Windows machine'); |
45 | 68 | } |
46 | 69 | else if (currentArch === 'x64' && currentPlatform === 'linux') { |
47 | | - console.log('You are using a 64-bit Linux machine') |
48 | | - await downloadFile("https://github.com/CynToolkit/pipelab/releases/download/v1.4.8/Pipelab-linux-x64-1.4.8.zip", "pipelab.zip") |
| 70 | + console.log('You are using a 64-bit Linux machine'); |
| 71 | + await downloadFile(`https://github.com/CynToolkit/pipelab/releases/download/v${pipelabVersion}/Pipelab-linux-x64-${pipelabVersion}.zip`, "pipelab.zip"); |
| 72 | + await extractZip("./downloads/pipelab.zip", "./downloads"); |
| 73 | + |
| 74 | + const workspace = process.env.GITHUB_WORKSPACE || ''; |
| 75 | + const jsonProject = path.resolve(workspace, project); |
| 76 | + const tmpLogFile = path.resolve('./downloads', 'log.txt'); |
| 77 | + const args = ['--', '--project', jsonProject, '--action', action, '--output', tmpLogFile]; |
| 78 | + |
| 79 | + console.log(`Running Pipelab with args: ${args.join(' ')}`); |
| 80 | + try { |
| 81 | + const { stdout } = await execa('./downloads/pipelab', args); |
| 82 | + console.log(stdout); |
| 83 | + } catch (error) { |
| 84 | + console.error(error.stderr); |
| 85 | + core.setFailed(error.stderr); |
| 86 | + } |
49 | 87 | } |
50 | 88 | else { |
51 | | - core.setFailed('You are using an unsupported platform') |
| 89 | + core.setFailed('You are using an unsupported platform'); |
52 | 90 | } |
53 | 91 | } catch (error) { |
54 | | - // Fail the workflow run if an error occurs |
55 | | - if (error instanceof Error) core.setFailed(error.message) |
| 92 | + console.error(error); |
| 93 | + if (error instanceof Error) core.setFailed(error.message); |
56 | 94 | } |
57 | 95 | } |
0 commit comments