HomeArtTechHackBlockchain
ONLINE ·
Index
/
Technology
/
Article

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

Operator
Khomkrid Lerdprasert
Filed
August 16, 2021
Channel
Technology
Read
~1 min
ใช้ 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
Khomkrid Lerdprasert
Operator

Khomkrid Lerdprasert

Technical Lead — building AI-powered platforms, omni-channel chat systems, and telemedicine solutions with Go, Next.js & clean architecture. 20+ years shipping software from crypto wallets to e-learning systems. Bangkok-based. Writes code late at night, brews beer on weekends.

GithubInstagram
Previous · transmission
มาทดลองทำ OCR Scan ดึง Text ออกมาจากรูปภาพง่ายๆกันดีกว่า
Next · transmission
Gatsbyjs with cookie consent
Metadata
Channel
Technology
Filed
August 16, 2021
Read
~1 min
Language
TH / EN
Transmit

Related

สร้าง RAG Pipeline ด้วย Go + pgvector + Hexagonal Architecture
Khomkrid Lerdprasert
April 23, 2026
1 min
aofiee.dev
signal / noise / code · craft
© 2019 – 2026, Khomkrid Lerdprasert.
All transmissions logged.
No newsletter. No profiling. Cookies require consent.
PGP · 7F3D 2024 A21E B584 · 0x7F3D
Channels
  • Art & Culture
  • Technology
  • Hack 101
  • Blockchain 101
  • Archive / All posts
— END OF TRANSMISSION —
// powered by curiosity, coffee, & wuxia
BKK · 13°45′N · 100°30′E