Skip to content

Commit 57845ef

Browse files
committed
chore: add GitHub Actions
1 parent 1e41c0c commit 57845ef

File tree

7 files changed

+234
-4
lines changed

7 files changed

+234
-4
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
name: Build Contracts
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
contractName:
7+
description: 'Enter Contract Name'
8+
required: true
9+
default: 'RandomContract'
10+
type: choice
11+
options:
12+
- 'RandomContract'
13+
version:
14+
description: 'Enter Contract Version'
15+
required: true
16+
default: '1.0.0'
17+
netsdk:
18+
description: 'Select .NET SDK Version'
19+
required: true
20+
default: '8.0'
21+
type: choice
22+
options:
23+
- '8.0'
24+
25+
env:
26+
DOTNET_INSTALL_DIR: "./.dotnet"
27+
28+
concurrency:
29+
group: workflow-${{ github.ref }}
30+
cancel-in-progress: true
31+
32+
jobs:
33+
prepare-matrix:
34+
runs-on: ubuntu-latest
35+
outputs:
36+
matrix: ${{ steps.set-matrix.outputs.matrix }}
37+
steps:
38+
- name: Generate Dynamic Matrix
39+
id: set-matrix
40+
run: |
41+
echo "[\"${{ github.event.inputs.contractName }}\"]" > matrix.json
42+
echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT
43+
44+
build-contract:
45+
runs-on: ubuntu-latest
46+
needs: prepare-matrix
47+
strategy:
48+
matrix:
49+
contractName: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }}
50+
51+
steps:
52+
- name: Debug Matrix Inputs
53+
run: |
54+
echo "The contract name from matrix is: ${{ matrix.contractName }}"
55+
56+
- name: Checkout Code
57+
uses: actions/checkout@v4
58+
59+
- name: Setup .NET
60+
uses: actions/setup-dotnet@v3
61+
with:
62+
dotnet-version: '8.0.x'
63+
64+
- name: Set Variables
65+
run: |
66+
VERSION="${{ github.event.inputs.version }}"
67+
NETSDK="${{ github.event.inputs.netsdk }}"
68+
HOME_DIR="${{ github.workspace }}/contracts_auto_build/${{ matrix.contractName }}"
69+
70+
case "${NETSDK}" in
71+
"9.0")
72+
IMAGE_NAME="aelf/build-contracts:dotnet-sdk-9.0-noble-amd64"
73+
;;
74+
"8.0")
75+
IMAGE_NAME="aelf/build-contracts:dotnet-sdk-8.0.204-amd64"
76+
;;
77+
*)
78+
echo "Error: Unsupported .NET SDK version selected."
79+
exit 1
80+
;;
81+
esac
82+
83+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
84+
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV
85+
echo "HOME_DIR=${HOME_DIR}" >> $GITHUB_ENV
86+
87+
- name: Build Selected Contract(s)
88+
run: |
89+
# Check if HOME_DIR is set
90+
if [ -z "${{ env.HOME_DIR }}"]; then
91+
echo "HOME_DIR environment variable is not set"
92+
exit 1
93+
fi
94+
95+
# Create build directory if it doesn't exist
96+
if [ ! -d "${{ env.HOME_DIR }}/build" ]; then
97+
mkdir -p "${{ env.HOME_DIR }}/build"
98+
fi
99+
100+
# Create contracts directory if it doesn't exist
101+
if [ ! -d "${{ env.HOME_DIR }}/contracts" ]; then
102+
mkdir -p "${{ env.HOME_DIR }}/contracts"
103+
fi
104+
105+
echo "Copying contracts for ${{ env.HOME_DIR }}/contracts/"
106+
107+
for item in *; do
108+
if [ "$item" != "contracts_auto_build" ] && [ "$item" != "$(basename "${HOME_DIR}/contracts/")" ]; then
109+
cp -r "$item" "${HOME_DIR}/contracts/"
110+
fi
111+
done
112+
113+
echo "Copying build.sh to ${{ env.HOME_DIR }}/build.sh"
114+
115+
if [ -f "build.sh" ]; then
116+
cp build.sh "${{ env.HOME_DIR }}/build.sh"
117+
else
118+
echo "build.sh not found in the current directory."
119+
exit 1
120+
fi
121+
122+
- name: Build Contract
123+
run: |
124+
echo "Building contract: ${{ matrix.contractName }}"
125+
cd ${{ env.HOME_DIR }}
126+
docker run --rm --name build-contracts \
127+
-e USER=root \
128+
-v ${{ env.HOME_DIR }}/contracts:/opt/contracts \
129+
-v ${{ env.HOME_DIR }}/build:/opt/build \
130+
-v ${{ env.HOME_DIR }}/build.sh:/opt/build.sh \
131+
${{ env.IMAGE_NAME }} /bin/bash -x /opt/build.sh \
132+
${{ matrix.contractName }} ${{ env.VERSION }}
133+
134+
- name: Archive Build Contract
135+
run: |
136+
cd ${{ env.HOME_DIR }}
137+
tar cfz ${{ matrix.contractName }}.${{ env.VERSION }}.tgz -C build .
138+
139+
- name: Upload build contract
140+
uses: actions/upload-artifact@v4
141+
with:
142+
name: ${{ matrix.contractName }}-contract
143+
path: ${{ env.HOME_DIR }}/${{ matrix.contractName }}.${{ env.VERSION }}.tgz
144+
retention-days: 7
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Test with code coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
8+
env:
9+
DOTNET_INSTALL_DIR: "./.dotnet"
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
pull-requests: write
16+
contents: write
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
- name: Setup dotnet
21+
uses: actions/setup-dotnet@v4
22+
with:
23+
dotnet-version: 8.0.100
24+
- name: chown
25+
run: |
26+
sudo chown -R $USER:$USER /home/runneradmin
27+
- name: Install dependencies
28+
run: dotnet restore test/RandomContract.Tests.csproj --verbosity quiet
29+
30+
- name: Build
31+
run: dotnet build test/RandomContract.Tests.csproj --no-restore /clp:ErrorsOnly /p:GeneratePackageOnBuild=false --verbosity quiet
32+
33+
- name: Test
34+
run: |
35+
for name in `ls ./test/*.Tests.csproj | awk '{print $NF}'`;
36+
do
37+
dotnet test ${name} --no-restore --no-build --logger trx --settings CodeCoverage.runsettings --results-directory coverage --collect:"XPlat Code Coverage"
38+
done
39+
40+
- name: Upload coverage reports to Codecov
41+
uses: codecov/codecov-action@v4
42+
with:
43+
fail_ci_if_error: true
44+
files: coverage/*/coverage.cobertura.xml
45+
env:
46+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,7 @@ paket-files/
257257
CodeCoverage
258258

259259
*.dll
260+
261+
#contract
262+
contract_csharp_plugin
263+
contract_csharp_plugin-v1.0.3-linux.zip

CodeCoverage.runsettings

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- File name extension must be .runsettings -->
3+
<RunSettings>
4+
<DataCollectionRunSettings>
5+
<DataCollectors>
6+
<DataCollector friendlyName="XPlat code coverage">
7+
<Configuration>
8+
<Format>cobertura</Format>
9+
<Exclude>[xunit.*]*,[*Tests]*</Exclude> <!-- [Assembly-Filter]Type-Filter -->
10+
<ExcludeByFile>**/test/**/*.cs,</ExcludeByFile>
11+
<ExcludeByAttribute>Obsolete,GeneratedCodeAttribute</ExcludeByAttribute>
12+
<SingleHit>false</SingleHit>
13+
</Configuration>
14+
</DataCollector>
15+
</DataCollectors>
16+
</DataCollectionRunSettings>
17+
</RunSettings>

build.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
ContractsName=$1
6+
VERSION=$2
7+
8+
[ -z ${VERSION} ] && { echo "Usage: $0 <version>"; exit 1; }
9+
10+
[ -d "/opt/build/${ContractsName}" ] && rm -rf /opt/build/${ContractsName}/*
11+
12+
cd /opt/contracts
13+
14+
dotnet build \
15+
src/${ContractsName}.csproj \
16+
/p:NoBuild=false \
17+
/p:Version=${VERSION} \
18+
-c Release \
19+
-o /opt/build/${ContractsName}-${VERSION}
20+

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
codecov:
2+
notify:
3+
after_n_builds: 1

src/RandomContract.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,5 @@
2323
</PackageReference>
2424
</ItemGroup>
2525

26-
<ItemGroup>
27-
<Folder Include="obj\Debug\net8.0\Protobuf\contract\" />
28-
</ItemGroup>
29-
3026
</Project>
3127

0 commit comments

Comments
 (0)