Skip to content

Commit

Permalink
src: make getDockerfilePath return the full path to the dockerfile (#…
Browse files Browse the repository at this point in the history
…64)

Previously we were just returning the path to the dir containing the dockerfile
in most cases.
  • Loading branch information
aayushshah15 authored Dec 9, 2024
1 parent a415504 commit 7b86428
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core';
import * as handlebars from 'handlebars';
import * as fs from 'fs';

import {Build} from '@docker/actions-toolkit/lib/buildx/build';
import {Context} from '@docker/actions-toolkit/lib/context';
Expand Down Expand Up @@ -89,15 +90,30 @@ export function getDockerfilePath(inputs: Inputs): string | null {
try {
const context = inputs.context || Context.gitContext();
const normalizedContext = path.normalize(context);
let dockerfilePath: string;

if (inputs.file) {
const normalizedFile = path.normalize(inputs.file);
return normalizedFile.startsWith(normalizedContext) ? normalizedFile : path.join(normalizedContext, normalizedFile);
dockerfilePath = normalizedFile.startsWith(normalizedContext) ? normalizedFile : path.join(normalizedContext, normalizedFile);
} else if (inputs['dockerfile']) {
const normalizedDockerfile = path.normalize(inputs['dockerfile']);
return normalizedDockerfile.startsWith(normalizedContext) ? normalizedDockerfile : path.join(normalizedContext, normalizedDockerfile);
dockerfilePath = normalizedDockerfile.startsWith(normalizedContext) ? normalizedDockerfile : path.join(normalizedContext, normalizedDockerfile);
} else {
return normalizedContext;
// Default to Dockerfile in the context directory
dockerfilePath = path.join(normalizedContext, 'Dockerfile');
}

// Verify the file exists
try {
const stats = fs.statSync(dockerfilePath);
if (!stats.isFile()) {
core.warning(`Path exists but is not a file: ${dockerfilePath}`);
return null;
}
return dockerfilePath;
} catch (statError) {
core.warning(`Dockerfile not found at path: ${dockerfilePath}`);
return null;
}
} catch (error) {
core.warning(`Error getting dockerfile path: ${(error as Error).message}`);
Expand Down

0 comments on commit 7b86428

Please sign in to comment.