HomeArtTechHackBlockchain

ใช้ Github Action build Coverage บน Sonarcloud.io

By Khomkrid Lerdprasert
Published in Technology
August 16, 2021
1 min read
ใช้ Github Action build Coverage บน Sonarcloud.io

ใช้ Github Action build Coverage เพื่อทำ test และ scan code ของเราบน Sonarcloud.io กันเถอะ

ช่วงนี้ต้องมาเขียน code บ่อยขึ้น ประกอบกับช่วงก่อนหน้านี้ covid ครึ่งปีแรก ไปนั่งทำ lab owasp top 10 เล่น พอมีเวลาก็เขียน go project ไว้ใช้งานเล่นด้วย พยายามเขียน unit test ให้ครอบคลุมไปด้วยทุกครั้ง และทุกครั้งที่ commit Sonarcloud.io จะ scan code ของเรา ว่า

Security เป็นยังไง Reliability เป็นไงบ้าง Code Smells เป็นไงบ้าง ใช้เวลาแก้เท่าไหร่ Coverage Unit Tests เท่าไหร่

ทั้งหมดนี่ ฟรี!! สำหรับ public repo และเสียเงินสำหรับ private repo

Sonarcloud.io
Sonarcloud.io

หลังจากนั้นเราไปที่ github repo ของเรา แล้วเลือกที่ menu setting เพื่อทำการตั้งค่า Sonarcloud.io token key

Key
Key

โดยเจ้า sonarcloud token key เนี่ยเราจะหาได้จาก ตามรูปเลย

Key
Key

ตอนนี้ โครงสร้าง repo ของเราจะเป็นประมาณนี้

.
├── Readme.md
├── conf
├── database
├── docker-compose.yml
├── redis
└── services

โดย code ที่เราจะทำการ scan จะอยู่ใน services directory

ต่อไปเราจะไปทำการสร้าง github action ขึ้นมา โดยจะให้ทำการ ลง golang และทำการ run test coverage ใน services ให้ได้ออกมาเป็น coverage.out และ report.json เพื่อนำไปใช้ใน Sonarcloud.io กัน

โดยเราจะสร้างไว้ที่ .github/workflows โดยผมมีการ debug ดู path file ไว้ที่ fix code coverage paths ว่าถูกต้องไหม

name: Build
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- name: Setup golang
uses: actions/setup-go@v2
with:
go-version: '^1.16.5'
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
cd ./services && go mod tidy
- name: Go Run Test
run: |
cd ./services && go test "./..." -coverprofile="coverage.out" -covermode=count -json > report.json
- name: fix code coverage paths
working-directory: ./services
run: |
pwd
ls -la
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
with:
projectBaseDir: ./services
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

จากนั้น จะไปสร้าง config ให้ Sonarcloud.io ใน services ชื่อว่า sonar-project.properties เพื่อตั้งค่า project ของเรา และกำหนด inclusions และ exclusions file ที่จะทำการ scan

sonar.projectKey=aofiee_golang-clean-architecture
sonar.organization=aofiee
sonar.sources=.
sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/*,**/*.html,**/mocks/*.go
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**,**/mocks/*.go
sonar.go.coverage.reportPaths=coverage.out
sonar.go.tests.reportPaths=report.json

หลังจากนั้นเราทำการ commit code และ push ขึ้นไปที่ branch main ระบบจะทำการ scan code และ แสดง report ออกมาดังตัวอย่าง

report
report

โดย Sonarcloud.io จะทำการ แสกนให้เราทุก commit ก่อนทำการ merge ลง master

report
report

report
report


Tags

#sonarcloud#github#golang

Share

Previous Article
มาทดลองทำ OCR Scan ดึง Text ออกมาจากรูปภาพง่ายๆกันดีกว่า
Khomkrid Lerdprasert

Khomkrid Lerdprasert

Full Stack Life

Related Posts

Docker compose ทำ mongodb มาเชื่อมต่อ กับ golang
March 05, 2022
1 min
© 2024, All Rights Reserved.
Powered By

Quick Links

Author

Social Media