1+ name : Chain Simulator Build and Integration Test
2+
3+ on :
4+ pull_request :
5+ branches :
6+ - ' main'
7+ - ' master'
8+ - ' rc/*'
9+ workflow_dispatch :
10+ issue_comment :
11+ types : [created]
12+
13+ permissions :
14+ issues : write
15+ pull-requests : write
16+ contents : read
17+
18+ jobs :
19+ build-and-test :
20+ if : |
21+ github.event_name == 'pull_request' ||
22+ (github.event_name == 'issue_comment' && contains(github.event.comment.body, 'Run Tests:')) ||
23+ github.event_name == 'workflow_dispatch'
24+
25+ strategy :
26+ matrix :
27+ # TODO Include Macos support later on
28+ runs-on : [ubuntu-latest]
29+ runs-on : ${{ matrix.runs-on }}
30+ env :
31+ BRANCH_NAME : ${{ github.head_ref || github.ref_name }}
32+ TARGET_BRANCH : " "
33+ MX_CHAIN_GO_TARGET_BRANCH : " "
34+ MX_CHAIN_SIMULATOR_TARGET_BRANCH : " "
35+ MX_CHAIN_TESTING_SUITE_TARGET_BRANCH : " "
36+
37+ steps :
38+ - name : Determine Target Branches
39+ id : target_branch
40+ run : |
41+ echo "CURRENT_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}" >> $GITHUB_ENV
42+
43+ # Default target branches based on the PR base branch
44+ if [[ "${{ github.event.pull_request.base.ref }}" == "main" ]]; then
45+ echo "MX_CHAIN_SIMULATOR_TARGET_BRANCH=main" >> $GITHUB_ENV
46+ echo "MX_CHAIN_TESTING_SUITE_TARGET_BRANCH=main" >> $GITHUB_ENV
47+ elif [[ "${{ github.event.pull_request.base.ref }}" == "master" ]]; then
48+ echo "MX_CHAIN_SIMULATOR_TARGET_BRANCH=main" >> $GITHUB_ENV
49+ echo "MX_CHAIN_TESTING_SUITE_TARGET_BRANCH=main" >> $GITHUB_ENV
50+ else
51+ echo "MX_CHAIN_SIMULATOR_TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
52+ echo "MX_CHAIN_TESTING_SUITE_TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
53+ fi
54+
55+ # Always set MX_CHAIN_GO_TARGET_BRANCH based on the PR base branch
56+ echo "MX_CHAIN_GO_TARGET_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
57+
58+
59+ - name : Fetch and Parse Last Comment for Branches
60+ uses : actions/github-script@v7
61+ id : fetch_and_parse_last_comment
62+ with :
63+ github-token : ${{ secrets.GITHUB_TOKEN }}
64+ script : |
65+ // Get the latest comment
66+ const comments = await github.rest.issues.listComments({
67+ owner: context.repo.owner,
68+ repo: context.repo.repo,
69+ issue_number: context.issue.number,
70+ });
71+
72+ const lastComment = comments.data.pop(); // Get the last comment
73+
74+ if (lastComment && lastComment.body.includes('Run Tests:')) {
75+ const body = lastComment.body.trim();
76+ core.setOutput('latest_comment', body);
77+
78+ // Parse the branches from the last comment
79+ const simulatorBranchMatch = body.match(/mx-chain-simulator-go:\s*(\S+)/);
80+ const testingSuiteBranchMatch = body.match(/mx-chain-testing-suite:\s*(\S+)/);
81+
82+ // Override the target branches if specified
83+ if (simulatorBranchMatch) {
84+ core.exportVariable('MX_CHAIN_SIMULATOR_TARGET_BRANCH', simulatorBranchMatch[1]);
85+ }
86+ if (testingSuiteBranchMatch) {
87+ core.exportVariable('MX_CHAIN_TESTING_SUITE_TARGET_BRANCH', testingSuiteBranchMatch[1]);
88+ }
89+ } else {
90+ core.info('The last comment does not contain "Run Tests:". Skipping branch override.');
91+ }
92+
93+
94+ - name : Print Target Branches
95+ run : |
96+ echo "Current branch mx-chain-go: ${{ env.CURRENT_BRANCH }}"
97+ echo "mx-chain-go target branch: ${{ env.MX_CHAIN_GO_TARGET_BRANCH }}"
98+ echo "mx-chain-simulator-go target branch: ${{ env.MX_CHAIN_SIMULATOR_TARGET_BRANCH }}"
99+ echo "mx-chain-testing-suite target branch: ${{ env.MX_CHAIN_TESTING_SUITE_TARGET_BRANCH }}"
100+
101+ - name : Set up Go 1.20.7
102+ uses : actions/setup-go@v3
103+ with :
104+ go-version : 1.20.7
105+ id : go
106+
107+ - name : Checkout mx-chain-go
108+ uses : actions/checkout@v4
109+ with :
110+ repository : ' multiversx/mx-chain-go'
111+ ref : ${{ github.head_ref }}
112+ fetch-depth : 0
113+ path : ' mx-chain-go'
114+
115+ - name : Get Latest mx-chain-go Commit Hash
116+ run : |
117+ cd mx-chain-go
118+ current_branch=$(git symbolic-ref --short HEAD)
119+ echo "CURRENT_BRANCH=${current_branch}" >> $GITHUB_ENV
120+ git fetch origin ${current_branch} --prune
121+ latest_commit_hash=$(git rev-parse origin/${current_branch})
122+ echo "LATEST_COMMIT_HASH=${latest_commit_hash}" >> $GITHUB_ENV
123+ echo "Latest commit hash: ${latest_commit_hash}"
124+
125+ - name : Checkout mx-chain-simulator-go
126+ uses : actions/checkout@v4
127+ with :
128+ repository : ' multiversx/mx-chain-simulator-go'
129+ ref : ${{ env.MX_CHAIN_SIMULATOR_TARGET_BRANCH || github.event.pull_request.base.ref }}
130+ path : ' mx-chain-simulator-go'
131+
132+ - name : Set up Python 3.10
133+ uses : actions/setup-python@v2
134+ with :
135+ python-version : ' 3.10'
136+
137+ - name : Install Python Dependencies and Update go.mod
138+ run : |
139+ cd mx-chain-simulator-go
140+ pip install -r scripts/update-go-mod/requirements.txt
141+ python scripts/update-go-mod/update-go-mod.py $LATEST_COMMIT_HASH
142+
143+ - name : Run go build
144+ run : |
145+ cd mx-chain-simulator-go/cmd/chainsimulator
146+ go build
147+ echo "CHAIN_SIMULATOR_BUILD_PATH=$(pwd)" >> $GITHUB_ENV
148+
149+ - name : Checkout mx-chain-testing-suite
150+ uses : actions/checkout@v4
151+ with :
152+ repository : ' multiversx/mx-chain-testing-suite'
153+ path : ' mx-chain-testing-suite'
154+ fetch-depth : 0
155+ ref : ${{ env.MX_CHAIN_TESTING_SUITE_TARGET_BRANCH || github.event.pull_request.base.ref }}
156+ token : ${{ secrets.MVX_TESTER_GH_TOKEN }}
157+
158+ - name : Install Dependencies
159+ run : |
160+ pip install -r mx-chain-testing-suite/requirements.txt
161+ echo "PYTHONPATH=mx-chain-testing-suite" >> $GITHUB_ENV
162+
163+
164+ - name : Run tests and generate HTML report
165+ run : |
166+ set +e
167+ pytest mx-chain-testing-suite/scenarios/ --html=report.html --self-contained-html --continue-on-collection-errors
168+ PYTEST_EXIT_CODE=$?
169+ set -e
170+ echo "PYTEST_EXIT_CODE=$PYTEST_EXIT_CODE" >> $GITHUB_ENV
171+ echo "Pytest exit code: $PYTEST_EXIT_CODE"
172+ if [ -f "report.html" ]; then
173+ echo "Report generated successfully."
174+ mkdir -p ./reports
175+ mv report.html ./reports/
176+ else
177+ echo "Report not found."
178+ fi
179+
180+ - name : Upload test report
181+ if : always()
182+ uses : actions/upload-artifact@v3
183+ with :
184+ name : pytest-report-${{ github.run_id }}
185+ path : reports/report.html
186+
187+ - name : Deploy Report to GitHub Pages
188+ if : always()
189+ id : deploy_report
190+ run : |
191+ # Navigate to the mx-chain-testing-suite directory
192+ cd mx-chain-testing-suite
193+
194+ # Configure Git user
195+ git config user.name "GitHub Action"
196+ git config user.email "action@github.com"
197+
198+ # Check if the report exists
199+ if [ -f "../reports/report.html" ]; then
200+ # Ensure we're on the 'gh-pages' branch and up to date
201+ git fetch --all
202+ git checkout gh-pages || git checkout --orphan gh-pages
203+
204+ # Create a new directory for the report based on the current timestamp
205+ TIMESTAMP=$(date +'%d%m%Y-%H%M%S')
206+ echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV
207+ REPORT_DIR="reports/${BRANCH_NAME}/${TIMESTAMP}"
208+ mkdir -p $REPORT_DIR
209+
210+ # Move the report into the new directory
211+ cp ../reports/report.html $REPORT_DIR/index.html
212+
213+ # Add and commit only the new report
214+ git add $REPORT_DIR/index.html
215+ git commit -m "Deploy Test Report at $BRANCH_NAME/$TIMESTAMP"
216+
217+ # Set remote URL with authentication token
218+ git remote set-url origin https://x-access-token:${{ secrets.MVX_TESTER_GH_TOKEN }}@github.com/multiversx/mx-chain-testing-suite.git
219+
220+ # Push changes to the remote 'gh-pages' branch
221+ git push --force origin gh-pages
222+ else
223+ echo "Report file not found, skipping deployment."
224+ fi
225+
226+
227+ - name : Update Index Page
228+ if : always()
229+ run : |
230+ cd mx-chain-testing-suite
231+ git fetch --all
232+ git checkout gh-pages || git checkout --orphan gh-pages
233+ if [ -d "docs" ]; then
234+ cd docs
235+ echo "<html><body><h1>Test Reports</h1><ul>" > index.html
236+ for report in $(ls ../reports); do
237+ echo "<li><a href='../reports/$report/index.html'>Report - $report</a></li>" >> index.html
238+ done
239+ echo "</ul></body></html>" >> index.html
240+ git add index.html
241+ git commit -m "Update Index of Reports"
242+ git push origin gh-pages --force
243+ else
244+ mkdir -p docs
245+ cd docs
246+ echo "<html><body><h1>Test Reports</h1><ul>" > index.html
247+ echo "</ul></body></html>" >> index.html
248+ echo "Docs directory was not found and has been created."
249+ fi
250+
251+ - name : Comment PR with report link or error message
252+ if : always()
253+ uses : actions/github-script@v7
254+ env :
255+ TIMESTAMP : ${{ env.TIMESTAMP }}
256+ BRANCH_NAME : ${{ env.BRANCH_NAME }}
257+ CURRENT_BRANCH : ${{ env.CURRENT_BRANCH }}
258+ MX_CHAIN_GO_TARGET_BRANCH : ${{ env.MX_CHAIN_GO_TARGET_BRANCH }}
259+ MX_CHAIN_SIMULATOR_TARGET_BRANCH : ${{ env.MX_CHAIN_SIMULATOR_TARGET_BRANCH }}
260+ MX_CHAIN_TESTING_SUITE_TARGET_BRANCH : ${{ env.MX_CHAIN_TESTING_SUITE_TARGET_BRANCH }}
261+ LATEST_COMMIT_HASH : ${{ env.LATEST_COMMIT_HASH }}
262+ PYTEST_EXIT_CODE : ${{ env.PYTEST_EXIT_CODE }}
263+
264+ with :
265+ github-token : ${{ secrets.GITHUB_TOKEN }}
266+ script : |
267+ const timestamp = process.env.TIMESTAMP;
268+ const branchName = process.env.BRANCH_NAME;
269+ const currentBranch = process.env.CURRENT_BRANCH;
270+ const goTargetBranch = process.env.MX_CHAIN_GO_TARGET_BRANCH;
271+ const simulatorTargetBranch = process.env.MX_CHAIN_SIMULATOR_TARGET_BRANCH;
272+ const testingSuiteTargetBranch = process.env.MX_CHAIN_TESTING_SUITE_TARGET_BRANCH;
273+ const commitHash = process.env.LATEST_COMMIT_HASH;
274+ const exitCode = process.env.PYTEST_EXIT_CODE;
275+ const issue_number = context.issue.number;
276+ const owner = context.repo.owner;
277+ const repo = context.repo.repo;
278+ let message;
279+
280+ if (timestamp && branchName && timestamp !== "" && branchName !== "") {
281+ const reportUrl = `https://multiversx.github.io/mx-chain-testing-suite/reports/${branchName}/${timestamp}/index.html`;
282+ message = `
283+ 📊 **MultiversX Automated Test Report:** [View Report](${reportUrl})
284+
285+ 🔄 **Build Details:**
286+ - **mx-chain-go Commit Hash:** \`${commitHash}\`
287+ - **Current Branch:** \`${currentBranch}\`
288+ - **mx-chain-go Target Branch:** \`${goTargetBranch}\`
289+ - **mx-chain-simulator-go Target Branch:** \`${simulatorTargetBranch}\`
290+ - **mx-chain-testing-suite Target Branch:** \`${testingSuiteTargetBranch}\`
291+
292+ 🚀 **Environment Variables:**
293+ - **TIMESTAMP:** \`${timestamp}\`
294+ - **PYTEST_EXIT_CODE:** \`${exitCode}\`
295+ 🎉 **MultiversX CI/CD Workflow Complete!**
296+ `;
297+ } else {
298+ message = "⚠️ No report was generated due to an error or cancellation of the process.\nPlease checkout gh action logs for details";
299+ }
300+
301+ github.rest.issues.createComment({
302+ issue_number: issue_number,
303+ owner: owner,
304+ repo: repo,
305+ body: message
306+ });
307+
308+ - name : Fail job if tests failed
309+ if : always()
310+ run : |
311+ if [ "${{ env.PYTEST_EXIT_CODE }}" != "0" ]; then
312+ echo "Tests failed with exit code ${{ env.PYTEST_EXIT_CODE }}"
313+ exit 1
314+ else
315+ echo "Tests passed successfully."
316+ fi
0 commit comments