Skip to content

Commit

Permalink
Cicd/Build docker (#77)
Browse files Browse the repository at this point in the history
* Fixed Dockerfile and added build step

* Added script for auto generating simple env

* Fixed docker tag and updated script to include faucet

* Updated package.json with new command

* renamed tutorial-setup

* Setup script generated wallet now persists
  • Loading branch information
CharlieMc0 authored Apr 6, 2023
1 parent 7ae7ad5 commit 2d33199
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Dockerfile
.github
.vscode
.changeset
.dockerignore
.dockerignore
.secrets
47 changes: 47 additions & 0 deletions .github/actions/build-docker-images/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: 'Build Docker Images'
description: 'Builds Docker images and pushes them to ECR and GHCR'
inputs:
DOCKER_FILENAME:
description: 'Name of the docker file to use for the build'
required: true
REPOSITORY_NAME:
description: 'Name of the Repository'
required: true
IMAGE_TAG:
description: 'Image Tag'
required: true
GHCR_USERNAME:
description: 'Username for GitHub Container Registry'
required: true
GHCR_TOKEN:
description: 'Token for GitHub Container Registry'
required: true

runs:
using: "composite"

steps:
- name: Set Environment Variables"
run: |
echo "DOCKER_FILENAME=${{ inputs.DOCKER_FILENAME }}" >> $GITHUB_ENV
echo "REPOSITORY_NAME=${{ inputs.REPOSITORY_NAME }}" >> $GITHUB_ENV
echo "IMAGE_TAG=${{ inputs.IMAGE_TAG }}" >> $GITHUB_ENV
echo "GHCR_USERNAME=${{ inputs.GHCR_USERNAME }}" >> $GITHUB_ENV
echo "GHCR_TOKEN=${{ inputs.GHCR_TOKEN }}" >> $GITHUB_ENV
shell: bash

- name: Log in to the GitHub Container Registry
id: login-ghcr
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ env.GHCR_USERNAME }}
password: ${{ env.GHCR_TOKEN }}

- name: Build, tag, and push images
shell: bash
env:
GHCR_REGISTRY: ghcr.io/zeta-chain
run: |
docker build -f $DOCKER_FILENAME -t $GHCR_REGISTRY/$REPOSITORY_NAME:$IMAGE_TAG .
docker push $GHCR_REGISTRY/$REPOSITORY_NAME:$IMAGE_TAG
45 changes: 45 additions & 0 deletions .github/actions/set-branch-name/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: 'SET:BRANCH:TAG:REFERENCES'
description: 'Logic to set branch-name from the event and type.'
inputs:
github_ref:
description: "github ref to use"
required: true
github_event:
description: "github event to use"
required: true
github_head_ref:
description: "github head ref to use"
required: true
github_commit_sha:
description: "github sha to use"
required: true
current_branch_name:
description: "current_branch"
required: true
runs:
using: "composite"
steps:
- uses: actions/checkout@v3

- name: Set branch name
id: set-branch-name
shell: bash
run: |
tag_check=`echo '${{ inputs.github_ref }}' | grep 'tags'` || echo "proceed"
if [ "${{ inputs.github_event }}" == "pull_request" ]; then
echo "Setting the current branch name to same as the source branch of the PR"
RELEASE_NAME=$(sed 's#/#-#g' <<< ${{ inputs.github_head_ref }})
echo "RELEASE_NAME=$RELEASE_NAME" >> $GITHUB_ENV
TAG_NAME="$RELEASE_NAME-${{ inputs.github_commit_sha }}"
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
elif [ "${{ inputs.github_ref }}" == "refs/heads/main" ]; then
TAG_NAME="latest"
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
else
RELEASE_NAME=$(sed 's#/#-#g' <<< ${{ inputs.current_branch_name }})
echo "RELEASE_NAME=$RELEASE_NAME" >> $GITHUB_ENV
TAG_NAME="$RELEASE_NAME-${{ inputs.github_commit_sha }}"
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
fi
53 changes: 53 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build

on:
push:
branches:
- main
pull_request:
branches:
- main



env:
GITHUB_REF_NAME: "$(echo ${{ github.ref_name }} | tr '//' '-')"

