Build Contracts #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Contracts | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| contractName: | |
| description: 'Enter Contract Name' | |
| required: true | |
| default: 'RandomContract' | |
| type: choice | |
| options: | |
| - 'RandomContract' | |
| version: | |
| description: 'Enter Contract Version' | |
| required: true | |
| default: '1.0.0' | |
| netsdk: | |
| description: 'Select .NET SDK Version' | |
| required: true | |
| default: '8.0' | |
| type: choice | |
| options: | |
| - '8.0' | |
| env: | |
| DOTNET_INSTALL_DIR: "./.dotnet" | |
| concurrency: | |
| group: workflow-${{ github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| prepare-matrix: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.set-matrix.outputs.matrix }} | |
| steps: | |
| - name: Generate Dynamic Matrix | |
| id: set-matrix | |
| run: | | |
| echo "[\"${{ github.event.inputs.contractName }}\"]" > matrix.json | |
| echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT | |
| build-contract: | |
| runs-on: ubuntu-latest | |
| needs: prepare-matrix | |
| strategy: | |
| matrix: | |
| contractName: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }} | |
| steps: | |
| - name: Debug Matrix Inputs | |
| run: | | |
| echo "The contract name from matrix is: ${{ matrix.contractName }}" | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v3 | |
| with: | |
| dotnet-version: '8.0.x' | |
| - name: Set Variables | |
| run: | | |
| VERSION="${{ github.event.inputs.version }}" | |
| NETSDK="${{ github.event.inputs.netsdk }}" | |
| HOME_DIR="${{ github.workspace }}/contracts_auto_build/${{ matrix.contractName }}" | |
| case "${NETSDK}" in | |
| "9.0") | |
| IMAGE_NAME="aelf/build-contracts:dotnet-sdk-9.0-noble-amd64" | |
| ;; | |
| "8.0") | |
| IMAGE_NAME="aelf/build-contracts:dotnet-sdk-8.0.204-amd64" | |
| ;; | |
| *) | |
| echo "Error: Unsupported .NET SDK version selected." | |
| exit 1 | |
| ;; | |
| esac | |
| echo "VERSION=${VERSION}" >> $GITHUB_ENV | |
| echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV | |
| echo "HOME_DIR=${HOME_DIR}" >> $GITHUB_ENV | |
| - name: Build Selected Contract(s) | |
| run: | | |
| # Check if HOME_DIR is set | |
| if [ -z "${{ env.HOME_DIR }}"]; then | |
| echo "HOME_DIR environment variable is not set" | |
| exit 1 | |
| fi | |
| # Create build directory if it doesn't exist | |
| if [ ! -d "${{ env.HOME_DIR }}/build" ]; then | |
| mkdir -p "${{ env.HOME_DIR }}/build" | |
| fi | |
| # Create contracts directory if it doesn't exist | |
| if [ ! -d "${{ env.HOME_DIR }}/contracts" ]; then | |
| mkdir -p "${{ env.HOME_DIR }}/contracts" | |
| fi | |
| echo "Copying contracts for ${{ env.HOME_DIR }}/contracts/" | |
| for item in *; do | |
| if [ "$item" != "contracts_auto_build" ] && [ "$item" != "$(basename "${HOME_DIR}/contracts/")" ]; then | |
| cp -r "$item" "${HOME_DIR}/contracts/" | |
| fi | |
| done | |
| echo "Copying build.sh to ${{ env.HOME_DIR }}/build.sh" | |
| if [ -f "build.sh" ]; then | |
| cp build.sh "${{ env.HOME_DIR }}/build.sh" | |
| else | |
| echo "build.sh not found in the current directory." | |
| exit 1 | |
| fi | |
| - name: Build Contract | |
| run: | | |
| echo "Building contract: ${{ matrix.contractName }}" | |
| cd ${{ env.HOME_DIR }} | |
| docker run --rm --name build-contracts \ | |
| -e USER=root \ | |
| -v ${{ env.HOME_DIR }}/contracts:/opt/contracts \ | |
| -v ${{ env.HOME_DIR }}/build:/opt/build \ | |
| -v ${{ env.HOME_DIR }}/build.sh:/opt/build.sh \ | |
| ${{ env.IMAGE_NAME }} /bin/bash -x /opt/build.sh \ | |
| ${{ matrix.contractName }} ${{ env.VERSION }} | |
| - name: Archive Build Contract | |
| run: | | |
| cd ${{ env.HOME_DIR }} | |
| tar cfz ${{ matrix.contractName }}.${{ env.VERSION }}.tgz -C build . | |
| - name: Upload build contract | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ matrix.contractName }}-contract | |
| path: ${{ env.HOME_DIR }}/${{ matrix.contractName }}.${{ env.VERSION }}.tgz | |
| retention-days: 7 |