Skip to content

Commit

Permalink
updated anvil-devchain workflow (#11108)
Browse files Browse the repository at this point in the history
* updated anvil-devchain workflow

* PR feedback

* ++ other dependent files for this workflow to run

* fix build
  • Loading branch information
soloseng committed Jul 3, 2024
1 parent 8320770 commit 3cd4060
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 78 deletions.
100 changes: 69 additions & 31 deletions .github/workflows/protocol-devchain-anvil.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ on:
branches:
- master
- 'release/**'
tags:
- core-contracts.v*
pull_request:
branches: [release/core-contracts/*, master]
paths:
- 'packages/protocol/**'
workflow_dispatch:
inputs:
npm_tag:
description: 'NPM TAG e.g. alpha, pre-merge (default: canary) '
required: true
type: string

env:
# Increment these to force cache rebuilding
FOUNDRY_CACHE_KEY: 1
# Supported Foundry version defined at celo-org (GitHub organisation) level, for consistency across workflows.
SUPPORTED_FOUNDRY_VERSION: ${{ vars.SUPPORTED_FOUNDRY_VERSION }}
ANVIL_PORT: 8546

jobs:
Expand All @@ -16,8 +30,8 @@ jobs:
run:
working-directory: packages/protocol
name: Generate anvil
runs-on: ["self-hosted", "org", "npm-publish"]
permissions:
runs-on: ['self-hosted', 'org', 'npm-publish']
permissions:
contents: read
pull-requests: read
id-token: write
Expand Down Expand Up @@ -54,14 +68,14 @@ jobs:
uses: actions/github-script@v7
with:
script: |
const result = (
await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})
).data[0];
core.setOutput("number", result ? result.number : "");
const result = (
await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})
).data[0];
core.setOutput("number", result ? result.number : "");
- name: Set PR Number
id: set_pr_number
Expand All @@ -79,7 +93,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: "nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9"
version: ${{ env.SUPPORTED_FOUNDRY_VERSION }}

- name: Install forge dependencies
run: forge install
Expand All @@ -88,10 +102,33 @@ jobs:
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

- name: Akeyless Get Secrets
id: get_auth_token
uses: docker://us-west1-docker.pkg.dev/devopsre/akeyless-public/akeyless-action:latest
with:
api-url: https://api.gateway.akeyless.celo-networks-dev.org
access-id: p-kf9vjzruht6l
static-secrets: '{"/static-secrets/NPM/npm-publish-token":"NPM_TOKEN"}'

- uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'

- name: 'Setup yarn'
shell: bash
run: |
npm install --global yarn
source ~/.bashrc
- name: 'Install packages'
shell: bash
run: yarn

- name: Generate migrations and run devchain
if: success() || failure()
run: ./scripts/foundry/create_and_migrate_anvil_devchain.sh

- name: Run integration tests against local anvil devchain
if: success() || failure()
run: |
Expand All @@ -105,12 +142,23 @@ jobs:
sanitized_ref_name=$(echo "${{ github.ref_name }}" | tr -cd '[:alnum:]-_')
echo "sanitized_ref_name=${sanitized_ref_name}" >> $GITHUB_ENV
- name: Set package.json version based on GitHub ref
run: |
VERSION=${{ env.PR_NUMBER }}
echo "Setting version to 0.0.$VERSION"
jq ".version = \"0.0.$VERSION\"" .tmp/package.json > .tmp/temp.json && mv .tmp/temp.json .tmp/package.json
- name:
Determine release type and version (or dry run)
# This is what sets the RELEASE_TYPE and RELEASE_VERSION env variables
run: yarn --silent determine-release-version >> "$GITHUB_ENV"
working-directory: packages/protocol
env:
GITHUB_TAG: ${{ github.ref_name }}
NPM_PACKAGE: '@celo/devchain-anvil'
NPM_TAG: ${{ inputs.npm_tag }}

- name: Prepare package for publishing
run: yarn prepare_devchain_anvil_publishing
working-directory: packages/protocol
env:
RELEASE_TYPE: ${{ env.RELEASE_TYPE }}
RELEASE_VERSION: ${{ env.RELEASE_VERSION }}

- name: Upload devchain as artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -120,23 +168,13 @@ jobs:
# https://docs.github.com/en/actions/learn-github-actions/usage-limits-billing-and-administration#artifact-and-log-retention-policy
retention-days: 90

- name: Akeyless Get Secrets
id: get_auth_token
uses: docker://us-west1-docker.pkg.dev/devopsre/akeyless-public/akeyless-action:latest
with:
api-url: https://api.gateway.akeyless.celo-networks-dev.org
access-id: p-kf9vjzruht6l
static-secrets: '{"/static-secrets/NPM/npm-publish-token":"NPM_TOKEN"}'

- uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'

- name: Publish @celo/devchain-anvil
run: |
cat package.json
npm publish --access public
npm publish $RELEASE_TYPE $DRY_RUN --access public
working-directory: packages/protocol/.tmp
env:
RELEASE_TYPE: --tag ${{ env.RELEASE_TYPE != '' && env.RELEASE_TYPE || 'canary' }}
RELEASE_VERSION: ${{ env.RELEASE_VERSION }}
NODE_AUTH_TOKEN: ${{ env.NPM_TOKEN }}
DRY_RUN: ${{ env.RELEASE_VERSION == '' && '--dry-run' || '' }}
1 change: 1 addition & 0 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"prebuild": "rm -rf ./build",
"determine-release-version": "yarn --silent ts-node --preferTsExts ./scripts/determine-release-version.ts",
"prepare_contracts_and_abis_publishing": "yarn ts-node --preferTsExts ./scripts/prepare-contracts-and-abis-publishing.ts",
"prepare_devchain_anvil_publishing": "yarn ts-node --preferTsExts ./scripts/change-anvil-devchain-package-version.ts",
"validate_abis_exports": "yarn ts-node --preferTsExts ./scripts/validate-abis-package.ts",
"sourcify-publish": "yarn ts-node ./scripts/sourcify-publish.ts",
"migrate": "./scripts/bash/migrate.sh",
Expand Down
35 changes: 35 additions & 0 deletions packages/protocol/scripts/change-anvil-devchain-package-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { replacePackageVersionAndMakePublic } from '@celo/protocol/scripts/utils'
import * as child_process from 'child_process'
import * as fs from 'fs'
import * as path from 'path'
import { DEVCHAIN_ANVIL_PACKAGE_SRC_DIR, TSCONFIG_PATH } from './consts'

function log(...args: any[]) {
// eslint-disable-next-line
console.info('[prepare-devchain-anvil]', ...args)
}

try {
log('Setting package.json target to ES2020')
const tsconfig = JSON.parse(fs.readFileSync(TSCONFIG_PATH, 'utf8'))
tsconfig.compilerOptions.target = 'ES2020'
fs.writeFileSync(TSCONFIG_PATH, JSON.stringify(tsconfig, null, 4))

prepareAnvilDevchainPackage()
} finally {
log('Cleaning up')
child_process.execSync(`git checkout ${TSCONFIG_PATH}`, { stdio: 'inherit' })
}

function prepareAnvilDevchainPackage() {
if (process.env.RELEASE_VERSION) {
log('Replacing @celo/devchain-anvil version with RELEASE_VERSION)')

const packageJsonPath = path.join(DEVCHAIN_ANVIL_PACKAGE_SRC_DIR, 'package.json')
replacePackageVersionAndMakePublic(packageJsonPath)

return
}

log('Skipping @celo/devchain-anvil package.json preparation (no RELEASE_VERSION provided)')
}
5 changes: 3 additions & 2 deletions packages/protocol/scripts/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const CONTRACTS_08_PACKAGE_DESTINATION_DIR = path.join(CONTRACTS_PACKAGE_
export const ABIS_PACKAGE_SRC_DIR = path.join(__dirname, '../abis')
export const ABIS_BUILD_DIR = path.join(ABIS_PACKAGE_SRC_DIR, 'src-generated')
export const ABIS_DIST_DIR = path.join(ABIS_PACKAGE_SRC_DIR, 'dist')
export const DEVCHAIN_ANVIL_PACKAGE_SRC_DIR = path.join(__dirname, '../.tmp')
export const BUILD_EXECUTABLE = path.join(__dirname, 'build.ts')
export const TSCONFIG_PATH = path.join(ROOT_DIR, 'tsconfig.json')

Expand Down Expand Up @@ -35,7 +36,7 @@ export const ProxyContracts = [
'RegistryProxy',
'SortedOraclesProxy',
'UniswapFeeHandlerSellerProxy',
'MintGoldScheduleProxy',
'CeloDistributionScheduleProxy',
]

export const CoreContracts = [
Expand All @@ -51,7 +52,7 @@ export const CoreContracts = [
'MultiSig',
'Registry',
'Freezer',
'MintGoldSchedule',
'CeloDistributionSchedule',

// governance
'Election',
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/scripts/determine-release-version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'child_process'
import { determineNextVersion, getReleaseTypeFromSemVer } from './utils'

const npmPackage = process.env.NPM_PACKAGE?.trim() || ''
const npmTag = process.env.NPM_TAG?.trim() || ''
const gitTag = process.env.GITHUB_TAG || ''
const branchName = execSync('git branch --show-current').toString().trim()
Expand All @@ -12,7 +13,7 @@ const branchName = execSync('git branch --show-current').toString().trim()
// if not on a release branch a dry-run will be done unless an NPM_TAG is provided
// in which case we will try to fetch the last published version with that tag and bump or use the canary to get major and start versioning from there the new tag at 0
// (e.g. `@celo/[email protected]@custom-tag.0`)
const nextVersion = determineNextVersion(gitTag, branchName, npmTag)
const nextVersion = determineNextVersion(gitTag, branchName, npmPackage, npmTag)

if (nextVersion === null) {
// dry-run will build the package but not publish it
Expand Down
42 changes: 11 additions & 31 deletions packages/protocol/scripts/prepare-contracts-and-abis-publishing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Exports, replacePackageVersionAndMakePublic } from '@celo/protocol/scripts/utils'
import * as child_process from 'child_process'
import * as fs from 'fs'
import * as path from 'path'
Expand Down Expand Up @@ -116,11 +117,6 @@ function createIndex() {
fs.writeFileSync(path.join(ABIS_BUILD_DIR, 'index.ts'), reExports.join('\n'))
}

type Exports = Record<
string,
{ import?: string; require?: string; types?: string; default?: string }
>

// Helper functions
function prepareTargetTypesExports() {
const exports: Exports = {}
Expand Down Expand Up @@ -238,38 +234,22 @@ function processRawJsonsAndPrepareExports() {
return exports
}

type JSON = Record<string, string | boolean | Exports>

function replacePackageVersionAndMakePublic(
packageJsonPath: string,
onDone?: (json: JSON) => void
) {
const json: JSON = JSON.parse(fs.readFileSync(packageJsonPath).toString())
function prepareAbisPackageJson(exports: Exports) {
log('Preparing @celo/abis package.json')
const packageJsonPath = path.join(ABIS_PACKAGE_SRC_DIR, 'package.json')

if (process.env.RELEASE_VERSION) {
log(`Replacing ${json.name as string} version with provided RELEASE_VERSION`)
log('Replacing @celo/abis version with RELEASE_VERSION)')

json.version = process.env.RELEASE_VERSION
json.private = false
} else {
log('No RELEASE_VERSION provided')
}
replacePackageVersionAndMakePublic(packageJsonPath, (json) => {
log('Setting @celo/abis exports')
json.exports = exports
})

if (onDone !== undefined) {
onDone(json)
return
}

fs.writeFileSync(packageJsonPath, JSON.stringify(json, null, 2))
}

function prepareAbisPackageJson(exports: Exports) {
log('Preparing @celo/abis package.json')
const packageJsonPath = path.join(ABIS_PACKAGE_SRC_DIR, 'package.json')

replacePackageVersionAndMakePublic(packageJsonPath, (json) => {
log('Setting @celo/abis exports')
json.exports = exports
})
log('Skipping @celo/abis package.json preparation (no RELEASE_VERSION provided)')
}

function prepareContractsPackage() {
Expand Down
Loading

0 comments on commit 3cd4060

Please sign in to comment.