Skip to content

Commit

Permalink
Add support for running on Windows via WSL (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <kzantow@gmail.com>
  • Loading branch information
kzantow authored Oct 11, 2021
1 parent df95fc4 commit 04df1d9
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 26 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ jobs:
- run: git diff --exit-code

test-on-fixture-dirs:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: Vampire/setup-wsl@v1
if: ${{ matrix.os == 'windows-latest' }}
with:
distribution: Alpine

- uses: actions/checkout@v2
with:
path: ./
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ Output parameters:
`cmd` can be referenced in a workflow like other output parameters:
`${{ steps.<step-id>.outputs.cmd }}`

## Windows

Windows is currently supported via Windows Subsystem for Linux (WSL). It is
required to set up a WSL distribution prior to invoking the `sbom-action`, for
example, you can add the small Alpine image:

```yaml
- uses: Vampire/setup-wsl@v1
with:
distribution: Alpine
```

## Diagnostics

This action makes extensive use of GitHub Action debug logging,
Expand Down
35 changes: 29 additions & 6 deletions dist/attachReleaseAssets/index.js

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

35 changes: 29 additions & 6 deletions dist/downloadSyft/index.js

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

35 changes: 29 additions & 6 deletions dist/runSyftAction/index.js

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

42 changes: 37 additions & 5 deletions src/github/SyftGithubAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ export function getArtifactName(): string {
return `${repo}-${job}${stepName}.${extension}`;
}

/**
* Maps the given parameter to a Windows Subsystem for Linux style path
* @param arg
*/
export function mapToWSLPath(arg: string) {
return arg.replace(
/^([A-Z]):(.*)$/,
(v, drive, path) => `/mnt/${drive.toLowerCase()}${path.replace(/\\/g, "/")}`
);
}

/**
* Execute directly for linux & macOS and use WSL for Windows
* @param cmd command to execute
* @param args command args
* @param options command options
*/
async function execute(
cmd: string,
args: string[],
options?: exec.ExecOptions
) {
if (process.platform === "win32") {
return await exec.exec(
"wsl",
[mapToWSLPath(cmd), ...args.map(mapToWSLPath)],
options
);
} else {
return exec.exec(cmd, args, options);
}
}

/**
* Gets a reference to the syft command and executes the syft action
* @param input syft input parameters
Expand Down Expand Up @@ -127,7 +160,7 @@ async function executeSyft({ input, format }: SyftOptions): Promise<string> {
});

const exitCode = await core.group("Executing Syft...", async () =>
exec.exec(cmd, args, {
execute(cmd, args, {
env,
outStream,
listeners: {
Expand Down Expand Up @@ -167,12 +200,11 @@ export async function downloadSyft(): Promise<string> {
const installPath = await cache.downloadTool(url);

// Make sure the tool's executable bit is set
await exec.exec(`chmod +x ${installPath}`);
const syftBinaryPath = `${installPath}_${name}`;

const cmd = `${installPath} -b ${installPath}_${name} ${version}`;
await exec.exec(cmd);
await execute("sh", [installPath, "-d", "-b", syftBinaryPath, version]);

return `${installPath}_${name}/${name}`;
return `${syftBinaryPath}/${name}`;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/SyftGithubAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as os from "os";
import * as path from "path";
import * as action from "../src/github/SyftGithubAction";
import {
downloadSyft,
downloadSyft, mapToWSLPath,
runAndFailBuildOnException
} from "../src/github/SyftGithubAction";

Expand Down Expand Up @@ -274,5 +274,11 @@ describe("Action", () => {
});

expect(action.getArtifactName()).toBe("something-something-image-image.spdx.json");
})
});

it ("properly maps paths for WSL", () => {
expect(mapToWSLPath("basic arg")).toBe("basic arg");
expect(mapToWSLPath("D:\\Some\\Path")).toBe("/mnt/d/Some/Path");
expect(mapToWSLPath("C:\\Some\\Path")).toBe("/mnt/c/Some/Path");
});
});

0 comments on commit 04df1d9

Please sign in to comment.