From 4e0a91235abe77e814ffac2d940e20ac89970bfa Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sat, 15 Apr 2023 17:22:48 -0500 Subject: [PATCH] use nushell script for pushing container image --- flake.nix | 21 +++++------- push-container.nu | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 push-container.nu diff --git a/flake.nix b/flake.nix index 9c4cc60..2a46b90 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,7 @@ pkgs.gnused pkgs.nix pkgs.nodejs-16_x + pkgs.nushell pkgs.podman ]; @@ -365,18 +366,14 @@ apps = { push-container = let - script = pkgs.writeScript "push-container" '' - set | grep -i plugin - set | grep -i DRONE - ls -l "''${1}" - echo -n "''${PLUGIN_PASSWORD}" | podman login --username "''${PLUGIN_USERNAME}" --password-stdin "''${PLUGIN_REGISTRY}" - image=''$(podman load --input "''${1}" | sed -n -e "s/Loaded image:.\\(.*\\)/\\1/p") - podman images - podman tag "''${image}" "''${PLUGIN_REGISTRY}/''${PLUGIN_REPOSITORY}:''${DRONE_BUILD_NUMBER}-''${DRONE_COMMIT_SHA:0:8}" - podman tag "''${image}" "''${PLUGIN_REGISTRY}/''${PLUGIN_REPOSITORY}:latest" - podman images - podman logout "''${PLUGIN_REGISTRY}" - ''; + script = pkgs.writeTextFile + { + name = "push-container"; + text = '' + #!${pkgs.nushell}/bin/nu + '' + (builtins.readFile ./push-container.nu); + executable = true; + }; in { type = "app"; diff --git a/push-container.nu b/push-container.nu new file mode 100644 index 0000000..edb9c28 --- /dev/null +++ b/push-container.nu @@ -0,0 +1,87 @@ +def main [ + input: string # tar.gz file containing container to be pushed to repository + ...tags: string # Tags to be added to pushed container + --no-latest-tag # Don't add latest tag + --no-drone-tag # Don't add tag calculated from DRONE_BUILD_NUMBER and DRONE_COMMIT_SHA + --no-github-tag # Don't add tag calculated from GItHUB_RUN_NUMBER and GITHUB_SHA +] { + env + print $input + print $tags + print $no_latest_tag + print $no_drone_tag + print $no_github_tag + + if not ($input | path exists) { + print $"($input) does not exist!" + exit 1 + } + + let tags = if not ($env | get -i PLUGIN_TAGS | is-empty) { + $tags | append ($env.PLUGIN_TAGS | split row ',' | str trim) + } else { + $tags + } + + let tags = if (not $no_latest_tag) { + $tags | append "latest" + } else { + $tags + } + + let tags = if ( + (not $no_drone_tag) + and + (not ($env | get -i DRONE_BUILD_NUMBER | is-empty)) + and + (not ($env | get -i DRONE_COMMIT_SHA | is-empty)) + ) { + $tags | append $"($env.DRONE_BUILD_NUMBER)-($env.DRONE_COMMIT_SHA | str substring 0..8)" + } else { + $tags + } + + let tags = if ( + (not $no_github_tag) + and + (not ($env | get -i GITHUB_RUN_NUMBER | is-empty)) + and + (not ($env | get -i GITHUB_SHA | is-empty)) + ) { + $tags | append $"($env.DRONE_BUILD_NUMBER)-($env.DRONE_COMMIT_SHA | str substring 0..8)" + } else { + $tags + } + + print $tags + + if ($env | get -i PLUGIN_PASSWORD | is-empty) { + print "No password specified!" + exit 1 + } + if ($env | get -i PLUGIN_USERNAME | is-empty) { + print "No username specified!" + exit 1 + } + if ($env | get -i PLUGIN_REGISTRY | is-empty) { + print "No registry specified!" + exit 1 + } + if ($env | get -i PLUGIN_REPOSITORY | is-empty) { + print "No repositiory specified!" + exit 1 + } + + $env.PLUGIN_PASSWORD | podman login --username $env.PLUGIN_USERNAME --password-stdin $env.PLUGIN_REGISTRY + let old_image = (podman load --input $input | parse "Loaded image: {image}" | get 0.image) + print $old_image + podman images + $tags | each { + |tag| + let new_image = $"($env.PLUGIN_REGISTRY)/($env.PLUGIN_REPOSITORY):($tag)" + podman tag $old_image $new_image + podman push $new_image + } + podman images + podman logout "''${PLUGIN_REGISTRY}" +}