All files index.ts

62.96% Statements 17/27
0% Branches 0/4
33.33% Functions 1/3
62.96% Lines 17/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x     1x 1x 1x         1x         1x 1x 1x     1x 1x 1x   1x 1x 1x 1x   1x                                                 1x  
import {exec} from 'child_process'
import STS from 'aws-sdk/clients/sts'
 
import {DOCKERLESS_EVENT} from '@utils/constants'
import {toId} from '@utils/string-utils'
import {GlobalDockerlessState} from '@src/dockerless-state'
import {ContainerValue} from '@utils/types/dockerless-config'
import {TypedEmitter} from '@utils/types'
import DockerlessEvents from '@utils/types/dockerless-events'
 
const pushedImage = async (
	eventEmitter: TypedEmitter<DockerlessEvents>,
	stsClient: STS,
	container: ContainerValue
) => {
	eventEmitter.emit(DOCKERLESS_EVENT.pushImageStarted)
	console.log('ECR Login..')
	const localImageTagName = `${toId(GlobalDockerlessState.config.serviceName)}-${toId(
		container.containerName
	)}-${GlobalDockerlessState.stage}`
	const dockerAppPath = container.dockerFilePath.replace('/Dockerfile', '')
	const awsAccountId = (await stsClient.getCallerIdentity().promise()).Account
	const remoteImageTagName = `${awsAccountId}.dkr.ecr.${process.env.AWS_DEFAULT_REGION}.amazonaws.com/${localImageTagName}:latest`
 
	const ecrGetLoginCommand = `aws ecr get-login-password --region ${process.env.AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${awsAccountId}.dkr.ecr.${process.env.AWS_DEFAULT_REGION}.amazonaws.com`
	const dockerBuildCommand = `docker build -t ${localImageTagName} ${dockerAppPath}`
	const remoteTagCommand = `docker tag ${localImageTagName}:latest ${remoteImageTagName}`
	const dockerPushCommand = `docker push ${remoteImageTagName}`
 
	exec(`${ecrGetLoginCommand}`, (err, stdout) => {
		if (err) {
			eventEmitter.emit(DOCKERLESS_EVENT.pushImageFailure)
 
			return console.log(`Error ECR Login`, err)
		}
 
		const ecrLoginCommand = stdout.replace('\n', '')
 
		exec(
			`${ecrLoginCommand} && ${dockerBuildCommand} && ${remoteTagCommand} && ${dockerPushCommand} `,
			(err, stdout) => {
				if (err) {
					eventEmitter.emit(DOCKERLESS_EVENT.pushImageFailure)
 
					return console.log(`Error pushing image`, err)
				}
 
				console.log('out', stdout)
				eventEmitter.emit(DOCKERLESS_EVENT.pushImageSuccess)
			}
		)
	})
}
 
export default pushedImage