Truffle

如何在 Gitlab CI 中執行 Truffle Solidity 測試?

  • January 8, 2022

這是我的 .gitlab-ci.yml 文件,靈感來自@GrandFleet 的回答

image: node:latest
 
cache:
 paths:
 - node_modules/

stages:
 - test

test_async:
 stage: test
 services:
  - trufflesuite/ganache-cli
 script:
  - npm install truffle
  - ./node_modules/truffle/build/cli.bundled.js compile
  - ./node_modules/truffle/build/cli.bundled.js migrate --network gitlab
  - ./node_modules/truffle/build/cli.bundled.js test --network gitlab

當我在 Gitlab 上執行它時,我得到這個錯誤./node_modules/truffle/build/cli.bundled.js migrate --network gitlab

$ ./node_modules/truffle/build/cli.bundled.js migrate --network gitlab
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Unknown network "gitlab". See your Truffle configuration file for available networks.
Truffle v5.4.25 (core: 5.4.25)
Node v17.2.0
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

我應該在我的 truffle-config.js 中建立一個 Gitlab 網路嗎?如果是這樣,怎麼辦?

編輯:

這是我的 truffle-config.js 文件:

module.exports = {
 networks: {
   development: {
    host: "127.0.0.1",     // Localhost (default: none)
    port: 7545,            // Standard Ethereum port (default: none)
    network_id: "*",       // Any network (default: none)
   },
 },

 // Configure your compilers
 compilers: {
   solc: {
     version: "0.8.10",    // Fetch exact version from solc-bin (default: truffle's version)
     // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
     settings: {          // See the solidity docs for advice about optimization and evmVersion
      optimizer: {
        enabled: false,
        runs: 200
      },
     }
   }
 }
};

要在 Gitlab CI 上執行測試,我們需要使用像 Ropsten、Kovan 或 Rinkeby 這樣的測試網路。在 truffle-config.js 中,在網路中,添加(如果使用 Ropsten,在這種情況下):

ropsten: {
     provider: () => {
       return new HDWalletProvider(mnemonic, rpc_url);
     },
     network_id: '3',
   }

.gitlab-ci.yml 文件應如下所示:

image: node:latest
 
cache:
 paths:
 - node_modules/

stages:
 - test

test_async:
 stage: test
 services:
  - trufflesuite/ganache-cli
 script:
  - echo "MNEMONIC"=$MNEMONIC >> ".env"
  - export NODE_OPTIONS=--openssl-legacy-provider
  - npm install truffle
  - ./node_modules/truffle/build/cli.bundled.js compile
  - ./node_modules/truffle/build/cli.bundled.js migrate --network ropsten
  - ./node_modules/truffle/build/cli.bundled.js test --network ropsten

其中 –network ropsten 指的是 truffle-config.js 中定義的“ropsten”網路。

我在本教程中編寫了更詳細的解決方案:https ://blog.benjaminazoulay.com/how-to-run-truffle-tests-in-gitlab-ci

引用自:https://ethereum.stackexchange.com/questions/116792