diff --git a/Dockerfile b/Dockerfile index fb10bb3..c0c35a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM python:3.7-alpine RUN apk add --no-cache img --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing # Setup other deps -RUN apk add --no-cache git skopeo docker +RUN apk add --no-cache git skopeo docker cargo # Define the working directory WORKDIR taskboot diff --git a/taskboot/cargo.py b/taskboot/cargo.py new file mode 100644 index 0000000..03cceb7 --- /dev/null +++ b/taskboot/cargo.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import logging +import subprocess + +from taskboot.config import Configuration + +logger = logging.getLogger(__name__) + + +def cargo_publish(target, args): + """ + Publish a crate on crates.io + """ + + # Load config from file/secret + config = Configuration(args) + assert config.has_cargo_auth(), "Missing Cargo authentication" + + # Build the package to publish on crates.io + subprocess.run(["cargo", "publish", "--dry-run"], check=True) + + # Publish the crate on crates.io + # stdout and stderr are captured to avoid leaking the token + proc = subprocess.run( + ["cargo", "publish", "--token", config.cargo["token"]], capture_output=True, + ) + + if proc.returncode != 0: + raise Exception("Failed to publish the crate on crates.io") diff --git a/taskboot/cli.py b/taskboot/cli.py index 35ff9d8..5cff02d 100644 --- a/taskboot/cli.py +++ b/taskboot/cli.py @@ -13,6 +13,7 @@ from taskboot.build import build_compose from taskboot.build import build_hook from taskboot.build import build_image +from taskboot.cargo import cargo_publish from taskboot.github import github_release from taskboot.push import heroku_release from taskboot.push import push_artifacts @@ -301,6 +302,12 @@ def main(): ) github_release_cmd.set_defaults(func=github_release) + # Publish on crates.io + cargo_publish_cmd = commands.add_parser( + "cargo-publish", help="Publish a crate on crates.io" + ) + cargo_publish_cmd.set_defaults(func=cargo_publish) + # Always load the target args = parser.parse_args() target = Target(args) diff --git a/taskboot/config.py b/taskboot/config.py index 8b0bc48..8cb2e0b 100644 --- a/taskboot/config.py +++ b/taskboot/config.py @@ -87,3 +87,9 @@ def has_github_auth(self): if github is None: return False return "token" in github + + def has_cargo_auth(self): + cargo = self.config.get("cargo") + if cargo is None: + return False + return "token" in cargo