Skip to content

Instantly share code, notes, and snippets.

@clintharrison
Created November 6, 2019 17:34
Show Gist options
  • Save clintharrison/ba684d77234a6b6786ef9608d386f56e to your computer and use it in GitHub Desktop.
Save clintharrison/ba684d77234a6b6786ef9608d386f56e to your computer and use it in GitHub Desktop.
exports_files(["pusher_template.sh"])
# example use
artifactory_file_push(
name = "publish",
src = "//app/foo:some_tar",
filename = "$(VERSION).tar",
registry_url = "https://artifactory.internal/some-artifactory-repo",
repository = "team-foo/app-bar",
)
#!/usr/bin/env bash
set -eu
readonly src_tar="TEMPLATED_src_tar"
readonly dest_filename="TEMPLATED_dest_filename"
readonly registry_url="TEMPLATED_registry_url"
readonly repository="TEMPLATED_repository"
if [[ -z "${ARTIFACTORY_USER}" || -z "${ARTIFACTORY_PASS}" ]]; then
echo 'Error: $ARTIFACTORY_USER and $ARTIFACTORY_PASS must be set to upload artifacts to Artifactory.'
exit 1
fi
curl --user "${ARTIFACTORY_USER}:${ARTIFACTORY_PASS}" -X PUT "${registry_url}/${repository}/${dest_filename}" --data-binary @"${src_tar}"
"""
This module contains a rule to push a tar file directly to Artifactory.
"""
def _impl(ctx):
"""Implementation of artifactory_file_push.
Writes a bash script template that specifically uploads the specified source
to a destination on artifactory.
"""
pusher = ctx.actions.declare_file("%s.pusher.sh" % ctx.attr.name)
ctx.actions.expand_template(
output = pusher,
template = ctx.file._pusher_template,
substitutions = {
"TEMPLATED_src_tar": ctx.file.src.short_path,
"TEMPLATED_registry_url": ctx.attr.registry_url,
"TEMPLATED_repository": ctx.attr.repository,
"TEMPLATED_dest_filename": ctx.expand_make_variables("filename", ctx.attr.filename, {}),
},
is_executable = True,
)
return [DefaultInfo(
files = depset([pusher]),
executable = pusher,
runfiles = ctx.runfiles([ctx.file.src]),
)]
artifactory_file_push = rule(
implementation = _impl,
attrs = {
"src": attr.label(
mandatory = True,
allow_single_file = True,
doc = "Target to upload to destination; e.g. the output of an archive target.",
),
"registry_url": attr.string(
mandatory = True,
doc = "Url to artifactory registry to which to push the file.",
),
"repository": attr.string(
mandatory = True,
doc = "Literal path within registry at which to store file.",
),
"filename": attr.string(
mandatory = True,
doc = "Templated name of destination file.",
),
"_pusher_template": attr.label(
default = "//artifactory_file_push:pusher_template.sh",
allow_single_file = True,
),
},
executable = True,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment