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

Cicd #19

Closed
wants to merge 13 commits into from
Closed

Cicd #19

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
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and publish image
on:
push:
branches:
- main
tags:
- '*'

jobs:
test_build_and_push_to_docker_registry:
name: Test, build and publish image to docker hub
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
# - uses: actions/setup-node@v3
# with:
# node-version: '20.x'
# - name: Install dependencies
# run: npm install && npm run postinstall
# - name: Unit tests
# run: npm run test
- name: Docker login
env:
DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}}
DOCKER_PASSWORD: ${{secrets.DOCKER_ACCESS_TOKEN}}
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

- name: Determine Docker Image Name
id: image
run: |
if [[ "${{github.ref_name}}" == "master" ]]; then
echo "name=xinfinorg/xdc-zero:latest" >> $GITHUB_OUTPUT
else
echo "name=xinfinorg/xdc-zero:${{github.ref_name}}" >> $GITHUB_OUTPUT
fi

- name: Docker build and tag image
run: docker build . --file cicd/Dockerfile --tag ${{ steps.image.outputs.name }}
- name: Docker push
run: docker push ${{ steps.image.outputs.name }}
50 changes: 50 additions & 0 deletions .github/workflows/pr_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Build custom branch
on:
pull_request:
branches:
- main

jobs:
build:
if: |
( startsWith(github.head_ref, 'feature') ||
startsWith(github.head_ref, 'fix') )
name: Deploy on PR
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: check out trigger branch
run: |
git fetch origin $BRANCH
git checkout $BRANCH
env:
BRANCH: ${{ github.head_ref }}

- name: Record branch env
id: branch
run: |
echo "repo=${{ github.repository }}" >> $GITHUB_OUTPUT
echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
echo "commit=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
# echo $repo
# echo $branch
# echo $commit

- name: Docker login
env:
DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}}
DOCKER_PASSWORD: ${{secrets.DOCKER_ACCESS_TOKEN}}
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

- name: Determine Docker Image Name
id: image
run: |
echo "name=xinfinorg/xdc-zero:${{ steps.branch.outputs.branch }}" >> $GITHUB_OUTPUT

- name: Build and push image
run: |
docker build . --file cicd/Dockerfile --tag ${{ steps.image.outputs.name }}
docker push ${{ steps.image.outputs.name }}
19 changes: 19 additions & 0 deletions cicd/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111
PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222
CSC=0x3333333333333333333333333333333333333333
REVERSE_CSC=0x4444444444444444444444444444444444444444


SUBNET_URL=https://devnetstats.apothem.network/subnet/
# PARENTNET can be devnet,testnet,mainnet
PARENTNET=devnet
# custom PARENTNET_URL can be used instead of PARENTNET
# PARENTNET_URL=


# SUBNET_ZERO_CONTRACT=
# PARENTNET_ZERO_CONTRACT=

