Skip to content

Commit

Permalink
feat: add drive type support
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 committed May 12, 2024
1 parent 3198c3a commit d1adbd9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,25 @@ jobs:
drive-format: NTFS
drive-path: "my_awesome_drive.vhdx"
workspace-copy: true

test-large-fixed:
runs-on: windows-2022
steps:
- name: Check out source code
uses: actions/checkout@v4
- name: Setup Dev Drive
uses: ./
with:
drive-size: 10GB
drive-type: Dynamic

test-large-dynamic:
runs-on: windows-2022
steps:
- name: Check out source code
uses: actions/checkout@v4
- name: Setup Dev Drive
uses: ./
with:
drive-size: 10GB
drive-type: Dynamic
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You can optionally pass parameters to the action as follows:
with:
drive-size: 1GB
drive-format: ReFS
drive-type: Fixed
drive-path: "dev_drive.vhdx"
workspace-copy: true
```
Expand Down Expand Up @@ -55,6 +56,12 @@ workspace drive letter.
When an absolute path is provided, make sure it's located outside `${{ github.workspace }}`
otherwise `workspace-copy` can cause issues.

#### `drive-type`

Determines the type of drive, `Fixed` or `Dynamic`. There are performance tradeoffs between
both, hence for the purposes of this action `Fixed` is used by default.
`Dynamic` is useful when you want to cache the disk across job runs as it yields a smaller
payload to cache when the job ends.

#### `workspace-copy`

Expand Down Expand Up @@ -97,7 +104,7 @@ your dev drive workspace will be `E:\<project-name>` by default assuming the dri
env:
CARGO_HOME: ${{ env.DEV_DRIVE }}/.cargo
RUSTUP_HOME: ${{ env.DEV_DRIVE }}/.rustup
run: rustup show
run: rustup show
```

## Runner Compatibility
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
drive-path:
description: "Absolute or relative path to the Dev Drive storage location. Example: C:/path/to/my/dev_drive.vhdx"
default: "dev_drive.vhdx"
drive-type:
description: "Determines whether all space is allocated initially or over time. Example: Fixed or Dynamic."
default: "Fixed"
workspace-copy:
description: "Copy your GITHUB_WORKSPACE checkout to your Dev Drive. Examples: true, false."
default: "false"
Expand Down
2 changes: 1 addition & 1 deletion dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ExternalInputs {
DriveSize = 'drive-size',
DriveFormat = 'drive-format',
DrivePath = 'drive-path',
DriveType = 'drive-type',
WorkspaceCopy = 'workspace-copy',
}

Expand All @@ -18,6 +19,8 @@ export enum GithubVariables {
GithubWorkspace = 'GITHUB_WORKSPACE',
}

export const VHDDriveTypes = new Set(['Fixed', 'Dynamic'])

export const NATIVE_DEV_DRIVE_WIN_VERSION = '10.0.22621'

export const POWERSHELL_BIN = 'pwsh.exe'
Expand Down
3 changes: 2 additions & 1 deletion src/setup-dev-drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ async function main() {
const driveSize = core.getInput(ExternalInputs.DriveSize)
const driveFormat = core.getInput(ExternalInputs.DriveFormat)
const drivePath = core.getInput(ExternalInputs.DrivePath)
const driveType = core.getInput(ExternalInputs.DriveType)
const copyWorkspace = core.getBooleanInput(ExternalInputs.WorkspaceCopy)
await setup(driveSize, driveFormat, drivePath, copyWorkspace)
await setup(driveSize, driveFormat, drivePath, driveType, copyWorkspace)
}

main().catch(err => core.setFailed(err.message))
30 changes: 26 additions & 4 deletions src/setup-impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import { toPlatformPath } from '@actions/core'
import { exec, ExecOptions } from '@actions/exec'
import { quote } from 'shell-quote'
import os from 'node:os'
Expand All @@ -12,15 +13,16 @@ import {
NATIVE_DEV_DRIVE_WIN_VERSION,
POWERSHELL_BIN,
StateVariables,
VHDDriveTypes,
VHDX_EXTENSION,
WIN_PLATFORM,
} from './constants'
import { toPlatformPath } from '@actions/core'

function genSetupCommandArgs(
driveSize: string,
driveFormat: string,
drivePath: string,
driveType: string,
): string[] {
const sizeArg = quote([driveSize])
const formatArg = quote([driveFormat])
Expand All @@ -36,7 +38,7 @@ function genSetupCommandArgs(
}

const pwshCommand = [
`$DevDrive = New-VHD -Path ${pathArg} -SizeBytes ${sizeArg} -Fixed |`,
`$DevDrive = New-VHD -Path ${pathArg} -SizeBytes ${sizeArg} -${driveType} |`,
'Mount-VHD -Passthru |',
'Initialize-Disk -Passthru |',
'New-Partition -AssignDriveLetter -UseMaximumSize |',
Expand All @@ -53,6 +55,7 @@ async function execSetupCommand(
driveSize: string,
driveFormat: string,
drivePath: string,
driveType: string,
): Promise<string> {
const options: ExecOptions = {}
let outStr = ''
Expand All @@ -66,7 +69,12 @@ async function execSetupCommand(
},
}

const pwshCommandArgs = genSetupCommandArgs(driveSize, driveFormat, drivePath)
const pwshCommandArgs = genSetupCommandArgs(
driveSize,
driveFormat,
drivePath,
driveType,
)

await exec(POWERSHELL_BIN, pwshCommandArgs, options)

Expand Down Expand Up @@ -98,6 +106,7 @@ async function doCreateDevDrive(
driveSize: string,
driveFormat: string,
drivePath: string,
driveType: string,
): Promise<string> {
// Normalize User Path Input
let normalizedDrivePath = toPlatformPath(drivePath)
Expand All @@ -108,11 +117,18 @@ async function doCreateDevDrive(
throw new Error(`Make sure ${ExternalInputs.DrivePath} ends with ${VHDX_EXTENSION}`)
}

// Check Drive Type
if (!VHDDriveTypes.has(driveType)) {
const allowedMsg = [...VHDDriveTypes].join(' or ')
throw new Error(`Make sure ${ExternalInputs.DriveType} is either ${allowedMsg}`)
}

core.info('Creating Dev Drive...')
const driveLetter = await execSetupCommand(
driveSize,
driveFormat,
normalizedDrivePath,
driveType,
)

core.debug(`Exporting EnvVar ${EnvVariables.DevDrive}=${driveLetter}`)
Expand Down Expand Up @@ -150,13 +166,19 @@ export async function setup(
driveSize: string,
driveFormat: string,
drivePath: string,
driveType: string,
copyWorkspace: boolean,
): Promise<void> {
if (process.platform !== WIN_PLATFORM) {
throw new Error('This action can only run on windows.')
}

const driveLetter = await doCreateDevDrive(driveSize, driveFormat, drivePath)
const driveLetter = await doCreateDevDrive(
driveSize,
driveFormat,
drivePath,
driveType,
)

if (copyWorkspace) {
await doCopyWorkspace(driveLetter)
Expand Down

0 comments on commit d1adbd9

Please sign in to comment.