Skip to content

Commit

Permalink
Fix #65: Add support for disabling file syncing
Browse files Browse the repository at this point in the history
This adds a new input parameter, `sync_files`. It allows to specify
which directions files should be synced. From the runner to the VM,
from the VM to the runner, both or none.
  • Loading branch information
jacob-carlborg committed Oct 24, 2023
1 parent cc49e99 commit de0e1f1
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 20 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,47 @@ jobs:
version: '13.2'
hypervisor: qemu
run: dmesg | grep -i avx2

test-sync-files:
timeout-minutes: 5
name: 'Test sync files: ${{ matrix.data.direction }}'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
data:
- direction: runner-to-vm
run: test -f foo.txt && touch bar.txt
run_after: '! test -f bar.txt' # The new files from the VM should not sync back

- direction: vm-to-runner
run: '[ ! -f foo.txt ] && touch bar.txt'
run_after: 'test -f bar.txt' # The new files from the VM should sync back

- direction: false
run: '[ ! -f foo.txt ] && touch bar.txt'
run_after: '! test -f bar.txt' # The new files from the VM should not sync back

- direction: true
run: test -f foo.txt && touch bar.txt
run_after: test -f bar.txt

steps:
- name: Checkout
uses: actions/checkout@v3
with:
persist-credentials: false

- run: touch foo.txt

- name: Test
uses: ./
with:
operating_system: freebsd
architecture: x86-64
version: '13.2'
sync_files: ${{ matrix.data.direction }}
run: ${{ matrix.data.run }}

- name: Run after
run: ${{ matrix.data.run_after }}
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ inputs:
required: false
description: URL for running the action with a custom image.
default: ''
sync_files:
required: false
description: |
Specifies if the local files should be synchronized to virtual
machine and in which direction. Valid values are `true`, `false`,
`runner-to-vm` and `vm-to-runner`.
default: 'true'

