mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-15 17:26:44 +00:00
refactored tests and added docker build test, repaired state.network
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
ServiceContainerInfo,
|
||||
StepContainerInfo
|
||||
} from 'hooklib/lib'
|
||||
import path from 'path'
|
||||
import { env } from 'process'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { runDockerCommand, RunDockerCommandOptions } from '../utils'
|
||||
@@ -146,17 +145,43 @@ export async function containerBuild(
|
||||
args: RunContainerStepArgs,
|
||||
tag: string
|
||||
): Promise<void> {
|
||||
const context = path.dirname(`${env.GITHUB_WORKSPACE}/${args.dockerfile}`)
|
||||
if (!args.dockerfile) {
|
||||
throw new Error('Container build expets args.dockerfile to be set')
|
||||
}
|
||||
|
||||
const dockerArgs: string[] = ['build']
|
||||
dockerArgs.push('-t', tag)
|
||||
dockerArgs.push('-f', `${env.GITHUB_WORKSPACE}/${args.dockerfile}`)
|
||||
dockerArgs.push(context)
|
||||
dockerArgs.push('-f', args.dockerfile)
|
||||
dockerArgs.push(getBuildContext(args.dockerfile))
|
||||
// TODO: figure out build working directory
|
||||
await runDockerCommand(dockerArgs, {
|
||||
workingDir: args['buildWorkingDirectory']
|
||||
workingDir: getWorkingDir(args.dockerfile)
|
||||
})
|
||||
}
|
||||
|
||||
function getBuildContext(dockerfilePath: string): string {
|
||||
const pathSplit = dockerfilePath.split('/')
|
||||
pathSplit.splice(pathSplit.length - 1)
|
||||
return pathSplit.join('/')
|
||||
}
|
||||
|
||||
function getWorkingDir(dockerfilePath: string): string {
|
||||
const workspace = env.GITHUB_WORKSPACE as string
|
||||
let workingDir = workspace
|
||||
if (!dockerfilePath?.includes(workspace)) {
|
||||
// This is container action
|
||||
const pathSplit = dockerfilePath.split('/')
|
||||
const actionIndex = pathSplit?.findIndex(d => d === '_actions')
|
||||
if (actionIndex) {
|
||||
const actionSubdirectoryDepth = 3 // handle + repo + [branch | tag]
|
||||
pathSplit.splice(actionIndex + actionSubdirectoryDepth + 1)
|
||||
workingDir = pathSplit.join('/')
|
||||
}
|
||||
}
|
||||
|
||||
return workingDir
|
||||
}
|
||||
|
||||
export async function containerLogs(id: string): Promise<void> {
|
||||
const dockerArgs: string[] = ['logs']
|
||||
dockerArgs.push('--details')
|
||||
@@ -330,7 +355,7 @@ export async function containerExecStep(
|
||||
export async function containerRun(
|
||||
args: RunContainerStepArgs,
|
||||
name: string,
|
||||
network: string
|
||||
network?: string
|
||||
): Promise<void> {
|
||||
if (!args.image) {
|
||||
throw new Error('expected image to be set')
|
||||
@@ -340,7 +365,9 @@ export async function containerRun(
|
||||
dockerArgs.push('--name', name)
|
||||
dockerArgs.push(`--workdir=${args.workingDirectory}`)
|
||||
dockerArgs.push(`--label=${getRunnerLabel()}`)
|
||||
dockerArgs.push(`--network=${network}`)
|
||||
if (network) {
|
||||
dockerArgs.push(`--network=${network}`)
|
||||
}
|
||||
|
||||
if (args.createOptions) {
|
||||
dockerArgs.push(...args.createOptions.split(' '))
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { RunContainerStepArgs } from 'hooklib/lib'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import {
|
||||
containerBuild,
|
||||
registryLogin,
|
||||
registryLogout,
|
||||
containerPull,
|
||||
containerRun
|
||||
containerRun,
|
||||
registryLogin,
|
||||
registryLogout
|
||||
} from '../dockerCommands'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import * as core from '@actions/core'
|
||||
import { RunContainerStepArgs } from 'hooklib/lib'
|
||||
import { getRunnerLabel } from '../dockerCommands/constants'
|
||||
|
||||
export async function runContainerStep(
|
||||
@@ -15,23 +14,23 @@ export async function runContainerStep(
|
||||
state
|
||||
): Promise<void> {
|
||||
const tag = generateBuildTag() // for docker build
|
||||
if (!args.image) {
|
||||
core.error('expected an image')
|
||||
} else {
|
||||
if (args.dockerfile) {
|
||||
await containerBuild(args, tag)
|
||||
args.image = tag
|
||||
} else {
|
||||
const configLocation = await registryLogin(args)
|
||||
try {
|
||||
await containerPull(args.image, configLocation)
|
||||
} finally {
|
||||
await registryLogout(configLocation)
|
||||
}
|
||||
if (args.image) {
|
||||
const configLocation = await registryLogin(args)
|
||||
try {
|
||||
await containerPull(args.image, configLocation)
|
||||
} finally {
|
||||
await registryLogout(configLocation)
|
||||
}
|
||||
} else if (args.dockerfile) {
|
||||
await containerBuild(args, tag)
|
||||
args.image = tag
|
||||
} else {
|
||||
throw new Error(
|
||||
'run container step should have image or dockerfile fields specified'
|
||||
)
|
||||
}
|
||||
// container will get pruned at the end of the job based on the label, no need to cleanup here
|
||||
await containerRun(args, tag.split(':')[1], state.network)
|
||||
await containerRun(args, tag.split(':')[1], state?.network)
|
||||
}
|
||||
|
||||
function generateBuildTag(): string {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
runContainerStep,
|
||||
runScriptStep
|
||||
} from './hooks'
|
||||
import { checkEnvironment } from './utils'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
const input = await getInputFromStdin()
|
||||
@@ -23,6 +24,7 @@ async function run(): Promise<void> {
|
||||
const state = input['state']
|
||||
|
||||
try {
|
||||
checkEnvironment()
|
||||
switch (command) {
|
||||
case Command.PrepareJob:
|
||||
await prepareJob(args as PrepareJobArgs, responseFile)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
/* eslint-disable import/no-commonjs */
|
||||
import * as core from '@actions/core'
|
||||
import { env } from 'process'
|
||||
// Import this way otherwise typescript has errors
|
||||
const exec = require('@actions/exec')
|
||||
|
||||
@@ -42,6 +43,12 @@ export function sanitize(val: string): string {
|
||||
return newNameBuilder.join('')
|
||||
}
|
||||
|
||||
export function checkEnvironment(): void {
|
||||
if (!env.GITHUB_WORKSPACE) {
|
||||
throw new Error('GITHUB_WORKSPACE is not set')
|
||||
}
|
||||
}
|
||||
|
||||
// isAlpha accepts single character and checks if
|
||||
// that character is [a-zA-Z]
|
||||
function isAlpha(val: string): boolean {
|
||||
|
||||
Reference in New Issue
Block a user