Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registry update functionality #1

Merged
merged 16 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ This action requires the upload directory to be already available so you will ne

Find out more about github token from [documentation](https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow).

### `registry-seed`

You can provide a seed (keep it secret, keep it safe) and this action will set corresponding skynet registry entry value to the deployed skylink.

Public link to the registry entry will be printed in the action log.

## Outputs

### `skylink`
Expand All @@ -42,7 +48,10 @@ with:
```yaml
name: My CI Pipeline

on: [pull_request]
on:
pull_request:
push:
branches: [main]

jobs:
build:
Expand All @@ -66,4 +75,5 @@ jobs:
with:
upload-dir: public
github-token: ${{ secrets.GITHUB_TOKEN }}
registry-seed: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && secrets.REGISTRY_SEED || '' }}
```
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ inputs:
github-token:
description: "Github token used for including the skylink url in pull request"
required: true
registry-seed:
description: "Seed that generates registry private key (optional, only if you want to update the registry entry with skylink)"
registry-datakey:
description: "Registry datakey to use when updating the registry entry"
default: "skylink.txt"
outputs:
skylink:
description: "Uploaded resource skylink"
Expand Down
38 changes: 35 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const core = require("@actions/core");
const github = require("@actions/github");
const { SkynetClient } = require("@nebulous/skynet");
const { parseSkylink } = require("skynet-js");
const { SkynetClient: NodeSkynetClient } = require("@nebulous/skynet");
const { parseSkylink, keyPairFromSeed, SkynetClient } = require("skynet-js");
const { HashDataKey } = require("skynet-js/dist/crypto");
const base64 = require("base64-js");
const base32Encode = require("base32-encode");

Expand All @@ -20,7 +21,7 @@ function encodeBase32(input) {
(async () => {
try {
// upload to skynet
const skynetClient = new SkynetClient();
const skynetClient = new NodeSkynetClient();
const skylink = await skynetClient.uploadDirectory(
core.getInput("upload-dir")
);
Expand All @@ -35,6 +36,37 @@ function encodeBase32(input) {
core.setOutput("skylink-url", skylinkUrl);
console.log(`Deployed to: ${skylinkUrl}`);

// if registry is properly configured, update the skylink in the entry
if (core.getInput("registry-seed") && core.getInput("registry-datakey")) {
try {
const skynetClient = new SkynetClient("https://siasky.net");
const dataKey = core.getInput("registry-datakey");
const { publicKey, privateKey } = keyPairFromSeed(
core.getInput("registry-seed")
);

const entry = await skynetClient.registry.getEntry(publicKey, dataKey);
const updatedEntry = {
datakey: dataKey,
revision: entry ? entry.entry.revision + 1 : 0,
data: skylink,
};
await skynetClient.registry.setEntry(privateKey, dataKey, updatedEntry);

const encodedPublicKey = encodeURIComponent(
`ed25519:${publicKey.toString("hex")}`
);
const encodedDataKey = encodeURIComponent(
Buffer.from(HashDataKey(dataKey)).toString("hex")
);
console.log(
`Registry entry updated: https://siasky.net/skynet/registry?publickey=${encodedPublicKey}&datakey=${encodedDataKey}`
);
} catch (error) {
console.log(`Failed to update registry entry ${error.message}`);
}
}

// put a skylink in a pull request comment if available
if (github.context.issue.number) {
const gitHubToken = core.getInput("github-token");
Expand Down
Loading