# SUBNET_APP=
# PARENTNET_APP=
5 changes: 5 additions & 0 deletions cicd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.env
package-lock.json
mount/*
!mount/placeholder.txt
14 changes: 14 additions & 0 deletions cicd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:20-alpine

COPY . /app

WORKDIR /app/cicd
RUN yarn
WORKDIR /app/endpoint
RUN yarn
WORKDIR /app/applications/subswap/contract
RUN yarn

WORKDIR /app/cicd

ENTRYPOINT ["node"]
2 changes: 2 additions & 0 deletions cicd/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**node_modules
**package-lock.json
87 changes: 87 additions & 0 deletions cicd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Deploy with Docker
## Deploy XDC Zero
1. Create a .env as the below example PARENTNET can be devnet,testnet,mainnet. For custom url PARENTNET_URL will override PARENTNET.
```
PARENTNET=devnet
SUBNET_URL=
SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111
PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222
CSC=0x3333333333333333333333333333333333333333
REVERSE_CSC=0x4444444444444444444444444444444444444444
```

2. Deploy XDC-Zero endpoints and register chain
```
docker run --env-file .env xinfinorg/xdc-zero:latest endpointandregisterchain.js
```

- add the output to .env
```
SUBNET_ZERO_CONTRACT=0x5555555555555555555555555555555555555555
PARENTNET_ZERO_CONTRACT=0x6666666666666666666666666666666666666666
```

## Deploy Subswap
3. Deploy Subswap
```
docker run --env-file .env xinfinorg/xdc-zero:latest subswap.js
```
- add the output to .env
```
SUBNET_APP=0x7777777777777777777777777777777777777777
PARENTNET_APP=0x8888888888888888888888888888888888888888
```


## Register Application to XDC Zero
4. Register Subswap to XDC-Zero
```
docker run --env-file .env xinfinorg/xdc-zero:latest applicationregister.js
```

<br/>
<br/>

# Deploy using this repository

1. Install packages
```
cd ../endpoint
yarn
cd ../applications/subswap/contract
yarn
cd cicd
yarn
```

2. Configure .env at cicd/mount/.env. PARENTNET can be devnet,testnet,mainnet. For custom url PARENTNET_URL will override PARENTNET.
```
PARENTNET=devnet
SUBNET_URL=
SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111
PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222
CSC=0x3333333333333333333333333333333333333333
REVERSE_CSC=0x4444444444444444444444444444444444444444
```

3. Deploy endpoint and register chain
```
cd cicd
node endpointandregisterchain.js
```

4. Deploy Subswap
```
node subswap.js
```
- add the output to cicd/mount/.env
```
SUBNET_APP=0x7777777777777777777777777777777777777777
PARENTNET_APP=0x8888888888888888888888888888888888888888
```


5. Register Application
```
node applicationregister.js
```
164 changes: 164 additions & 0 deletions cicd/applicationregister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
process.chdir(__dirname)
const { execSync } = require("child_process");
const fs = require('node:fs');
const env = require("dotenv").config({path: 'mount/.env'});
const config = {
"relativePath": "../endpoint/"
}
const endpointConfig = {}

const { ethers } = require('ethers');
const u = require('./util.js')


main()

async function main(){
console.log("start application register")
importEndpointJson()
initApplicationRegister()
await u.getNetworkID(config)
configureEndpointJson()
registerApplication()
exportEndpointJson()

}


function importEndpointJson(){
if (!fs.existsSync('./mount/endpointconfig.json')){
if (process.env.SUBNET_ZERO_CONTRACT && process.env.PARENTNET_ZERO_CONTRACT){
config.subnetEndpoint = process.env.SUBNET_ZERO_CONTRACT
config.parentnetEndpoint = process.env.PARENTNET_ZERO_CONTRACT
} else {
throw Error("mount/endpointconfig.json not found, and SUBNET_ZERO_CONTRACT and PARENTNET_ZERO_CONTRACT are not configured")
}
endpointConfig["xdcsubnet"]={
"endpoint": config.subnetEndpoint,
"applications":[]
}
endpointConfig["xdcparentnet"]={
"endpoint": config.parentnetEndpoint,
"applications":[]
}
return
}

const epjs = JSON.parse(fs.readFileSync('./mount/endpointconfig.json', 'utf8'));
if (epjs.xdcsubnet.endpoint && epjs.xdcparentnet.endpoint){
endpointConfig["xdcsubnet"] = epjs.xdcsubnet
endpointConfig["xdcparentnet"] = epjs.xdcparentnet
} else {
throw Error("invalid endpointconfig.json format")
}
}

function initApplicationRegister(){
if (process.env.PARENTNET_URL){
parentnetURL = process.env.PARENTNET_URL
} else if (process.env.PARENTNET){
parentnet = process.env.PARENTNET
if (parentnet == "devnet") parentnetURL = "https://devnetstats.apothem.network/devnet";
if (parentnet == "testnet") parentnetURL = "https://devnetstats.apothem.network/testnet";
if (parentnet == "mainnet") parentnetURL = "https://devnetstats.apothem.network/mainnet";
} else {
throw Error("PARENTNET or PARENTNET_URL not found")
}

const reqENV = [
"SUBNET_PK",
"PARENTNET_PK",
"SUBNET_APP",
"PARENTNET_APP",
"SUBNET_URL",
];
const isEnabled = reqENV.every(envVar => envVar in process.env)
if (!isEnabled){
throw Error("incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_APP, PARENTNET_APP, SUBNET_URL")
}
subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`;
parentnetPK = process.env.PARENTNET_PK.startsWith("0x") ? process.env.PARENTNET_PK : `0x${process.env.PARENTNET_PK}`;
subnetApp = process.env.SUBNET_APP.startsWith("0x") ? process.env.SUBNET_APP : `0x${process.env.SUBNET_APP}`;
parentnetApp = process.env.PARENTNET_APP.startsWith("0x") ? process.env.PARENTNET_APP : `0x${process.env.PARENTNET_APP}`;
subnetURL = process.env.SUBNET_URL;

config["subnetPK"] = subnetPK
config["parentnetPK"] = parentnetPK
config["subnetURL"] = subnetURL
config["parentnetURL"] = parentnetURL
config["subnetApp"] = subnetApp
config["parentnetApp"] = parentnetApp

}

function configureEndpointJson(){
subApp = {
"rid": config.parentnetID,
"rua": config.parentnetApp,
"sua": config.subnetApp
}
parentApp = {
"rid": config.subnetID,
"rua": config.subnetApp,
"sua": config.parentnetApp,
}

existingSubApps = endpointConfig.xdcsubnet.applications
existingParentApps = endpointConfig.xdcparentnet.applications

if (!(Array.isArray(existingSubApps) && Array.isArray(existingParentApps))){
endpointConfig.xdcsubnet.applications = [subApp]
endpointConfig.xdcparentnet.applications = [parentApp]
} else{
subDupe = false
for (var i=0; i<existingSubApps.length; i++){
if (
existingSubApps[i].rid == subApp.rid &&
existingSubApps[i].rua == subApp.rua &&
existingSubApps[i].sua == subApp.sua
){ subDupe = true; break } //don't append if app already exists
}
if (!subDupe) endpointConfig.xdcsubnet.applications.push(subApp)

parentDupe = false;
for (var i=0; i<existingParentApps.length; i++){
if (
existingParentApps[i].rid == parentApp.rid &&
existingParentApps[i].rua == parentApp.rua &&
existingParentApps[i].sua == parentApp.sua
){ parentDupe = true; break } //don't append if app already exists
}
if (!parentDupe) endpointConfig.xdcparentnet.applications.push(parentApp)
}
console.log("writing endpointconfig.json")
fs.writeFileSync('../endpoint/endpointconfig.json', JSON.stringify(endpointConfig, null, 2) , 'utf-8', err => {
if (err) {
throw Error("error writing endpointconfig.json, "+err)
}
});
}

function registerApplication(){
console.log("writing network config")
u.writeNetworkJson(config)
console.log("configuring PK")
u.writeEnv(config.subnetPK, config.relativePath)
console.log("register parentnet application to subnet")
subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcsubnet")
if (!subnetEndpointOut.includes("success")) throw Error("failed to register parentnet app to subnet")

console.log("configuring PK")
u.writeEnv(config.parentnetPK, config.relativePath)
console.log("register subnet application to parentnet endpoint")
parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcparentnet")
if (!parentnetEndpointOut.includes("success")) throw Error("failed to register subnet app to parentnet")

}


function exportEndpointJson(){
fs.copyFileSync('../endpoint/endpointconfig.json', './mount/endpointconfig.json')
ep = fs.readFileSync('../endpoint/endpointconfig.json').toString()
console.log("SUCCESS register application, endpointconfig:")
console.log(ep)
}
Loading
Loading