2023-04-15 17:22:48 -05:00
|
|
|
def main [
|
2023-04-15 18:17:58 -05:00
|
|
|
input: string # tar.gz file containing container image to be pushed to repository
|
|
|
|
...tags: string # Tags to be added to pushed container image
|
2023-04-15 18:54:36 -05:00
|
|
|
--registry: string = "" # container registry
|
|
|
|
--repository: string = "" # container repository
|
2023-04-15 18:17:58 -05:00
|
|
|
--no-latest-tag # Don't add "latest" tag to list of tags
|
2023-04-15 17:22:48 -05:00
|
|
|
--no-drone-tag # Don't add tag calculated from DRONE_BUILD_NUMBER and DRONE_COMMIT_SHA
|
2023-08-17 15:09:54 -05:00
|
|
|
--no-github-tag # Don't add tag calculated from GITHUB_RUN_NUMBER and GITHUB_SHA
|
2023-04-15 17:22:48 -05:00
|
|
|
] {
|
|
|
|
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 (
|
2023-04-15 23:51:46 -05:00
|
|
|
(not $no_github_tag)
|
2023-04-15 17:22:48 -05:00
|
|
|
and
|
2023-04-15 23:51:46 -05:00
|
|
|
(not ($env | get -i GITHUB_RUN_NUMBER | is-empty))
|
2023-04-15 17:22:48 -05:00
|
|
|
and
|
2023-04-15 23:51:46 -05:00
|
|
|
(not ($env | get -i GITHUB_SHA | is-empty))
|
2023-04-15 17:22:48 -05:00
|
|
|
) {
|
2023-04-15 23:51:46 -05:00
|
|
|
$tags | append $"($env.GITHUB_RUN_NUMBER)-($env.GITHUB_SHA | str substring 0..8)"
|
2023-04-15 17:22:48 -05:00
|
|
|
} else {
|
|
|
|
$tags
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = if (
|
2023-04-15 23:51:46 -05:00
|
|
|
(not $no_drone_tag)
|
2023-04-15 17:22:48 -05:00
|
|
|
and
|
2023-04-15 23:51:46 -05:00
|
|
|
(not ($env | get -i DRONE_BUILD_NUMBER | is-empty))
|
2023-04-15 17:22:48 -05:00
|
|
|
and
|
2023-04-15 23:51:46 -05:00
|
|
|
(not ($env | get -i DRONE_COMMIT_SHA | is-empty))
|
2023-04-15 17:22:48 -05:00
|
|
|
) {
|
|
|
|
$tags | append $"($env.DRONE_BUILD_NUMBER)-($env.DRONE_COMMIT_SHA | str substring 0..8)"
|
|
|
|
} else {
|
|
|
|
$tags
|
|
|
|
}
|
|
|
|
|
2023-04-15 18:54:36 -05:00
|
|
|
let auth = {username: null, password: null}
|
|
|
|
|
|
|
|
let auth = (
|
|
|
|
if (
|
2023-08-21 11:37:31 -05:00
|
|
|
(not ($env | get -i USERNAME | is-empty))
|
2023-04-15 18:54:36 -05:00
|
|
|
and
|
2023-08-21 11:37:31 -05:00
|
|
|
(not ($env | get -i PASSWORD | is-empty))
|
2023-04-15 18:54:36 -05:00
|
|
|
) {
|
2023-08-21 11:37:31 -05:00
|
|
|
print "Got username and password from USERNAME and PASSWORD"
|
2023-08-21 13:40:21 -05:00
|
|
|
{username: $env.USERNAME, password: $env.PASSWORD}
|
2023-04-15 18:54:36 -05:00
|
|
|
} else if (
|
|
|
|
(not ($env | get -i PLUGIN_USERNAME | is-empty))
|
|
|
|
and
|
|
|
|
(not ($env | get -i PLUGIN_PASSWORD | is-empty))
|
|
|
|
) {
|
2023-08-21 11:37:31 -05:00
|
|
|
print "Got username and password from PLUGIN_USERNAME and PLUGIN_PASSWORD"
|
2023-08-21 13:40:21 -05:00
|
|
|
{username: $env.PLUGIN_USERNAME, password: $env.PLUGIN_PASSWORD}
|
2023-08-17 15:09:54 -05:00
|
|
|
} else if (
|
2023-08-21 11:37:31 -05:00
|
|
|
(not ($env | get -i GITHUB_ACTOR | is-empty))
|
2023-08-17 15:09:54 -05:00
|
|
|
and
|
2023-08-21 11:37:31 -05:00
|
|
|
(not ($env | get -i GITHUB_TOKEN | is-empty))
|
2023-08-17 15:09:54 -05:00
|
|
|
) {
|
2023-08-21 11:37:31 -05:00
|
|
|
print "Got username and password from GITHUB_ACTOR and GITHUB_TOKEN"
|
2023-08-21 13:40:21 -05:00
|
|
|
{username: $env.GITHUB_ACTOR, password: $env.GITHUB_TOKEN}
|
2023-04-15 18:54:36 -05:00
|
|
|
} else {
|
|
|
|
print "Unable to determine authentication parameters!"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
let registry = (
|
|
|
|
if ($registry | is-empty) {
|
|
|
|
if not ($env | get -i PLUGIN_REGISTRY | is-empty) {
|
|
|
|
$env.PLUGIN_REGISTRY
|
2023-08-17 15:09:54 -05:00
|
|
|
} else if not ($env | get -i REGISTRY | is-empty) {
|
|
|
|
$env.REGISTRY
|
2023-04-15 18:54:36 -05:00
|
|
|
} else {
|
|
|
|
print "No registry specified!"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$registry
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-04-15 19:04:24 -05:00
|
|
|
let repository = (
|
2023-04-15 18:54:36 -05:00
|
|
|
if ($repository | is-empty) {
|
2023-04-15 19:04:24 -05:00
|
|
|
if not ($env | get -i PLUGIN_REPOSITORY | is-empty) {
|
|
|
|
$env.PLUGIN_REPOSITORY
|
2023-08-17 15:09:54 -05:00
|
|
|
} else if not ($env | get -i REPOSITORY | is-empty) {
|
|
|
|
$env.REPOSITORY
|
2023-04-15 18:54:36 -05:00
|
|
|
} else {
|
|
|
|
print "No repository specified!"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$repository
|
|
|
|
}
|
|
|
|
)
|
2023-04-15 17:22:48 -05:00
|
|
|
|
2023-08-27 14:30:16 -05:00
|
|
|
alias podman = ^podman --log-level debug
|
2023-04-15 18:17:58 -05:00
|
|
|
|
2023-04-15 18:54:36 -05:00
|
|
|
$auth.password | podman login --username $auth.username --password-stdin $registry
|
2023-04-16 01:14:30 -05:00
|
|
|
|
|
|
|
let load_result = (do { podman load --input $input } | complete)
|
2023-04-15 18:17:58 -05:00
|
|
|
if $load_result.exit_code != 0 {
|
|
|
|
print $load_result.stderr
|
|
|
|
exit 1
|
|
|
|
}
|
2023-04-16 01:14:30 -05:00
|
|
|
|
2023-04-15 18:54:36 -05:00
|
|
|
let old_image = ($load_result.stdout | str trim | parse "Loaded image: {image}" | get 0.image)
|
2023-04-15 18:17:58 -05:00
|
|
|
|
2023-04-15 17:22:48 -05:00
|
|
|
$tags | each {
|
|
|
|
|tag|
|
2023-04-15 18:54:36 -05:00
|
|
|
let new_image = $"($registry)/($repository):($tag)"
|
2023-04-15 18:17:58 -05:00
|
|
|
let tag_result = (do { podman tag $old_image $new_image } | complete)
|
|
|
|
if $tag_result.exit_code != 0 {
|
|
|
|
print $tag_result.stderr
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
let push_result = (do { podman push $new_image } | complete)
|
|
|
|
if $push_result.exit_code != 0 {
|
|
|
|
print $push_result.stderr
|
|
|
|
exit 1
|
|
|
|
}
|
2023-04-16 09:21:46 -05:00
|
|
|
print $"Pushed ($new_image)"
|
2023-04-15 17:22:48 -05:00
|
|
|
}
|
2023-04-16 01:14:30 -05:00
|
|
|
|
2023-04-15 18:54:36 -05:00
|
|
|
podman logout $registry
|
2023-04-15 17:22:48 -05:00
|
|
|
}
|