Skip to content

Commit

Permalink
chore: Add GH Action for automatic deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
gmolki committed Mar 23, 2024
1 parent 5dfafe2 commit c9cafca
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/build_and_deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Build and Deploy

"on":
push:
branches:
- main
jobs:
build-project:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: 18

- name: Cache Node Modules
uses: actions/cache@v3
with:
path: |
~/.npm
**/node_modules
key: "${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}"
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies and Build Project
env:
FONTAWESOME_NPM_AUTH_TOKEN: "${{ secrets.FONTAWESOME_NPM_AUTH_TOKEN }}"
run: |
npm config set "@fortawesome:registry" https://npm.fontawesome.com/
npm config set "//npm.fontawesome.com/:_authToken" "${FONTAWESOME_NPM_AUTH_TOKEN}"
npm ci
npm run build
- uses: actions/upload-artifact@v3
with:
name: aleph-node-metrics-build-artifact
path: |
build/
scripts/
id: aleph-node-metrics-build-artifact

push-to-ipfs:
needs: build-project
runs-on: ubuntu-22.04
steps:
- name: Download Build Artifact
uses: actions/download-artifact@v3
with:
name: aleph-node-metrics-build-artifact

- name: Install aioipfs
run: |
pip install 'aioipfs>=0.6.2'
- name: Push on IPFS
run: |
IPFS_CID=$(python3 ./scripts/aleph_ipfs_push.py)
echo $IPFS_CID > ipfs_cid.txt
- uses: actions/upload-artifact@v3
with:
name: ipfs-cid
path: ipfs_cid.txt

deploy:
needs: push-to-ipfs
runs-on: ubuntu-22.04
steps:
- uses: actions/download-artifact@v3
with:
name: ipfs-cid

- name: Read IPFS CID from downloaded artifact
id: ipfs_cid
run: echo "::set-output name=CID::$(cat ipfs_cid.txt)"

- name: Update the main node-metrics on https://node-metrics.gerardmolina.com
if: github.ref == 'refs/heads/main'
run: |
mkdir --parents /home/runner/.aleph-im/private-keys
echo ${{ secrets.ALEPH_PRIVATE_KEY }} | xxd -r -p > /home/runner/.aleph-im/private-keys/ethereum.key
pip install 'aleph-client==0.6.1'
ITEM_HASH=$(aleph file pin ${{ steps.ipfs_cid.outputs.CID }} | jq -r '.item_hash')
echo 'y' | aleph domain attach node-metrics.gerardmolina.com --item-hash $ITEM_HASH
- uses: actions/upload-artifact@v3
with:
name: aleph-node-metrics
path: out/
55 changes: 55 additions & 0 deletions scripts/aleph_ipfs_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

"""
This script uploads the build SPA to IPFS.
It does not ping it or do anything else yet, so the result can only be accessed
as long as the files are not garbage collected.
Requires: 'aioipfs>=0.6.2'
"""

import asyncio
import logging
from pathlib import Path
from typing import NewType

import aioipfs

logger = logging.getLogger(__file__)

Multiaddr = NewType("Multiaddr", str)
CID = NewType("CID", str)


def raise_no_cid():
raise ValueError("Could not obtain a CID")


async def upload_site(files: list[Path], multiaddr: Multiaddr) -> CID:
client = aioipfs.AsyncIPFS(maddr=multiaddr)

try:
cid = None
async for added_file in client.add(*files, recursive=True):
logger.debug(
f"Uploaded file {added_file['Name']} with CID: {added_file['Hash']}"
)
cid = added_file["Hash"]
# The last CID is the CID of the directory uploaded
return cid or raise_no_cid()
finally:
await client.close()


async def publish_site(multiaddr: Multiaddr) -> CID:
path = Path(__file__).parent.parent / "build"
cid = await upload_site(files=[path], multiaddr=multiaddr)
return cid


def main():
print(asyncio.run(publish_site(Multiaddr("/dns6/ipfs-2.aleph.im/tcp/443/https"))))


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

0 comments on commit c9cafca

Please sign in to comment.