diff --git a/.deploy/ipfs_deploy.js b/.deploy/ipfs_deploy.js index d27d9a9..030bf97 100644 --- a/.deploy/ipfs_deploy.js +++ b/.deploy/ipfs_deploy.js @@ -1,10 +1,13 @@ import { create, globSource } from 'kubo-rpc-client'; +import { writeFile } from 'node:fs'; -async function addDir(){ - const auth = process.env.IPFS_RPC_API_SECRET; - const url = process.env.IPFS_RPC_API_URL; - console.log('auth:', auth) +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: { @@ -12,9 +15,36 @@ async function addDir(){ } }) - for await (const file of client.addAll(globSource(".", "public/**/*"))) { - console.log(file) + console.log('adding files on IPFS..'); + for await (const file of client.addAll(globSource(".", "public/**/*"))) { + files.push({ path: file.path, cid: file.cid.toString() }); + } + + const newPublicDirCID = files[files.length - 1].cid; + console.log(`pinned: ${newPublicDirCID}`); + + let unpinned; + for await (const file of client.pin.ls()) { + unpinned = file.cid.toString(); + if (file.type === 'recursive' && unpinned !== newPublicDirCID) { + await client.pin.rmAll(unpinned); } + } + + console.log(`unpinned: ${unpinned}`); + + console.log('publishion 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(`saving unpinned and pinned CIDs in ${save_dir}`) + await writeFile(save_dir, `${unpinned}\n${newPublicDirCID}`, err => { + if (err) throw err; + console.log(`${save_dir} has been saved`); + }); } -addDir() +IPFSDeploy(); diff --git a/.github/workflows/github-actions-demo.yaml b/.github/workflows/github-actions-demo.yaml index 4984f44..573067d 100644 --- a/.github/workflows/github-actions-demo.yaml +++ b/.github/workflows/github-actions-demo.yaml @@ -2,9 +2,13 @@ name: Publish portfolio on IPFS run-name: Build Zola portfolio and publish it on IPFS on: push jobs: - build: + deploy: runs-on: ubuntu-latest environment: production + env: + CIDS_FILENAME: '/tmp/cids' + PINATA_PIN_NAME: ${{ vars.PINATA_PIN_NAME }} + steps: - name: Install Zola uses: taiki-e/install-action@v2 @@ -28,7 +32,7 @@ jobs: npx all-relative cd .. - - name: Add public on IPFS + - name: Deploy on IPFS run: | cd .deploy && npm install . mv ../public . @@ -37,3 +41,20 @@ jobs: IPFS_RPC_API_URL: ${{ vars.IPFS_RPC_API_URL }} IPFS_RPC_API_SECRET: ${{ vars.IPFS_RPC_API_SECRET }} + - name: Unpin old and pin new versions on Pinata + run: | + UNPIN_CID=$(cat $CIDS_FILE_PATH | head -n 1) + PIN_CID=$(cat CIDS_FILE_PATH | tail -n 1) + + curl -X DELETE \ + -H "Authorization: Bearer ${{ secrets.PINATA_API_KEY }}" \ + https://api.pinata.cloud/pinning/unpin/$UNPIN_CID + + 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