jobs:
build-docker:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3

- name: setup-git-credentials
uses: de-vri-es/[email protected]
with:
credentials: ${{ secrets.PAT_GITHUB_SERVICE_ACCT }}

- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6

- name: Git Hash
run: |
echo "GIT_HASH=$(git rev-parse --short HEAD)" >> ${GITHUB_ENV}
echo "WORKSPACE_DIR=$(pwd)" >> ${GITHUB_ENV}
- name: set-branch-name
uses: ./.github/actions/set-branch-name
with:
github_ref: "${{github.ref}}"
github_event: "${{ github.event_name }}"
github_head_ref: "${{ github.event.pull_request.head.ref }}"
github_commit_sha: "${{ env.GIT_HASH }}"
current_branch_name: "${{ steps.branch-name.outputs.current_branch }}"

- name: Build, tag, and push docker images
uses: ./.github/actions/build-docker-images
with:
DOCKER_FILENAME: Dockerfile
REPOSITORY_NAME: zetachain
IMAGE_TAG: ${{ env.TAG_NAME }}
GHCR_USERNAME: ${{ secrets.PAT_GITHUB_SERVICE_ACCT_USERNAME }}
GHCR_TOKEN: ${{ secrets.PAT_GITHUB_SERVICE_ACCT }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ node_modules
!.yarn/sdks
!.yarn/versions


# Hardhat
artifacts
typechain-types
Expand All @@ -20,8 +21,12 @@ tsconfig.tsbuildinfo
# Misc
.env
.DS_Store
.secrets
.wallet.json

# Slither
scripts/slither-results/*
!scripts/slither-results/.gitkeep



16 changes: 13 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ RUN apk add --update python3 yarn git nodejs make g++
## Install node modules before the code is copied to the container
WORKDIR /home/zetachain/
COPY package*.json ./
RUN yarn install
COPY packages/addresses-tools/package.json ./packages/addresses-tools/package.json
COPY packages/interfaces/package.json ./packages/interfaces/package.json
COPY packages/zevm-example-contracts/package.json ./packages/zevm-example-contracts/package.json
COPY packages/addresses/package.json packages/addresses/package.json
COPY packages/protocol-contracts/package.json ./packages/protocol-contracts/package.json
COPY packages/zevm-protocol-contracts/package.json ./packages/zevm-protocol-contracts/package.json
COPY packages/example-contracts/package.json ./packages/example-contracts/package.json
COPY packages/zeta-app-contracts/package.json ./packages/zeta-app-contracts/package.json
RUN yarn install ; exit 0

COPY . ./
RUN yarn install
RUN yarn install

RUN yarn add [email protected]
RUN yarn add [email protected]
Expand All @@ -21,7 +29,9 @@ RUN yarn add [email protected]

RUN cd packages/protocol-contracts && npx hardhat compile && cd -
RUN cd packages/example-contracts && npx hardhat compile && cd -
RUN cd packages/zevm-contracts && npx hardhat compile && cd -
RUN cd packages/zeta-app-contracts && npx hardhat compile && cd -
RUN cd packages/zevm-example-contracts && npx hardhat compile && cd -
RUN cd packages/zevm-protocol-contracts && npx hardhat compile && cd -

WORKDIR /home/zetachain/

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"slither": "npx ts-node ./scripts/slither.ts",
"test": "yarn workspaces foreach -ptv run test",
"g:changeset": "changeset",
"g:release": "yarn changeset publish"
"g:release": "yarn changeset publish",
"setup-tutorial": "ts-node ./scripts/setup-tutorial.ts"
},
"devDependencies": {
"@changesets/cli": "^2.23.1",
Expand Down Expand Up @@ -62,5 +63,8 @@
"typechain": "^8.0.0",
"typescript": "^4.6.3"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"solc": "0.8.7"
}
}
8 changes: 4 additions & 4 deletions packages/addresses-tools/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getHardhatConfigNetworks = (PRIVATE_KEYS: string[]): NetworksUserCo
// chainId: 7001,
gas: 5000000,
gasPrice: 80000000000,
url: `https://archive.athens2.zetachain.com/evm`,
url: `https://api.athens2.zetachain.com/evm`,
},
"bsc-localnet": {
gas: 5000000,
Expand All @@ -26,13 +26,13 @@ export const getHardhatConfigNetworks = (PRIVATE_KEYS: string[]): NetworksUserCo
},
"eth-mainnet": {
accounts: PRIVATE_KEYS,
url: "https://api.mycryptoapi.com/eth",
url: "https://rpc.ankr.com/eth",
},
goerli: {
accounts: PRIVATE_KEYS,
gas: 2100000,
gasPrice: 38000000000,
url: `https://eth-goerli.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY_GOERLI}`,
url: `https://rpc.ankr.com/eth_goerli`,
},
hardhat: {
chainId: 1337,
Expand Down Expand Up @@ -64,7 +64,7 @@ export const getHardhatConfigNetworks = (PRIVATE_KEYS: string[]): NetworksUserCo
accounts: PRIVATE_KEYS,
gas: 5000000,
gasPrice: 80000000000,
url: "https://polygon-mumbai.chainstacklabs.com",
url: "https://rpc.ankr.com/polygon_mumbai",
},
ropsten: {
accounts: PRIVATE_KEYS,
Expand Down
105 changes: 105 additions & 0 deletions scripts/setup-tutorial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Wallet } from "ethers";
import * as fs from "fs";
import * as https from "https";

const jsonPath = "scripts/.wallet.json";

interface WalletConfig {
address: string;
privateKey: string;
}

function saveEnvFiles(address: string, privateKey: string): void {
const value: string = `
PRIVATE_KEY=${privateKey}
ZETA_NETWORK=athens
EXECUTE_PROGRAMMATICALLY=true`;
const filePaths: string[] = ["packages/example-contracts/.env", "packages/zevm-example-contracts/.env"];

filePaths.forEach((filePath: string) => {
fs.writeFile(filePath, value, (err: NodeJS.ErrnoException | null) => {
if (err) {
console.error(`Failed to write to file ${filePath}: ${err}`);
} else {
console.log(`Updated .env file "${filePath}".`);
}
});
});
}
function saveWalletFile(address: string, privateKey: string, jsonPath: string): void {
const data = `{"address": "${address}", "privateKey": "${privateKey}}`;

fs.access(jsonPath, err => {
if (err) {
// The file does not exist, so we can write to it
fs.writeFile(jsonPath, data, err => {
if (err) throw err;
console.log("The new wallet file has been saved!");
});
} else {
// The file already exists, so we do not write to it
// console.log("The file already exists!");
}
});
}
function callFaucet(address: string): void {
// Hit Faucet to get some testnet Zeta
console.log("Requesting testnet assets from the faucet...");
const options: https.RequestOptions = {
hostname: "faucet.zetachain.link",
method: "GET",
path: `/eth/${address}`,
port: 443
};

const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);

res.on("data", d => {
process.stdout.write(d);
});
});

req.on("error", error => {
console.error(error);
});

req.end();
}

function createWallet(filePath: string, callback: () => void): void {
fs.access(filePath, err => {
if (!err) {
callback();
}
});
}

async function getOrCreateWallet(filePath: string): Promise<Wallet> {
let wallet: Wallet;

try {
const data = await fs.promises.readFile(filePath, "utf8");
const walletConfig: WalletConfig = JSON.parse(data);
wallet = new Wallet(walletConfig.privateKey);
} catch (error) {
console.log("No Wallet Exists, Creating One...");
const newWallet = Wallet.createRandom();
const walletConfig: WalletConfig = {
address: newWallet.address,
privateKey: newWallet.privateKey
};
await fs.promises.writeFile(filePath, JSON.stringify(walletConfig));
wallet = newWallet;
}

return wallet;
}

const wallet = getOrCreateWallet(jsonPath).then(async wallet => {
console.log(`Your Wallet Address: ${wallet.address}`);
console.log(`Your Private Key: ${wallet.privateKey.substring(2)}`);
saveEnvFiles(wallet.address, wallet.privateKey.substring(2));
saveWalletFile(wallet.address, wallet.privateKey.substring(2), jsonPath);
await callFaucet(wallet.address);
});
Loading

0 comments on commit 2d33199

Please sign in to comment.