Skip to content

Commit

Permalink
foo
Browse files Browse the repository at this point in the history
  • Loading branch information
vesla0x1 committed Apr 9, 2024
1 parent a6808d9 commit 867667d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 66 deletions.
76 changes: 33 additions & 43 deletions .deploy/ipfs_deploy.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
import { create, globSource } from 'kubo-rpc-client';
import { writeFile } from 'node:fs';

const auth = process.env.IPFS_RPC_API_SECRET;
const url = process.env.IPFS_RPC_API_URL;
const key = process.env.IPFS_RPC_API_NAME_KEY;
const cids_path = process.env.CIDS_FILE_PATH;

async function IPFSDeploy(){
const files = [];
const client = await create({
url: `${url}/api/v0`,
headers: {
authorization: `Bearer ${auth}`,
}
})

console.log('adding files on IPFS..');
for await (const file of client.addAll(globSource(".", "public/**/*"))) {
files.push({ path: file.path, cid: file.cid.toString() });
async function IPFSDeploy(ipfsRpcApiKey, ipfsRpcApiUrl, ipfsRpcApiNameKey){
const client = await create({
url: `${ipfsRpcApiUrl}/api/v0`,
headers: {
authorization: `Bearer ${ipfsRpcApiKey}`,
}

const newPublicDirCID = files[files.length - 1].cid;
console.log(`pinned: ${newPublicDirCID}`);

let unpinned = '';
for await (const file of client.pin.ls()) {
if (file.type === 'recursive' && file.cid.toString() !== newPublicDirCID) {
for await (const unpinnedFile of client.pin.rmAll(file)) {
unpinned = unpinnedFile;
}
}
});

let lastUploadedCID;
console.log('adding files on IPFS..');
for await (const file of client.addAll(globSource(".", "public/**/*"))) {
lastUploadedCID = file.cid.toString();
}
console.log(`pinned: ${lastUploadedCID}`);

for await (const file of client.pin.ls()) {
const cid = file.cid.toString();
if (file.type === 'recursive' && cid !== lastUploadedCID) {
await client.pin.rmAll(file);
console.log(`unpinned: ${cid}`);
}
}

console.log(`unpinned: ${unpinned}`);

console.log('publishing on IPNS...')
const r = await client.name.publish(`${newPublicDirCID}`, { key });

console.log('new version available on:');
console.log(`IPFS address: https://ipfs.io/ipfs/${newPublicDirCID}`);
console.log(`IPNS address: https://ipfs.io/ipns/${r.name}`);
console.log('publishing on IPNS...')
const r = await client.name.publish(`${lastUploadedCID}`, {
key: ipfsRpcApiNameKey
});
console.log('new version available on:');
console.log(`IPFS address: https://ipfs.io/ipfs/${lastUploadedCID}`);
console.log(`IPNS address: https://ipfs.io/ipns/${r.name}`);

console.log(`saving unpinned and pinned CIDs in ${cids_path}`)
await writeFile(cids_path, `${unpinned}\n${newPublicDirCID}`, err => {
if (err) throw err;
console.log(`${cids_path} has been saved!`);
});
console.log(lastUploadedCID);
}

IPFSDeploy();
IPFSDeploy(
process.argv[2],
process.argv[3],
process.argv[4]
);
54 changes: 54 additions & 0 deletions .deploy/pinata_update_pinned.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
let API_KEY = '';
const PINATA_BASE_URL = 'https://api.pinata.cloud';
const ENDPOINT_UNPIN = '/pinning/unpin/';
const ENDPOINT_PIN_LIST = '/data/pinList';
const ENDPOINT_PIN_BY_HASH = '/pinning/pinByHash';

async function pinataApiReq(resource, method = 'GET', body = null) {
const headers = {
authorization: `Bearer ${API_KEY}`,
};

const options = {
method,
headers
};

if (body) {
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(body);
}

return fetch(PINATA_BASE_URL + resource, options).then(data =>
data.json().then(encoded => encoded, _ => data)
);
}

async function upadtePinned(apiKey, pinName, cidToPin) {
API_KEY = apiKey;
let response = await pinataApiReq(ENDPOINT_PIN_LIST);
const hashesToUnpin = response.rows
.filter(r => r.date_unpinned === null &&
r.metadata.name === pinName)
.map(r => r.ipfs_pin_hash);

await Promise.all(hashesToUnpin.map(cid =>
pinataApiReq(ENDPOINT_UNPIN + cid, 'DELETE').then(_ => {
console.log(`unpinned: ${cid}`);
})
));

response = await pinataApiReq(ENDPOINT_PIN_BY_HASH, 'POST', {
hashToPin: cidToPin,
pinataMetadata: JSON.stringify({
name: pinName
}),
});

console.log(`pinned: ${response.ipfsHash}`);
}

(() => {
API_KEY = process.argv[2];
upadtePinned(process.argv[3], process.argv[4]);
})();
34 changes: 11 additions & 23 deletions .github/workflows/github-actions-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ jobs:
runs-on: ubuntu-latest
environment: production
env:
CIDS_FILE_PATH: '/tmp/cids'
PINATA_PIN_NAME: ${{ vars.PINATA_PIN_NAME }}
CID_TO_PIN_PATH: '/tmp/cid_to_pin'

steps:
- name: Install Zola
Expand Down Expand Up @@ -36,26 +35,15 @@ jobs:
run: |
cd .deploy && npm install .
mv ../public .
node ipfs_deploy.js
env:
IPFS_RPC_API_URL: ${{ vars.IPFS_RPC_API_URL }}
IPFS_RPC_API_SECRET: ${{ vars.IPFS_RPC_API_SECRET }}
IPFS_RPC_API_NAME_KEY: ${{ vars.IPFS_RPC_API_NAME_KEY }}
node ipfs_deploy.js \
${{ secrets.IPFS_RPC_API_SECRET }} \
${{ vars.IPFS_RPC_API_URL }} \
${{ vars.IPFS_RPC_API_NAME_KEY }} | tail -n 1 > ${{ env.CID_TO_PIN_PATH }}
- name: Unpin old and pin new versions on Pinata
- name: Update pinned versions on Pinata
run: |
UNPIN_CID=$(cat $CIDS_FILE_PATH | head -n 1)
PIN_CID=$(cat $CIDS_FILE_PATH | tail -n 1)
curl -X POST \
-H "Authorization: Bearer ${{ secrets.PINATA_API_KEY }}" \
-H 'Content-type: application/json' \
-d "{
\"hashToPin\": \"$PIN_CID\",
\"pinataMetadata\": \"{\\\"name\\\": \\\"$PINATA_PIN_NAME\\\"}\"
}" \
https://api.pinata.cloud/pinning/pinByHash
curl -X DELETE \
-H "Authorization: Bearer ${{ secrets.PINATA_API_KEY }}" \
https://api.pinata.cloud/pinning/unpin/$UNPIN_CID
CID_TO_PIN=$(cat ${{ env.CID_TO_PIN_PATH }}
node pinata_update_pinned.js \
${{ secrets.PINATA_API_KEY }} \
${{ vars.PINATA_PIN_NAME }} \
$CID_TO_PIN

0 comments on commit 867667d

Please sign in to comment.