Skip to content

Commit

Permalink
feat: validate dev drive inside github workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 committed May 13, 2024
1 parent 4630757 commit 7377931
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ jobs:
working-directory: ${{ env.DEV_DRIVE }}
run: New-Item test.txt

test-copy-inside-workspace:
strategy:
fail-fast: false
matrix:
runs-on: [ windows-2022 ]
workspace-copy: [ true, false ]
runs-on: ${{ matrix.runs-on }}
steps:
- name: Check out source code
uses: actions/checkout@v4
- name: Setup Dev Drive
id: setup-drive
continue-on-error: true
uses: ./
with:
drive-path: "${{ github.workspace }}/dev_drive.vhdx"
workspace-copy: ${{ matrix.workspace-copy }}
- name: Fail when `workspace-copy` and failure is not the outcome
if: ${{ matrix.workspace-copy && steps.setup-drive.outcome != 'failure' }}
run: exit 1
- name: Fail when not `workspace-copy` and success is not the outcome
if: ${{ !matrix.workspace-copy && steps.setup-drive.outcome != 'success' }}
run: exit 1

test-cache-storage:
runs-on: windows-2022
steps:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.1

* Added error check when Dev Drive is located inside `${{ github.workspace }}`.

# 2.1.0

* Added input option `mount-if-exists` to allow mounting pre-existing VHDX drives (e.g. from `actions/cache`).
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Note, when a relative path is provided, it will be relative to `C:\` or the defa
workspace drive letter.

When an absolute path is provided, make sure it's located outside `${{ github.workspace }}`
otherwise `workspace-copy` can cause issues.
otherwise `workspace-copy` can cause issues. This action will raise an error in such cases.

#### `drive-type`

Expand Down Expand Up @@ -144,4 +144,4 @@ still get speed gains by using this action due to `ReFS` and `VHDX` usage.

## Credits

Thanks to Paco Sevilla for the idea to use a VHDX within a Github Workflow.
Thanks to Paco Sevilla for the idea to use a VHDX within a GitHub Workflow.
2 changes: 1 addition & 1 deletion dist/main.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
ignores: [
'lib/',
'dist/',
'node_modules/',
]
}
);
eslint.configs.recommended,
...tseslint.configs.recommended,
{
ignores: [
'lib/',
'dist/',
'node_modules/',
],
},
)
56 changes: 31 additions & 25 deletions src/setup-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,10 @@ async function doDevDriveCommand(
driveType: string,
mountIfExists: boolean,
): Promise<string> {
// Normalize User Path Input
let normalizedDrivePath = toPlatformPath(drivePath)
if (!path.isAbsolute(drivePath)) {
normalizedDrivePath = path.resolve('/', normalizedDrivePath)
}
if (path.extname(normalizedDrivePath) !== VHDX_EXTENSION) {
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}`)
}

if (mountIfExists) {
try {
// Do a check on the Dev Drive
await fs.access(normalizedDrivePath, fs.constants.F_OK | fs.constants.R_OK)
await fs.access(drivePath, fs.constants.F_OK | fs.constants.R_OK)
} catch (e) {
core.debug((e as NodeJS.ErrnoException).message)
// Fallback to creation...
Expand All @@ -50,27 +35,27 @@ async function doDevDriveCommand(
let driveLetter
if (!mountIfExists) {
core.info('Creating Dev Drive...')
driveLetter = await create(driveSize, driveFormat, normalizedDrivePath, driveType)
driveLetter = await create(driveSize, driveFormat, drivePath, driveType)
core.info('Successfully created Dev Drive...')
} else {
core.info('Mounting Dev Drive...')
driveLetter = await mount(normalizedDrivePath)
driveLetter = await mount(drivePath)
core.info('Successfully mounted Dev Drive...')
}

core.debug(`Exporting EnvVar ${EnvVariables.DevDrive}=${driveLetter}`)
core.exportVariable(EnvVariables.DevDrive, driveLetter)

core.debug(`Exporting EnvVar ${EnvVariables.DevDrivePath}=${normalizedDrivePath}`)
core.exportVariable(EnvVariables.DevDrivePath, normalizedDrivePath)
core.debug(`Exporting EnvVar ${EnvVariables.DevDrivePath}=${drivePath}`)
core.exportVariable(EnvVariables.DevDrivePath, drivePath)

core.debug(`Saving State ${StateVariables.DevDrivePath}=${normalizedDrivePath}`)
core.saveState(StateVariables.DevDrivePath, normalizedDrivePath)
core.debug(`Saving State ${StateVariables.DevDrivePath}=${drivePath}`)
core.saveState(StateVariables.DevDrivePath, drivePath)

return driveLetter
}

async function doCopyWorkspace(driveLetter: string) {
async function doCopyWorkspace(driveLetter: string, drivePath: string) {
const githubWorkspace = process.env[GithubVariables.GithubWorkspace]
if (!githubWorkspace) {
throw new Error(
Expand All @@ -81,6 +66,13 @@ async function doCopyWorkspace(driveLetter: string) {
const copyFrom = path.resolve(githubWorkspace)
const copyTo = path.resolve(driveLetter, path.basename(copyFrom))

const isDevDriveInWorkspace = drivePath.startsWith(copyFrom)
if (isDevDriveInWorkspace) {
throw new Error(
`Your dev drive is located inside the Github Workspace when ${ExternalInputs.WorkspaceCopy} is enabled! ${drivePath}`,
)
}

core.info(`Copying workspace from ${copyFrom} to ${copyTo}...`)
await fs.copy(copyFrom, copyTo)

Expand All @@ -101,16 +93,30 @@ export async function setup(
if (process.platform !== WIN_PLATFORM) {
throw new Error('This action can only run on windows.')
}
// Normalize User Path Input
let normalizedDrivePath = toPlatformPath(drivePath)
if (!path.isAbsolute(drivePath)) {
normalizedDrivePath = path.resolve('/', normalizedDrivePath)
}
if (path.extname(normalizedDrivePath) !== VHDX_EXTENSION) {
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}`)
}

const driveLetter = await doDevDriveCommand(
driveSize,
driveFormat,
drivePath,
normalizedDrivePath,
driveType,
mountIfExists,
)

if (copyWorkspace) {
await doCopyWorkspace(driveLetter)
await doCopyWorkspace(driveLetter, normalizedDrivePath)
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sourceMap": true
},
"exclude": [
"./lib",
"./dist",
"./node_modules"
]
Expand Down

0 comments on commit 7377931

Please sign in to comment.