Add better error msg

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
divyansh42 2021-01-29 12:20:28 +05:30
parent c83fdad66a
commit 23bd822301
4 changed files with 108 additions and 31 deletions

View file

@ -23,7 +23,14 @@ jobs:
FROM busybox FROM busybox
RUN echo "hello world" RUN echo "hello world"
EOF EOF
docker tag ${{ env.IMAGE_NAME }}:v1 ${{ env.IMAGE_NAME }}:${{ github.sha }}
- name: Build Image using Podman
run: |
podman build -t ${{ env.IMAGE_NAME }}:v1 -<<EOF
FROM busybox
RUN echo "hello world"
EOF
podman tag ${{ env.IMAGE_NAME }}:v1 ${{ env.IMAGE_NAME }}:${{ github.sha }}
# Push the image to image registry # Push the image to image registry
- name: Push To Quay - name: Push To Quay

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -10,6 +10,11 @@ interface ExecResult {
stderr: string; stderr: string;
} }
interface ImageStorageCheckResult {
readonly foundTags: string[];
readonly missingTags: string[];
}
let podmanPath: string | undefined; let podmanPath: string | undefined;
// boolean value to check if pushed image is from Docker image storage // boolean value to check if pushed image is from Docker image storage
@ -43,39 +48,78 @@ async function run(): Promise<void> {
const registryPathList: string[] = []; const registryPathList: string[] = [];
// check if image with all the required tags exist in Podman image storage // check if image with all the required tags exist in Podman image storage
const isPresentInPodman: boolean = await checkImageInPodman(); const podmanImageStorageCheckResult: ImageStorageCheckResult = await checkImageInPodman();
const podmanFoundTags: string[] = podmanImageStorageCheckResult.foundTags;
const podmanMissingTags: string[] = podmanImageStorageCheckResult.missingTags;
if (podmanFoundTags.length > 0) {
core.info(`Tag(s) ${podmanFoundTags.join(", ")} of ${imageToPush} found in Podman image storage`);
}
if (podmanMissingTags.length > 0) {
core.warning(`Tag(s) ${podmanMissingTags.join(", ")} of ${imageToPush} not found in Podman image storage`);
}
// check if image with all the required tags exist in Docker image storage // check if image with all the required tags exist in Docker image storage
// and if exist pull the image with all the tags to Podman // and if exist pull the image with all the tags to Podman
const isPresentInDocker: boolean = await pullImageFromDocker(); const dockerImageStorageCheckResult: ImageStorageCheckResult = await pullImageFromDocker();
// failing if image with any of the tag is not found in Docker as well as Podman const dockerFoundTags: string[] = dockerImageStorageCheckResult.foundTags;
if (!isPresentInDocker && !isPresentInPodman) { const dockerMissingTags: string[] = dockerImageStorageCheckResult.missingTags;
throw new Error(`All the tags of ${imageToPush} not found in Podman local storage, or Docker local storage.`);
if (dockerFoundTags.length > 0) {
core.info(`Tag(s) ${dockerFoundTags.join(", ")} of ${imageToPush} found in Docker image storage`);
} }
if (isPresentInPodman && isPresentInDocker) { if (dockerMissingTags.length > 0) {
core.warning(`Tag(s) ${dockerMissingTags.join(", ")} of ${imageToPush} not found in Docker image storage`);
}
// failing if image with any of the tag is not found in Docker as well as Podman
if (podmanMissingTags.length > 0 && dockerMissingTags.length > 0) {
throw new Error(
`Few tags of ${imageToPush} not found in Podman image storage, or Docker image storage.`
);
}
const allTagsinPodman: boolean = podmanFoundTags.length === tagsList.length;
const allTagsinDocker: boolean = dockerFoundTags.length === tagsList.length;
if (allTagsinPodman && allTagsinDocker) {
const isPodmanImageLatest = await isPodmanLocalImageLatest(); const isPodmanImageLatest = await isPodmanLocalImageLatest();
if (!isPodmanImageLatest) { if (!isPodmanImageLatest) {
core.warning(`The version of ${imageToPush} in the Docker image storage is more recent ` core.warning(
+ `than the version in the Podman image storage. The image(s) from the Docker image storage ` `The version of ${imageToPush} in the Docker image storage is more recent `
+ `will be pushed.`); + `than the version in the Podman image storage. The image(s) from the Docker image storage `
+ `will be pushed.`
);
imageToPush = `${dockerBaseUrl}/${imageToPush}`; imageToPush = `${dockerBaseUrl}/${imageToPush}`;
isImageFromDocker = true; isImageFromDocker = true;
} }
else { else {
core.warning(`The version of ${imageToPush} in the Podman image storage is more recent ` core.warning(
+ `than the version in the Docker image storage. The image(s) from the Podman image ` `The version of ${imageToPush} in the Podman image storage is more recent `
+ `storage will be pushed.`); + `than the version in the Docker image storage. The image(s) from the Podman image `
+ `storage will be pushed.`
);
} }
} }
else if (isPresentInDocker) { else if (allTagsinDocker) {
imageToPush = `${dockerBaseUrl}/${imageToPush}`; imageToPush = `${dockerBaseUrl}/${imageToPush}`;
core.info(`${imageToPush} was found in the Docker image storage, but not in the Podman ` core.info(
+ `image storage. The image(s) will be pulled into Podman image storage, pushed, and then ` `${imageToPush} was found in the Docker image storage, but not in the Podman `
+ `removed from the Podman image storage.`); + `image storage. The image(s) will be pulled into Podman image storage, pushed, and then `
+ `removed from the Podman image storage.`
);
isImageFromDocker = true; isImageFromDocker = true;
} }
else {
core.info(
`${imageToPush} was found in the Podman image storage, but not in the Docker `
+ `image storage. The image(s) will be pushed from Podman image storage.`
);
}
let pushMsg = `Pushing ${imageToPush} with tags ${tagsList.join(", ")} to ${registry}`; let pushMsg = `Pushing ${imageToPush} with tags ${tagsList.join(", ")} to ${registry}`;
if (username) { if (username) {
@ -135,41 +179,67 @@ async function run(): Promise<void> {
core.setOutput("registry-paths", JSON.stringify(registryPathList)); core.setOutput("registry-paths", JSON.stringify(registryPathList));
} }
async function pullImageFromDocker(): Promise<boolean> { async function pullImageFromDocker(): Promise<ImageStorageCheckResult> {
core.info(`Checking if ${imageToPush} with tag(s) ${tagsList.join(", ")} is present in Docker image storage`);
let imageWithTag; let imageWithTag;
const foundTags: string[] = [];
const missingTags: string[] = [];
try { try {
for (const tag of tagsList) { for (const tag of tagsList) {
imageWithTag = `${imageToPush}:${tag}`; imageWithTag = `${imageToPush}:${tag}`;
await execute(await getPodmanPath(), [ "pull", `docker-daemon:${imageWithTag}` ]); const commandResult: ExecResult = await execute(
core.info(`${imageWithTag} found in Docker image storage`); await getPodmanPath(),
[ "pull", `docker-daemon:${imageWithTag}` ],
{ ignoreReturnCode: true, failOnStdErr: false }
);
if (!commandResult.exitCode) {
foundTags.push(tag);
}
else {
missingTags.push(tag);
}
} }
} }
catch (err) { catch (err) {
core.info(`${imageWithTag} not found in Docker image storage`); core.debug(err);
return false;
} }
return true; return {
foundTags,
missingTags,
};
} }
async function checkImageInPodman(): Promise<boolean> { async function checkImageInPodman(): Promise<ImageStorageCheckResult> {
// check if images exist in Podman's storage // check if images exist in Podman's storage
core.info(`Checking if ${imageToPush} with tag(s) ${tagsList.join(", ")} is present in Podman image storage`); core.info(`Checking if ${imageToPush} with tag(s) ${tagsList.join(", ")} is present in Podman image storage`);
let imageWithTag; let imageWithTag;
const foundTags: string[] = [];
const missingTags: string[] = [];
try { try {
for (const tag of tagsList) { for (const tag of tagsList) {
imageWithTag = `${imageToPush}:${tag}`; imageWithTag = `${imageToPush}:${tag}`;
await execute(await getPodmanPath(), [ "image", "exists", imageWithTag ]); const commandResult: ExecResult = await execute(
core.info(`${imageWithTag} found in Podman image storage`); await getPodmanPath(),
[ "image", "exists", imageWithTag ],
{ ignoreReturnCode: true }
);
if (!commandResult.exitCode) {
foundTags.push(tag);
}
else {
missingTags.push(tag);
}
} }
} }
catch (err) { catch (err) {
core.info(`${imageWithTag} not found in Podman image storage`);
core.debug(err); core.debug(err);
return false;
} }
return true; return {
foundTags,
missingTags,
};
} }
async function isPodmanLocalImageLatest(): Promise<boolean> { async function isPodmanLocalImageLatest(): Promise<boolean> {