2021-03-24 21:11:14 +00:00
const yaml = require ( 'js-yaml' )
2021-03-30 21:35:22 +00:00
const fs = require ( 'fs' ) . promises
2021-03-24 21:11:14 +00:00
const { execShellAsync } = require ( '../../common' )
const { docker } = require ( '../../docker' )
const { saveAppLog } = require ( '../../logging' )
const { deleteSameDeployments } = require ( '../cleanup' )
2021-04-15 20:40:44 +00:00
module . exports = async function ( configuration , imageChanged ) {
2021-04-19 07:46:05 +00:00
const generateEnvs = { }
for ( const secret of configuration . publish . secrets ) {
generateEnvs [ secret . name ] = secret . value
}
const containerName = configuration . build . container . name
2021-03-30 21:35:22 +00:00
2021-04-19 07:46:05 +00:00
// Only save SHA256 of it in the configuration label
const baseServiceConfiguration = configuration . baseServiceConfiguration
delete configuration . baseServiceConfiguration
2021-03-30 21:35:22 +00:00
2021-04-19 07:46:05 +00:00
const stack = {
version : '3.8' ,
services : {
[ containerName ] : {
image : ` ${ configuration . build . container . name } : ${ configuration . build . container . tag } ` ,
networks : [ ` ${ docker . network } ` ] ,
environment : generateEnvs ,
deploy : {
... baseServiceConfiguration ,
labels : [
'managedBy=coolify' ,
'type=application' ,
'configuration=' + JSON . stringify ( configuration ) ,
'traefik.enable=true' ,
'traefik.http.services.' +
2021-03-24 21:11:14 +00:00
configuration . build . container . name +
` .loadbalancer.server.port= ${ configuration . publish . port } ` ,
2021-04-19 07:46:05 +00:00
'traefik.http.routers.' +
2021-03-24 21:11:14 +00:00
configuration . build . container . name +
'.entrypoints=websecure' ,
2021-04-19 07:46:05 +00:00
'traefik.http.routers.' +
2021-03-24 21:11:14 +00:00
configuration . build . container . name +
'.rule=Host(`' +
configuration . publish . domain +
'`) && PathPrefix(`' +
configuration . publish . path +
'`)' ,
2021-04-19 07:46:05 +00:00
'traefik.http.routers.' +
2021-03-24 21:11:14 +00:00
configuration . build . container . name +
'.tls.certresolver=letsencrypt' ,
2021-04-19 07:46:05 +00:00
'traefik.http.routers.' +
2021-03-24 21:11:14 +00:00
configuration . build . container . name +
'.middlewares=global-compress'
2021-04-19 07:46:05 +00:00
]
2021-03-24 21:11:14 +00:00
}
}
2021-04-19 07:46:05 +00:00
} ,
networks : {
[ ` ${ docker . network } ` ] : {
external : true
}
2021-03-24 21:11:14 +00:00
}
2021-04-19 07:46:05 +00:00
}
await saveAppLog ( '### Publishing.' , configuration )
await fs . writeFile ( ` ${ configuration . general . workdir } /stack.yml ` , yaml . dump ( stack ) )
if ( imageChanged ) {
// console.log('image changed')
await execShellAsync ( ` docker service update --image ${ configuration . build . container . name } : ${ configuration . build . container . tag } ${ configuration . build . container . name } _ ${ configuration . build . container . name } ` )
} else {
// console.log('new deployment or force deployment or config changed')
await deleteSameDeployments ( configuration )
await execShellAsync (
2021-03-24 21:11:14 +00:00
` cat ${ configuration . general . workdir } /stack.yml | docker stack deploy --prune -c - ${ containerName } `
2021-04-19 07:46:05 +00:00
)
2021-03-24 21:11:14 +00:00
}
2021-04-19 07:46:05 +00:00
await saveAppLog ( '### Published done!' , configuration )
2021-03-24 21:11:14 +00:00
}