runs:
using: node16
Expand Down
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.20.0] - 2023-10-24
### Added
- Add support for disabling file syncing ([#65](https://github.com/cross-platform-actions/action/issues/65)).
This adds a new input parameter, `sync_files`. It allows to specify
which directions files should be synced. From the runner to the VM,
from the VM to the runner, both or none.

## [0.19.1] - 2023-10-07
### Fixed
- NetBSD - VM doesn't start ([#62](https://github.com/cross-platform-actions/action/issues/62))
Expand Down Expand Up @@ -133,8 +140,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release

[Unreleased]: https://github.com/cross-platform-actions/action/compare/v0.19.1...HEAD
[Unreleased]: https://github.com/cross-platform-actions/action/compare/v0.20.0...HEAD

[0.19.1]: https://github.com/cross-platform-actions/action/compare/v0.19.1...v0.20.0
[0.19.1]: https://github.com/cross-platform-actions/action/compare/v0.19.0...v0.19.1
[0.19.0]: https://github.com/cross-platform-actions/action/compare/v0.18.0...v0.19.0
[0.18.0]: https://github.com/cross-platform-actions/action/compare/v0.17.0...v0.18.0
Expand Down
69 changes: 66 additions & 3 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@ Different platforms need to run on different runners, so see the

This section lists the available inputs for the action.

| Input | Required | Default Value | Type | Description |
|-------------------------|----------|-------------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `run` ||| string | Runs command-line programs using the operating system's shell. This will be executed inside the virtual machine. |
| `operating_system` ||| string | The type of operating system to run the job on. See [Supported Platforms](#supported-platforms). |
| `architecture` || `x86-64` | string | The architecture of the operating system. See [Supported Platforms](#supported-platforms). |
| `version` ||| string | The version of the operating system to use. See [Supported Platforms](#supported-platforms). |
| `shell` || `default` | string | The shell to use to execute the commands. Defaults to the default shell for the given operating system. Allowed values are: `default`, `sh` and `bash` |
| `environment_variables` || `""` | string | A list of environment variables to forward to the virtual machine. The list should be separated with spaces. |
| `memory` || `6G` or `13G` | string | The amount of memory for the virtual machine. The default value is `6G` for Linux runners and `13G` for macOS runners. |
| `cpu_count` || `2` or `3` cores | integer | The number of CPU cores for the virtual machine. The default value is `2` for Linux runners and `3` for macOS runners. |
| `hypervisor` || `xhyve` or `qemu` | string | The hypervisor to use for running the virtual machine. For Linux runners the only valid value is `qemu`. For macOS runners the default for OpenBSD and FreeBSD is `xhyve` for all other platforms the default is `qemu`. |
| `image_url` ||| string | URL a custom VM image that should be used in place of the default ones. |
| Input | Required | Default Value | Type | Description |
|-------------------------|----------|-------------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `run` ||| string | Runs command-line programs using the operating system's shell. This will be executed inside the virtual machine. |
| `operating_system` ||| string | The type of operating system to run the job on. See [Supported Platforms](#supported-platforms). |
| `architecture` || `x86-64` | string | The architecture of the operating system. See [Supported Platforms](#supported-platforms). |
| `version` ||| string | The version of the operating system to use. See [Supported Platforms](#supported-platforms). |
| `shell` || `default` | string | The shell to use to execute the commands. Defaults to the default shell for the given operating system. Allowed values are: `default`, `sh` and `bash` |
| `environment_variables` || `""` | string | A list of environment variables to forward to the virtual machine. The list should be separated with spaces. |
| `memory` || `6G` or `13G` | string | The amount of memory for the virtual machine. The default value is `6G` for Linux runners and `13G` for macOS runners. |
| `cpu_count` || `2` or `3` cores | integer | The number of CPU cores for the virtual machine. The default value is `2` for Linux runners and `3` for macOS runners. |
| `hypervisor` || `xhyve` or `qemu` | string | The hypervisor to use for running the virtual machine. For Linux runners the only valid value is `qemu`. For macOS runners the default for OpenBSD and FreeBSD is `xhyve` for all other platforms the default is `qemu`. |
| `image_url` ||| string | URL a custom VM image that should be used in place of the default ones. |
| `sync_files` || `true` | string | Specifies if the local files should be synchronized to the virtual machine and in which direction. Valid values are `true`, `false`, `runner-to-vm` and `vm-to-runner`. `true` synchronizes files in both directions. `false` disables file synchronization. |

All inputs are expected to be of the specified type. It's especially important
that you specify `version` as a string, using single or
Expand Down
32 changes: 29 additions & 3 deletions src/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as vmModule from '../vm'
import * as input from './input'
import * as shell from './shell'
import * as utility from '../utility'
import {SyncDirection} from './sync_direction'

export enum ImplementationKind {
qemu,
Expand Down Expand Up @@ -108,7 +109,7 @@ export class Action {
await vm.wait(120)
await this.operatingSystem.setupWorkDirectory(vm, this.workDirectory)
await this.syncFiles(
vm.ipAddress,
vm,
this.targetDiskName,
this.resourceDisk.diskPath,
...excludes
Expand Down Expand Up @@ -235,21 +236,32 @@ export class Action {
}

private async syncFiles(
ipAddress: string,
vm: vmModule.Vm,
...excludePaths: string[]
): Promise<void> {
if (!this.shouldSyncFiles) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await vm.execute(`mkdir -p '${process.env['GITHUB_WORKSPACE']!}'`, {
log: false
})

return
}

core.debug(`Syncing files to VM, excluding: ${excludePaths}`)
// prettier-ignore
await exec.exec('rsync', [
`-auzrtopg${this.syncVerboseFlag}`,
'--exclude', '_actions/cross-platform-actions/action',
...flatMap(excludePaths, p => ['--exclude', p]),
`${this.workDirectory}/`,
`runner@${ipAddress}:work`
`runner@${vm.ipAddress}:work`
])
}

private async syncBack(ipAddress: string): Promise<void> {
if (!this.shouldSyncBack) return

core.info('Syncing back files')
// prettier-ignore
await exec.exec('rsync', [
Expand Down Expand Up @@ -304,6 +316,20 @@ export class Action {
this.input.hypervisor
)
}

private get shouldSyncFiles(): boolean {
return (
this.input.syncFiles === SyncDirection.runner_to_vm ||
this.input.syncFiles === SyncDirection.both
)
}

private get shouldSyncBack(): boolean {
return (
this.input.syncFiles === SyncDirection.vm_to_runner ||
this.input.syncFiles === SyncDirection.both
)
}
}

export abstract class Implementation {
Expand Down
28 changes: 28 additions & 0 deletions src/action/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {Shell, toShell} from './shell'
import * as os from '../operating_systems/kind'
import {Host, host as defaultHost} from '../host'
import * as hypervisor from '../hypervisor'
import {
SyncDirection,
toSyncDirection,
validSyncDirections
} from './sync_direction'

export class Input {
private readonly host: Host
Expand All @@ -17,6 +22,7 @@ export class Input {
private memory_?: string
private cpuCount_?: number
private hypervisor_?: hypervisor.Hypervisor
private syncDirection_?: SyncDirection

constructor(host: Host = defaultHost) {
this.host = host
Expand Down Expand Up @@ -121,4 +127,26 @@ export class Input {
const hypervisorClass = hypervisor.toHypervisor(kind)
return (this.hypervisor_ = new hypervisorClass())
}

get syncFiles(): SyncDirection {
if (this.syncDirection_ !== undefined) return this.syncDirection_

const input = core.getInput('sync_files')
core.debug(`sync_files input: '${input}'`)
if (input === undefined || input === '')
return (this.syncDirection_ = SyncDirection.both)

const syncDirection = toSyncDirection(input)
core.debug(`syncDirection: '${syncDirection}'`)

if (syncDirection === undefined) {
const values = validSyncDirections.join(', ')

throw Error(
`Invalid sync-files: ${input}\nValid sync-files values are: ${values}`
)
}

return (this.syncDirection_ = syncDirection)
}
}

0 comments on commit de0e1f1

Please sign in to comment.