mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-16 17:56:44 +00:00
included generation of random handle/image
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import * as k8s from '@kubernetes/client-node'
|
import * as k8s from '@kubernetes/client-node'
|
||||||
import { RunContainerStepArgs } from 'hooklib'
|
import { RunContainerStepArgs } from 'hooklib'
|
||||||
import {
|
import {
|
||||||
@@ -8,7 +9,8 @@ import {
|
|||||||
getPodLogs,
|
getPodLogs,
|
||||||
getPodStatus,
|
getPodStatus,
|
||||||
waitForJobToComplete,
|
waitForJobToComplete,
|
||||||
waitForPodPhases
|
waitForPodPhases,
|
||||||
|
containerBuild
|
||||||
} from '../k8s'
|
} from '../k8s'
|
||||||
import {
|
import {
|
||||||
containerVolumes,
|
containerVolumes,
|
||||||
@@ -17,12 +19,14 @@ import {
|
|||||||
PodPhase,
|
PodPhase,
|
||||||
writeEntryPointScript
|
writeEntryPointScript
|
||||||
} from '../k8s/utils'
|
} from '../k8s/utils'
|
||||||
import { JOB_CONTAINER_NAME } from './constants'
|
import { getRunnerPodName, JOB_CONTAINER_NAME } from './constants'
|
||||||
|
|
||||||
export async function runContainerStep(
|
export async function runContainerStep(
|
||||||
stepContainer: RunContainerStepArgs
|
stepContainer: RunContainerStepArgs
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
if (stepContainer.dockerfile) {
|
if (stepContainer.dockerfile) {
|
||||||
|
const imagePath = `${generateRandomHandle()}/${generateBuildTag()}`
|
||||||
|
await containerBuild(stepContainer, imagePath)
|
||||||
throw new Error('Building container actions is not currently supported')
|
throw new Error('Building container actions is not currently supported')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,3 +112,16 @@ function createPodSpec(
|
|||||||
|
|
||||||
return podContainer
|
return podContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateBuildTag(): string {
|
||||||
|
return `${getRunnerPodName}:${uuidv4().substring(0, 6)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateRandomHandle(length = 10): string {
|
||||||
|
let handle = ''
|
||||||
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
handle += chars.charAt(Math.floor(Math.random() * length))
|
||||||
|
}
|
||||||
|
return handle
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as k8s from '@kubernetes/client-node'
|
import * as k8s from '@kubernetes/client-node'
|
||||||
import { ContainerInfo, Registry } from 'hooklib'
|
import { RunContainerStepArgs, ContainerInfo, Registry } from 'hooklib'
|
||||||
import * as stream from 'stream'
|
import * as stream from 'stream'
|
||||||
import {
|
import {
|
||||||
getJobPodName,
|
getJobPodName,
|
||||||
@@ -485,12 +485,15 @@ export async function isPodContainerAlpine(
|
|||||||
return isAlpine
|
return isAlpine
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function buildContainer(): Promise<void> {
|
export async function containerBuild(
|
||||||
|
args: RunContainerStepArgs,
|
||||||
|
imagePath: string
|
||||||
|
): Promise<void> {
|
||||||
const cm = registryConfigMap()
|
const cm = registryConfigMap()
|
||||||
const secret = registrySecret()
|
const secret = registrySecret()
|
||||||
const ss = registryStatefulSet()
|
const ss = registryStatefulSet()
|
||||||
const svc = registryService()
|
const svc = registryService()
|
||||||
const pod = kanikoPod()
|
const pod = kanikoPod(args.workingDirectory, imagePath)
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
k8sApi.createNamespacedConfigMap(namespace(), cm),
|
k8sApi.createNamespacedConfigMap(namespace(), cm),
|
||||||
k8sApi.createNamespacedSecret(namespace(), secret)
|
k8sApi.createNamespacedSecret(namespace(), secret)
|
||||||
|
|||||||
@@ -173,7 +173,10 @@ export function registryService(): k8s.V1Service {
|
|||||||
return svc
|
return svc
|
||||||
}
|
}
|
||||||
|
|
||||||
export function kanikoPod(): k8s.V1Pod {
|
export function kanikoPod(
|
||||||
|
workingDirectory: string, // git://github.com/<handle>/<repo>
|
||||||
|
imagePath: string // <handle>/<image>:<tag>
|
||||||
|
): k8s.V1Pod {
|
||||||
const pod = new k8s.V1Pod()
|
const pod = new k8s.V1Pod()
|
||||||
pod.apiVersion = 'v1'
|
pod.apiVersion = 'v1'
|
||||||
pod.kind = 'Pod'
|
pod.kind = 'Pod'
|
||||||
@@ -193,8 +196,8 @@ export function kanikoPod(): k8s.V1Pod {
|
|||||||
]
|
]
|
||||||
c.args = [
|
c.args = [
|
||||||
'--dockerfile=Dockerfile',
|
'--dockerfile=Dockerfile',
|
||||||
'--context=git://github.com/nikola-jokic/dockeraction.git',
|
`--context=${workingDirectory}`,
|
||||||
'--destination=docker-registry.default.svc.cluster.local:5000/test/app:1.0'
|
`--destination=docker-registry.default.svc.cluster.local:5000/${imagePath}`
|
||||||
]
|
]
|
||||||
spec.containers = [c]
|
spec.containers = [c]
|
||||||
spec.dnsPolicy = 'ClusterFirst'
|
spec.dnsPolicy = 'ClusterFirst'
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { buildContainer } from '../src/k8s'
|
import { containerBuild } from '../src/k8s'
|
||||||
import { TestHelper } from './test-setup'
|
|
||||||
|
|
||||||
jest.useRealTimers()
|
jest.useRealTimers()
|
||||||
|
|
||||||
@@ -9,6 +8,13 @@ describe('container build', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should finish without throwing an exception', async () => {
|
it('should finish without throwing an exception', async () => {
|
||||||
await expect(buildContainer()).resolves.not.toThrow()
|
await expect(
|
||||||
|
containerBuild(
|
||||||
|
{
|
||||||
|
workingDirectory: 'git://github.com/nikola-jokic/dockeraction.git'
|
||||||
|
},
|
||||||
|
'randhandle/randimg:123123'
|
||||||
|
)
|
||||||
|
).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user