Skip to content

Commit

Permalink
feat: Support unpublished dependencies in publish-crates-cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz committed Mar 21, 2024
1 parent af84b1a commit fe25448
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 68 deletions.
23 changes: 15 additions & 8 deletions .github/workflows/release-crates-cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Release crates (Cargo)
on:
workflow_call:
inputs:
repos:
repo:
type: string
required: true
live-run:
Expand All @@ -12,12 +12,15 @@ on:
branch:
type: string
required: true
inter-deps-pattern:
unpublished-deps-patterns:
type: string
required: true
required: false
unpublished-deps-repos:
type: string
required: false
workflow_dispatch:
inputs:
repos:
repo:
type: string
required: true
live-run:
Expand All @@ -26,19 +29,23 @@ on:
branch:
type: string
required: true
inter-deps-pattern:
unpublished-deps-patterns:
type: string
required: true
required: false
unpublished-deps-repos:
type: string
required: false

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: eclipse-zenoh/ci/publish-crates-cargo@main
with:
repos: ${{ inputs.repos }}
repo: ${{ inputs.repo }}
live-run: ${{ inputs.live-run }}
branch: ${{ inputs.branch }}
inter-deps-pattern: ${{ inputs.inter-deps-pattern }}
unpublished-deps-pattern: ${{ inputs.unpublished-deps-pattern }}
unpublished-deps-repos: ${{ inputs.unpublished-deps-repos }}
github-token: ${{ secrets.BOT_TOKEN_WORKFLOW }}
crates-io-token: ${{ secrets.CRATES_IO_TOKEN }}
56 changes: 29 additions & 27 deletions dist/publish-crates-cargo-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82304,39 +82304,37 @@ async function spawn() {


function setup() {
const liveRun = lib_core.getInput("live-run");
const liveRun = lib_core.getBooleanInput("live-run", { required: true });
const branch = lib_core.getInput("branch", { required: true });
const repos = lib_core.getInput("repos", { required: true });
const repo = lib_core.getInput("repo", { required: true });
const githubToken = lib_core.getInput("github-token", { required: true });
const interDepsPattern = lib_core.getInput("inter-deps-pattern", { required: true });
const unpublishedDepsPatterns = lib_core.getInput("unpublished-deps-patterns", { required: true });
const unpublishedDepsRepos = lib_core.getInput("unpublished-deps-repos", { required: true });
const cratesIoToken = lib_core.getInput("crates-io-token", { required: true });
return {
liveRun: liveRun == "" ? false : lib_core.getBooleanInput("live-run"),
liveRun,
branch,
repos: repos.split("\n"),
repo,
githubToken,
interDepsRegExp: interDepsPattern == "" ? undefined : new RegExp(interDepsPattern),
unpublishedDepsRegExp: new RegExp(unpublishedDepsPatterns.split("\n").join("|")),
unpublishedDepsRepos: unpublishedDepsRepos.split("\n"),
cratesIoToken,
};
}
async function main(input) {
let registry;
try {
registry = await spawn();
for (const repo of input.repos) {
lib_core.startGroup(`Publishing ${repo} to estuary`);
clone(repo, input);
await publishToEstuary(repo, input, registry);
lib_core.endGroup();
for (const repo of input.unpublishedDepsRepos) {
await publishToEstuary(input, repo, registry, /^$/);
}
await publishToEstuary(input, input.repo, registry, input.unpublishedDepsRegExp);
await deleteRepos(input);
if (input.liveRun) {
for (const repo of input.repos) {
lib_core.startGroup(`Publishing ${repo} to crates.io`);
clone(repo, input);
publishToCratesIo(repo, input);
lib_core.endGroup();
for (const repo of input.unpublishedDepsRepos) {
publishToCratesIo(input, repo);
}
publishToCratesIo(input, input.repo);
}
await cleanup(input, registry);
}
Expand All @@ -82360,37 +82358,41 @@ async function cleanup(input, registry) {
}
await deleteRepos(input);
}
function clone(repo, input) {
function clone(input, repo) {
const remote = `https://${input.githubToken}@github.com/${repo}.git`;
sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`);
}
async function deleteRepos(input) {
for (const repo of input.repos) {
lib_core.info(`Deleting repository ${repoPath(repo)}`);
lib_core.info(`Deleting repository clone ${repoPath(input.repo)}`);
await (0,promises_namespaceObject.rm)(repoPath(input.repo), { recursive: true, force: true });
for (const repo of input.unpublishedDepsRepos) {
lib_core.info(`Deleting repository clone ${repoPath(repo)}`);
await (0,promises_namespaceObject.rm)(repoPath(repo), { recursive: true, force: true });
}
}
function repoPath(repo) {
return repo.split("/").at(1);
}
async function publishToEstuary(repo, input, registry) {
const path = repoPath(repo);
async function publishToEstuary(input, repo, registry, registryDepsRegExp) {
clone(input, repo);
const path = repoPath(input.repo);
await configRegistry(path, registry.name, registry.index);
await setRegistry(path, input.interDepsRegExp, registry.name);
await setRegistry(path, registryDepsRegExp, registry.name);
const env = {
CARGO_REGISTRY_DEFAULT: registry.name,
[`CARGO_REGISTRIES_${registry.name.toUpperCase()}_TOKEN`]: registry.token,
};
publish(repo, env);
publish(path, env);
}
function publishToCratesIo(repo, input) {
function publishToCratesIo(input, repo) {
clone(input, repo);
const path = repoPath(input.repo);
const env = {
CARGO_REGISTRY_TOKEN: input.cratesIoToken,
};
publish(repo, env);
publish(path, env);
}
function publish(repo, env) {
const path = repoPath(repo);
function publish(path, env) {
const options = {
env,
cwd: path,
Expand Down
10 changes: 6 additions & 4 deletions publish-crates-cargo/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ name: Publish crates (Cargo)

inputs:
live-run:
required: false
required: true
branch:
required: true
repos:
repo:
required: true
github-token:
required: true
crates-io-token:
required: true
inter-deps-pattern:
required: true
unpublished-deps-patterns:
required: false
unpublished-deps-repos:
required: false

runs:
using: node20
Expand Down
72 changes: 43 additions & 29 deletions src/publish-crates-cargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@ import { sh } from "./command";
export type Input = {
liveRun: boolean;
branch: string;
repos: string[];
repo: string;
githubToken: string;
interDepsRegExp: RegExp;
unpublishedDepsRegExp: RegExp;
unpublishedDepsRepos: string[];
cratesIoToken?: string;
};

export function setup(): Input {
const liveRun = core.getInput("live-run");
const liveRun = core.getBooleanInput("live-run", { required: true });
const branch = core.getInput("branch", { required: true });
const repos = core.getInput("repos", { required: true });
const repo = core.getInput("repo", { required: true });
const githubToken = core.getInput("github-token", { required: true });
const interDepsPattern = core.getInput("inter-deps-pattern", { required: true });
const unpublishedDepsPatterns = core.getInput("unpublished-deps-patterns", { required: true });
const unpublishedDepsRepos = core.getInput("unpublished-deps-repos", { required: true });
const cratesIoToken = core.getInput("crates-io-token", { required: true });

return {
liveRun: liveRun == "" ? false : core.getBooleanInput("live-run"),
liveRun,
branch,
repos: repos.split("\n"),
repo,
githubToken,
interDepsRegExp: interDepsPattern == "" ? undefined : new RegExp(interDepsPattern),
unpublishedDepsRegExp: new RegExp(unpublishedDepsPatterns.split("\n").join("|")),
unpublishedDepsRepos: unpublishedDepsRepos.split("\n"),
cratesIoToken,
};
}
Expand All @@ -37,22 +40,21 @@ export async function main(input: Input) {
let registry: estuary.Estuary;
try {
registry = await estuary.spawn();
for (const repo of input.repos) {
core.startGroup(`Publishing ${repo} to estuary`);
clone(repo, input);
await publishToEstuary(repo, input, registry);
core.endGroup();

for (const repo of input.unpublishedDepsRepos) {
await publishToEstuary(input, repo, registry, /^$/);
}

await publishToEstuary(input, input.repo, registry, input.unpublishedDepsRegExp);

await deleteRepos(input);

if (input.liveRun) {
for (const repo of input.repos) {
core.startGroup(`Publishing ${repo} to crates.io`);
clone(repo, input);
publishToCratesIo(repo, input);
core.endGroup();
for (const repo of input.unpublishedDepsRepos) {
publishToCratesIo(input, repo);
}

publishToCratesIo(input, input.repo);
}

await cleanup(input, registry);
Expand All @@ -77,14 +79,17 @@ export async function cleanup(input: Input, registry: estuary.Estuary) {
await deleteRepos(input);
}

function clone(repo: string, input: Input): void {
function clone(input: Input, repo: string): void {
const remote = `https://${input.githubToken}@github.com/${repo}.git`;
sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`);
}

async function deleteRepos(input: Input) {
for (const repo of input.repos) {
core.info(`Deleting repository ${repoPath(repo)}`);
core.info(`Deleting repository clone ${repoPath(input.repo)}`);
await rm(repoPath(input.repo), { recursive: true, force: true });

for (const repo of input.unpublishedDepsRepos) {
core.info(`Deleting repository clone ${repoPath(repo)}`);
await rm(repoPath(repo), { recursive: true, force: true });
}
}
Expand All @@ -93,30 +98,38 @@ function repoPath(repo: string): string {
return repo.split("/").at(1);
}

async function publishToEstuary(repo: string, input: Input, registry: estuary.Estuary): Promise<void> {
const path = repoPath(repo);
async function publishToEstuary(
input: Input,
repo: string,
registry: estuary.Estuary,
registryDepsRegExp: RegExp,
): Promise<void> {
clone(input, repo);
const path = repoPath(input.repo);

await cargo.configRegistry(path, registry.name, registry.index);
await cargo.setRegistry(path, input.interDepsRegExp, registry.name);
await cargo.setRegistry(path, registryDepsRegExp, registry.name);

const env = {
CARGO_REGISTRY_DEFAULT: registry.name,
[`CARGO_REGISTRIES_${registry.name.toUpperCase()}_TOKEN`]: registry.token,
};

publish(repo, env);
publish(path, env);
}

function publishToCratesIo(repo: string, input: Input) {
function publishToCratesIo(input: Input, repo: string) {
clone(input, repo);
const path = repoPath(input.repo);

const env = {
CARGO_REGISTRY_TOKEN: input.cratesIoToken,
};

publish(repo, env);
publish(path, env);
}

function publish(repo: string, env: NodeJS.ProcessEnv) {
const path = repoPath(repo);
function publish(path: string, env: NodeJS.ProcessEnv) {
const options = {
env,
cwd: path,
Expand All @@ -128,5 +141,6 @@ function publish(repo: string, env: NodeJS.ProcessEnv) {
sh(`cargo publish --manifest-path ${package_.manifestPath}`, options);
}
}

sh("cargo clean", options);
}

0 comments on commit fe25448

Please sign in to comment.