Compare commits
	
		
			4 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 4854d27a19 | |||
| 75a0e8a7d8 | |||
| 43eb19f772 | |||
| dc2665d250 | 
							
								
								
									
										12
									
								
								changelog.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								changelog.md
									
									
									
									
									
								
							| @@ -1,5 +1,17 @@ | |||||||
| # Changelog | # Changelog | ||||||
|  |  | ||||||
|  | ## 2024-11-17 - 4.1.37 - fix(docker) | ||||||
|  | Enhanced base image extraction logic from Dockerfile | ||||||
|  |  | ||||||
|  | - Improved dockerBaseImage to accurately extract base images considering ARG variables. | ||||||
|  | - Added support for parsing Dockerfile content without external libraries. | ||||||
|  | - Enhanced error handling for missing FROM instructions. | ||||||
|  |  | ||||||
|  | ## 2024-11-17 - 4.1.36 - fix(docker) | ||||||
|  | Improve logging for Dockerfile build order with base image details. | ||||||
|  |  | ||||||
|  | - Enhance logging in Dockerfile sorting process to include base image information. | ||||||
|  |  | ||||||
| ## 2024-11-17 - 4.1.35 - fix(docker) | ## 2024-11-17 - 4.1.35 - fix(docker) | ||||||
| Fix Dockerfile dependency sorting and enhance environment variable handling for GitHub repos | Fix Dockerfile dependency sorting and enhance environment variable handling for GitHub repos | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@ship.zone/npmci", |   "name": "@ship.zone/npmci", | ||||||
|   "version": "4.1.35", |   "version": "4.1.37", | ||||||
|   "private": false, |   "private": false, | ||||||
|   "description": "A tool to streamline Node.js and Docker workflows within CI environments, particularly GitLab CI, providing various CI/CD utilities.", |   "description": "A tool to streamline Node.js and Docker workflows within CI environments, particularly GitLab CI, providing various CI/CD utilities.", | ||||||
|   "main": "dist_ts/index.js", |   "main": "dist_ts/index.js", | ||||||
|   | |||||||
| @@ -3,6 +3,6 @@ | |||||||
|  */ |  */ | ||||||
| export const commitinfo = { | export const commitinfo = { | ||||||
|   name: '@ship.zone/npmci', |   name: '@ship.zone/npmci', | ||||||
|   version: '4.1.35', |   version: '4.1.37', | ||||||
|   description: 'A tool to streamline Node.js and Docker workflows within CI environments, particularly GitLab CI, providing various CI/CD utilities.' |   description: 'A tool to streamline Node.js and Docker workflows within CI environments, particularly GitLab CI, providing various CI/CD utilities.' | ||||||
| } | } | ||||||
|   | |||||||
| @@ -102,7 +102,11 @@ export class Dockerfile { | |||||||
|  |  | ||||||
|     // Log the sorted order |     // Log the sorted order | ||||||
|     sortedDockerfiles.forEach((dockerfile, index) => { |     sortedDockerfiles.forEach((dockerfile, index) => { | ||||||
|       logger.log('info', `Build order ${index + 1}: ${dockerfile.cleanTag}`); |       logger.log( | ||||||
|  |         'info', | ||||||
|  |         `Build order ${index + 1}: ${dockerfile.cleanTag} | ||||||
|  |       with base image ${dockerfile.baseImage}` | ||||||
|  |       ); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     return sortedDockerfiles; |     return sortedDockerfiles; | ||||||
| @@ -170,12 +174,62 @@ export class Dockerfile { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * returns the docker base image for a Dockerfile |    * Extracts the base image from a Dockerfile content without using external libraries. | ||||||
|  |    * @param dockerfileContentArg The content of the Dockerfile as a string. | ||||||
|  |    * @returns The base image specified in the first FROM instruction. | ||||||
|    */ |    */ | ||||||
|   public static dockerBaseImage(dockerfileContentArg: string): string { |   public static dockerBaseImage(dockerfileContentArg: string): string { | ||||||
|     const baseImageRegex = /FROM\s([a-zA-z0-9\/\-\:]*)\n?/; |     const lines = dockerfileContentArg.split(/\r?\n/); | ||||||
|     const regexResultArray = baseImageRegex.exec(dockerfileContentArg); |     const args: { [key: string]: string } = {}; | ||||||
|     return regexResultArray[1]; |  | ||||||
|  |     for (const line of lines) { | ||||||
|  |       const trimmedLine = line.trim(); | ||||||
|  |  | ||||||
|  |       // Skip empty lines and comments | ||||||
|  |       if (trimmedLine === '' || trimmedLine.startsWith('#')) { | ||||||
|  |         continue; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Match ARG instructions | ||||||
|  |       const argMatch = trimmedLine.match(/^ARG\s+([^\s=]+)(?:=(.*))?$/i); | ||||||
|  |       if (argMatch) { | ||||||
|  |         const argName = argMatch[1]; | ||||||
|  |         const argValue = argMatch[2] !== undefined ? argMatch[2] : process.env[argName] || ''; | ||||||
|  |         args[argName] = argValue; | ||||||
|  |         continue; | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       // Match FROM instructions | ||||||
|  |       const fromMatch = trimmedLine.match(/^FROM\s+(.+?)(?:\s+AS\s+[^\s]+)?$/i); | ||||||
|  |       if (fromMatch) { | ||||||
|  |         let baseImage = fromMatch[1].trim(); | ||||||
|  |  | ||||||
|  |         // Substitute variables in the base image name | ||||||
|  |         baseImage = Dockerfile.substituteVariables(baseImage, args); | ||||||
|  |  | ||||||
|  |         return baseImage; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     throw new Error('No FROM instruction found in Dockerfile'); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * Substitutes variables in a string, supporting default values like ${VAR:-default}. | ||||||
|  |    * @param str The string containing variables. | ||||||
|  |    * @param vars The object containing variable values. | ||||||
|  |    * @returns The string with variables substituted. | ||||||
|  |    */ | ||||||
|  |   private static substituteVariables(str: string, vars: { [key: string]: string }): string { | ||||||
|  |     return str.replace(/\${([^}:]+)(:-([^}]+))?}/g, (_, varName, __, defaultValue) => { | ||||||
|  |       if (vars[varName] !== undefined) { | ||||||
|  |         return vars[varName]; | ||||||
|  |       } else if (defaultValue !== undefined) { | ||||||
|  |         return defaultValue; | ||||||
|  |       } else { | ||||||
|  |         return ''; | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user