Skip to content

Commit

Permalink
Move environment variable backup (#37)
Browse files Browse the repository at this point in the history
This PR moves the environment variable backup/restore process to the
entrypoint `local-action` script. Previously these would be reset before
long-running actions would complete.
  • Loading branch information
ncalteen authored Feb 23, 2024
2 parents 0672ca7 + 76fa345 commit 0cdee07
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 57 deletions.
8 changes: 0 additions & 8 deletions __tests__/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from 'fs'
import { ResetCoreMetadata } from '../src/stubs/core-stubs'
import { ResetEnvMetadata } from '../src/stubs/env-stubs'

let envBackup: { [key: string]: string | undefined } = process.env

let fs_existsSyncSpy: jest.SpyInstance
let fs_readFileSyncSpy: jest.SpyInstance

Expand All @@ -17,9 +15,6 @@ describe('Bootstrap', () => {
// Reset metadata
ResetEnvMetadata()
ResetCoreMetadata()

// Back up environment variables
envBackup = process.env
})

afterEach(() => {
Expand All @@ -28,9 +23,6 @@ describe('Bootstrap', () => {

// Reset module imports
jest.resetModules()

// Restore environment variables
process.env = envBackup
})

it('Does nothing if no target action path is provided', async () => {
Expand Down
8 changes: 0 additions & 8 deletions __tests__/commands/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const summary_writeSpy: jest.SpyInstance = jest
.spyOn(summary, 'write')
.mockImplementation()

let envBackup: { [key: string]: string | undefined } = process.env

describe('Command: run', () => {
beforeAll(() => {
// Prevent output during tests
Expand All @@ -24,17 +22,11 @@ describe('Command: run', () => {
// Reset metadata
ResetEnvMetadata()
ResetCoreMetadata()

// Back up environment variables
envBackup = process.env
})

afterEach(() => {
// Reset all spies
jest.resetAllMocks()

// Restore environment variables
process.env = envBackup
})

describe('TypeScript', () => {
Expand Down
8 changes: 0 additions & 8 deletions __tests__/stubs/core-stubs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { EnvMeta, ResetEnvMetadata } from '../../src/stubs/env-stubs'
import type { CoreMetadata } from '../../src/types'
import path from 'path'

let envBackup: { [key: string]: string | undefined } = process.env

/** Empty CoreMetadata Object */
const empty: CoreMetadata = {
exitCode: 0,
Expand Down Expand Up @@ -66,17 +64,11 @@ describe('Core', () => {
// Reset metadata
ResetEnvMetadata()
ResetCoreMetadata()

// Back up environment variables
envBackup = process.env
})

afterEach(() => {
// Reset all spies
jest.resetAllMocks()

// Restore environment variables
process.env = envBackup
})

describe('CoreMeta', () => {
Expand Down
10 changes: 2 additions & 8 deletions __tests__/stubs/env-stubs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ const empty: EnvMetadata = {
dotenvFile: '',
entrypoint: '',
env: {},
envBackup: {},
inputs: {},
outputs: {},
path: '',
pathBackup: ''
path: ''
}

describe('Env', () => {
Expand Down Expand Up @@ -45,11 +43,9 @@ describe('Env', () => {
EnvMeta.dotenvFile = '.env'
EnvMeta.entrypoint = 'index.ts'
EnvMeta.env = { TEST: 'test' }
EnvMeta.envBackup = { TEST: 'testBackup' }
EnvMeta.inputs = { input: { description: 'test input' } }
EnvMeta.outputs = { output: { description: 'test output' } }
EnvMeta.path = '/usr/bin'
EnvMeta.pathBackup = '/usr/bin/backup'

// Verify the updated metadata
expect(EnvMeta).toMatchObject({
Expand All @@ -58,11 +54,9 @@ describe('Env', () => {
dotenvFile: '.env',
entrypoint: 'index.ts',
env: { TEST: 'test' },
envBackup: { TEST: 'testBackup' },
inputs: { input: { description: 'test input' } },
outputs: { output: { description: 'test output' } },
path: '/usr/bin',
pathBackup: '/usr/bin/backup'
path: '/usr/bin'
})

// Reset the metadata
Expand Down
8 changes: 8 additions & 0 deletions bin/local-action
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
const path = require('path')
const { execSync } = require('child_process')

// Back up the environment
const envBackup = { ...process.env }
const pathBackup = process.env.PATH

/**
* This script is used to run the local action. It sets the NODE_OPTIONS
* environment variable to require the bootstrap file, which sets up the
Expand Down Expand Up @@ -35,4 +39,8 @@ try {
execSync(command, { cwd: packagePath, stdio: 'inherit' })
} catch (error) {
process.exit(error.status)
} finally {
// Restore the environment
process.env = { ...envBackup }
process.env.PATH = pathBackup
}
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@github/local-action",
"description": "Local Debugging for GitHub Actions",
"version": "1.2.0",
"version": "1.3.0",
"author": "Nick Alteen <ncalteen@github.com>",
"private": false,
"homepage": "https://github.com/github/local-action",
Expand Down
8 changes: 0 additions & 8 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export async function action(): Promise<void> {
const path = await import('path')
const YAML = await import('yaml')

// Back up the environment
EnvMeta.envBackup = { ...process.env }
EnvMeta.pathBackup = process.env.PATH

CoreMeta.colors = {
cyan: /* istanbul ignore next */ (msg: string) =>
console.log(chalk.cyan(msg)),
Expand Down Expand Up @@ -141,8 +137,4 @@ export async function action(): Promise<void> {
warning
}
})

// Reset environment and PATH
process.env = EnvMeta.envBackup
process.env.PATH = EnvMeta.pathBackup
}
6 changes: 1 addition & 5 deletions src/stubs/env-stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export const EnvMeta: EnvMetadata = {
dotenvFile: '',
entrypoint: '',
env: {},
envBackup: {},
inputs: {},
outputs: {},
path: '',
pathBackup: ''
path: ''
}

/**
Expand All @@ -27,9 +25,7 @@ export function ResetEnvMetadata(): void {
EnvMeta.dotenvFile = ''
EnvMeta.entrypoint = ''
EnvMeta.env = {}
EnvMeta.envBackup = {}
EnvMeta.inputs = {}
EnvMeta.outputs = {}
EnvMeta.path = ''
EnvMeta.pathBackup = ''
}
9 changes: 0 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@ export type EnvMetadata = {
TZ?: string | undefined
}

/** Backup of environment variables prior to action invocation */
envBackup: {
[key: string]: string | undefined
TZ?: string | undefined
}

/** System path */
path: string

/** Backup of system path prior to action invocation */
pathBackup: string | undefined

/** Inputs defined in `action.yml` */
inputs: { [key: string]: Input }

Expand Down

0 comments on commit 0cdee07

Please sign in to comment.