Build and Postman Tests #11
This file contains 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 and Postman Tests | |
on: | |
workflow_dispatch: | |
inputs: | |
Iterations: | |
description: 'Iteration count for tests:' | |
required: false | |
default: 1 | |
type: number | |
Query: | |
description: 'Search parameter for all API request:' | |
required: true | |
default: 'The Rookie' | |
type: string | |
QueryAllPage: | |
description: 'Parameter for search on all pages:' | |
required: true | |
default: 'test' | |
type: string | |
Url: | |
description: 'Local or Publish server:' | |
required: false | |
default: 'http://localhost:8443' | |
type: choice | |
options: | |
- 'http://localhost:8443' | |
- 'https://torapi.vercel.app' | |
- 'https://toruapi.vercel.app' | |
- 'https://rutorapi.vercel.app' | |
Docker: | |
description: 'Build Docker image from:' | |
required: false | |
default: 'not' | |
type: choice | |
options: | |
- 'not' | |
- 'GitHub' | |
- 'DockerHub' | |
# Docker: | |
# description: 'Build docker image from GitHub' | |
# required: false | |
# default: false | |
# type: boolean | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Clone repository | |
uses: actions/checkout@v4 | |
- name: Install Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Cache npm dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Install newman | |
run: | | |
npm install -g newman | |
npm install -g newman-reporter-html | |
- name: Packet check | |
run: | | |
echo "node version: $(node --version)" | |
echo "npm version: $(npm --version)" | |
echo "newman version: $(newman --version)" | |
- name: Build docker image and run container | |
if: ${{ github.event.inputs.Url == 'http://localhost:8443' && github.event.inputs.Docker != 'not' }} | |
run: | | |
if [ "${{ github.event.inputs.Docker }}" == "GitHub" ]; then | |
echo "Docker build from GitHub" | |
docker build -t lifailon/torapi:latest . | |
docker run -d --name TorAPI -p 8443:8443 lifailon/torapi:latest | |
elif [ "${{ github.event.inputs.Docker }}" == "DockerHub" ]; then | |
echo "Docker build from Docker Hub" | |
docker run -d --name TorAPI -p 8443:8443 --restart=unless-stopped lifailon/torapi:latest | |
fi | |
for i in {1..30}; do | |
response_code=$(curl -o /dev/null -s -w '%{http_code}' http://localhost:8443/api/provider/list 2> /dev/null || echo "404") | |
if [ "$response_code" -eq 200 ]; then | |
echo "Server is up: status code 200" | |
break | |
else | |
echo "Waiting for the server to start: $(($i*2)) seconds..." | |
sleep 2 | |
fi | |
done | |
if [ "$response_code" -ne 200 ]; then | |
exit 1 | |
fi | |
- name: Install dependencies and start local server | |
if: ${{ github.event.inputs.Url == 'http://localhost:8443' && github.event.inputs.Docker == 'not' }} | |
run: | | |
npm install | |
npm start > torapi.log & | |
- name: Run postman tests | |
run: | | |
newman run postman-tests.json \ | |
--iteration-count ${{ github.event.inputs.Iterations }} \ | |
--env-var "query=${{ github.event.inputs.Query }}" \ | |
--env-var "queryAllPage=${{ github.event.inputs.queryAllPage }}" \ | |
--env-var "baseUrl=${{ github.event.inputs.Url }}" \ | |
--reporters cli,junit,html \ | |
--reporter-html-export postman-report.html \ | |
--reporter-junit-export postman-results.xml | |
- name: View local server logs | |
if: ${{ github.event.inputs.Url == 'http://localhost:8443' && (success() || failure()) }} | |
run: | | |
if [ "${{ github.event.inputs.Docker }}" == "not" ]; then | |
cat torapi.log | |
else | |
docker logs TorAPI | |
fi | |
- name: Upload Newman HTML Report | |
uses: actions/upload-artifact@v4 | |
if: success() || failure() | |
with: | |
name: Postman HTML Report | |
path: postman-report.html | |
- name: Publish JUnit Test Results | |
uses: mikepenz/action-junit-report@v4 | |
if: success() || failure() | |
with: | |
report_paths: '**/postman-results.xml' |