@@ -250,6 +250,45 @@ export const deploy = async (teamId: number, githubUrl: string) => {
250250 } ) ;
251251
252252 try {
253+ // Check if team has an active project and stop/remove existing container
254+ const existingProject = await prisma . project . findFirst ( {
255+ where : {
256+ teamId,
257+ status : 'running' ,
258+ } ,
259+ } ) ;
260+
261+ if ( existingProject && existingProject . containerId ) {
262+ // First try-catch: Stop the existing container
263+ try {
264+ const existingContainer = docker . getContainer ( existingProject . containerId ) ;
265+ await existingContainer . stop ( ) ;
266+ console . log ( `Stopped existing container: ${ existingProject . containerId } ` ) ;
267+ } catch ( stopError ) {
268+ console . log ( `Failed to stop container ${ existingProject . containerId } :` , stopError ) ;
269+ // Continue even if stop fails - container might already be stopped
270+ }
271+
272+ // Second try-catch: Remove the existing container
273+ try {
274+ const existingContainer = docker . getContainer ( existingProject . containerId ) ;
275+ await existingContainer . remove ( ) ;
276+ console . log ( `Removed existing container: ${ existingProject . containerId } ` ) ;
277+
278+ // Update the existing project status
279+ await prisma . project . update ( {
280+ where : { id : existingProject . id } ,
281+ data : {
282+ status : 'stopped' ,
283+ stoppedAt : new Date ( ) ,
284+ } ,
285+ } ) ;
286+ } catch ( removeError ) {
287+ console . log ( `Failed to remove container ${ existingProject . containerId } :` , removeError ) ;
288+ // Continue even if remove fails - we'll create a new container anyway
289+ }
290+ }
291+
253292 // Ensure the projects network exists
254293 await ensureProjectsNetwork ( ) ;
255294
0 commit comments