mirror of
https://github.com/redhat-actions/push-to-registry.git
synced 2025-04-19 22:26:18 +02:00
Add better error msg
Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
parent
c83fdad66a
commit
23bd822301
4 changed files with 108 additions and 31 deletions
9
.github/workflows/verify-push.yaml
vendored
9
.github/workflows/verify-push.yaml
vendored
|
@ -23,7 +23,14 @@ jobs:
|
|||
FROM busybox
|
||||
RUN echo "hello world"
|
||||
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
|
||||
- name: Push To Quay
|
||||
|
|
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
126
src/index.ts
126
src/index.ts
|
@ -10,6 +10,11 @@ interface ExecResult {
|
|||
stderr: string;
|
||||
}
|
||||
|
||||
interface ImageStorageCheckResult {
|
||||
readonly foundTags: string[];
|
||||
readonly missingTags: string[];
|
||||
}
|
||||
|
||||
let podmanPath: string | undefined;
|
||||
|
||||
// boolean value to check if pushed image is from Docker image storage
|
||||
|
@ -43,39 +48,78 @@ async function run(): Promise<void> {
|
|||
const registryPathList: string[] = [];
|
||||
|
||||
// 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
|
||||
// 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
|
||||
if (!isPresentInDocker && !isPresentInPodman) {
|
||||
throw new Error(`All the tags of ${imageToPush} not found in Podman local storage, or Docker local storage.`);
|
||||
const dockerFoundTags: string[] = dockerImageStorageCheckResult.foundTags;
|
||||
const dockerMissingTags: string[] = dockerImageStorageCheckResult.missingTags;
|
||||
|
||||
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();
|
||||
if (!isPodmanImageLatest) {
|
||||
core.warning(`The version of ${imageToPush} in the Docker image storage is more recent `
|
||||
+ `than the version in the Podman image storage. The image(s) from the Docker image storage `
|
||||
+ `will be pushed.`);
|
||||
core.warning(
|
||||
`The version of ${imageToPush} in the Docker image storage is more recent `
|
||||
+ `than the version in the Podman image storage. The image(s) from the Docker image storage `
|
||||
+ `will be pushed.`
|
||||
);
|
||||
imageToPush = `${dockerBaseUrl}/${imageToPush}`;
|
||||
isImageFromDocker = true;
|
||||
}
|
||||
else {
|
||||
core.warning(`The version of ${imageToPush} in the Podman image storage is more recent `
|
||||
+ `than the version in the Docker image storage. The image(s) from the Podman image `
|
||||
+ `storage will be pushed.`);
|
||||
core.warning(
|
||||
`The version of ${imageToPush} in the Podman image storage is more recent `
|
||||
+ `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}`;
|
||||
core.info(`${imageToPush} was found in the Docker image storage, but not in the Podman `
|
||||
+ `image storage. The image(s) will be pulled into Podman image storage, pushed, and then `
|
||||
+ `removed from the Podman image storage.`);
|
||||
core.info(
|
||||
`${imageToPush} was found in the Docker image storage, but not in the Podman `
|
||||
+ `image storage. The image(s) will be pulled into Podman image storage, pushed, and then `
|
||||
+ `removed from the Podman image storage.`
|
||||
);
|
||||
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}`;
|
||||
if (username) {
|
||||
|
@ -135,41 +179,67 @@ async function run(): Promise<void> {
|
|||
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;
|
||||
const foundTags: string[] = [];
|
||||
const missingTags: string[] = [];
|
||||
try {
|
||||
for (const tag of tagsList) {
|
||||
imageWithTag = `${imageToPush}:${tag}`;
|
||||
await execute(await getPodmanPath(), [ "pull", `docker-daemon:${imageWithTag}` ]);
|
||||
core.info(`${imageWithTag} found in Docker image storage`);
|
||||
const commandResult: ExecResult = await execute(
|
||||
await getPodmanPath(),
|
||||
[ "pull", `docker-daemon:${imageWithTag}` ],
|
||||
{ ignoreReturnCode: true, failOnStdErr: false }
|
||||
);
|
||||
if (!commandResult.exitCode) {
|
||||
foundTags.push(tag);
|
||||
}
|
||||
else {
|
||||
missingTags.push(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
core.info(`${imageWithTag} not found in Docker image storage`);
|
||||
return false;
|
||||
core.debug(err);
|
||||
}
|
||||
|
||||
return true;
|
||||
return {
|
||||
foundTags,
|
||||
missingTags,
|
||||
};
|
||||
}
|
||||
|
||||
async function checkImageInPodman(): Promise<boolean> {
|
||||
async function checkImageInPodman(): Promise<ImageStorageCheckResult> {
|
||||
// check if images exist in Podman's storage
|
||||
core.info(`Checking if ${imageToPush} with tag(s) ${tagsList.join(", ")} is present in Podman image storage`);
|
||||
let imageWithTag;
|
||||
const foundTags: string[] = [];
|
||||
const missingTags: string[] = [];
|
||||
try {
|
||||
for (const tag of tagsList) {
|
||||
imageWithTag = `${imageToPush}:${tag}`;
|
||||
await execute(await getPodmanPath(), [ "image", "exists", imageWithTag ]);
|
||||
core.info(`${imageWithTag} found in Podman image storage`);
|
||||
const commandResult: ExecResult = await execute(
|
||||
await getPodmanPath(),
|
||||
[ "image", "exists", imageWithTag ],
|
||||
{ ignoreReturnCode: true }
|
||||
);
|
||||
if (!commandResult.exitCode) {
|
||||
foundTags.push(tag);
|
||||
}
|
||||
else {
|
||||
missingTags.push(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
core.info(`${imageWithTag} not found in Podman image storage`);
|
||||
core.debug(err);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return {
|
||||
foundTags,
|
||||
missingTags,
|
||||
};
|
||||
}
|
||||
|
||||
async function isPodmanLocalImageLatest(): Promise<boolean> {
|
||||
|
|
Loading…
Add table
Reference in a new issue