included secretName

This commit is contained in:
Nikola Jokic
2022-10-21 16:56:30 +02:00
parent 11de25a121
commit e33f331739
4 changed files with 76 additions and 34 deletions

View File

@@ -23,7 +23,8 @@ import {
localRegistryHost, localRegistryHost,
localRegistryPort, localRegistryPort,
remoteRegistryHost, remoteRegistryHost,
remoteRegistryHandle remoteRegistryHandle,
remoteRegistrySecretName
} from './settings' } from './settings'
export * from './settings' export * from './settings'
@@ -484,6 +485,7 @@ export async function containerBuild(
): Promise<string> { ): Promise<string> {
let kanikoRegistry = '' let kanikoRegistry = ''
let pullRegistry = '' let pullRegistry = ''
let secretName: string | undefined = undefined
if (localRegistryHost()) { if (localRegistryHost()) {
const host = `${localRegistryHost()}.${namespace()}.svc.cluster.local` const host = `${localRegistryHost()}.${namespace()}.svc.cluster.local`
const port = localRegistryPort() const port = localRegistryPort()
@@ -493,8 +495,10 @@ export async function containerBuild(
} else { } else {
kanikoRegistry = `${remoteRegistryHost()}/${remoteRegistryHandle()}/${generateBuildImage()}` kanikoRegistry = `${remoteRegistryHost()}/${remoteRegistryHandle()}/${generateBuildImage()}`
pullRegistry = kanikoRegistry pullRegistry = kanikoRegistry
secretName = remoteRegistrySecretName()
} }
const pod = kanikoPod(args.dockerfile, kanikoRegistry)
const pod = kanikoPod(args.dockerfile, kanikoRegistry, secretName)
if (!pod.metadata?.name) { if (!pod.metadata?.name) {
throw new Error('kaniko pod name is not set') throw new Error('kaniko pod name is not set')
} }

View File

@@ -17,7 +17,11 @@ function getKanikoName(): string {
)}-kaniko` )}-kaniko`
} }
export function kanikoPod(dockerfile: string, destination: string): k8s.V1Pod { export function kanikoPod(
dockerfile: string,
destination: string,
secretName?: string
): k8s.V1Pod {
const pod = new k8s.V1Pod() const pod = new k8s.V1Pod()
pod.apiVersion = 'v1' pod.apiVersion = 'v1'
pod.kind = 'Pod' pod.kind = 'Pod'
@@ -62,5 +66,30 @@ export function kanikoPod(dockerfile: string, destination: string): k8s.V1Pod {
persistentVolumeClaim: { claimName } persistentVolumeClaim: { claimName }
} }
] ]
if (secretName) {
const volumeName = 'docker-registry'
pod.spec.volumes.push({
name: volumeName,
projected: {
sources: [
{
secret: {
name: secretName,
items: [
{
key: '.dockerconfigjson',
path: 'config.json'
}
]
}
}
]
}
})
c.volumeMounts.push({
name: volumeName,
mountPath: '/kaniko/.docker/'
})
}
return pod return pod
} }

View File

@@ -61,3 +61,11 @@ export function remoteRegistryHandle(): string {
} }
throw new Error(`environment variable ${name} is not set`) throw new Error(`environment variable ${name} is not set`)
} }
export function remoteRegistrySecretName(): string {
const name = 'ACTIONS_RUNNER_CONTAINER_HOOKS_REMOTE_REGISTRY_SECRET_NAME'
if (process.env[name]) {
return process.env[name]
}
throw new Error(`environment variable ${name} is not set`)
}

View File

@@ -3,41 +3,41 @@ import { TestHelper } from './test-setup'
jest.useRealTimers() jest.useRealTimers()
// describe('Run container step with image', () => { describe('Run container step with image', () => {
// let testHelper: TestHelper let testHelper: TestHelper
// let runContainerStepData: any let runContainerStepData: any
// beforeEach(async () => { beforeEach(async () => {
// testHelper = new TestHelper() testHelper = new TestHelper()
// await testHelper.initialize() await testHelper.initialize()
// runContainerStepData = testHelper.getRunContainerStepDefinition() runContainerStepData = testHelper.getRunContainerStepDefinition()
// }) })
// afterEach(async () => { afterEach(async () => {
// await testHelper.cleanup() await testHelper.cleanup()
// }) })
// it('should not throw', async () => { it('should not throw', async () => {
// const exitCode = await runContainerStep(runContainerStepData.args) const exitCode = await runContainerStep(runContainerStepData.args)
// expect(exitCode).toBe(0) expect(exitCode).toBe(0)
// }) })
// it('should fail if the working directory does not exist', async () => { it('should fail if the working directory does not exist', async () => {
// runContainerStepData.args.workingDirectory = '/foo/bar' runContainerStepData.args.workingDirectory = '/foo/bar'
// await expect(runContainerStep(runContainerStepData.args)).rejects.toThrow() await expect(runContainerStep(runContainerStepData.args)).rejects.toThrow()
// }) })
// it('should shold have env variables available', async () => { it('should shold have env variables available', async () => {
// runContainerStepData.args.entryPoint = 'bash' runContainerStepData.args.entryPoint = 'bash'
// runContainerStepData.args.entryPointArgs = [ runContainerStepData.args.entryPointArgs = [
// '-c', '-c',
// "'if [[ -z $NODE_ENV ]]; then exit 1; fi'" "'if [[ -z $NODE_ENV ]]; then exit 1; fi'"
// ] ]
// await expect( await expect(
// runContainerStep(runContainerStepData.args) runContainerStep(runContainerStepData.args)
// ).resolves.not.toThrow() ).resolves.not.toThrow()
// }) })
// }) })
describe('run container step with docker build', () => { describe('run container step with docker build', () => {
let testHelper: TestHelper let testHelper: TestHelper
@@ -56,7 +56,8 @@ describe('run container step with docker build', () => {
const { registryName, localRegistryPort, nodePort } = const { registryName, localRegistryPort, nodePort } =
await testHelper.createContainerRegistry() await testHelper.createContainerRegistry()
process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_HOST = registryName process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_HOST =
registryName
process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_PORT = process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_PORT =
localRegistryPort.toString() localRegistryPort.toString()
process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_NODE_PORT = process.env.ACTIONS_RUNNER_CONTAINER_HOOKS_LOCAL_REGISTRY_NODE_PORT =