Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
270b565
added sonar for SAST
bashizip Jun 12, 2025
ca8aa81
added sonar for SAST
bashizip Jun 12, 2025
eb6e815
added sonar for SAST, fixed typo
bashizip Jun 12, 2025
eb534e5
added the option CLOUD_LOGGING_ONLY
bashizip Jun 12, 2025
4dad629
fix var refs
bashizip Jun 12, 2025
b3448ac
fix var refs
bashizip Jun 12, 2025
c3bd2c2
refactor the config and add docs
bashizip Jun 12, 2025
02ae067
refactor the config and add docs
bashizip Jun 12, 2025
496f4d7
refactor the config and add docs 2
bashizip Jun 12, 2025
df51c3c
refactor the config and add docs 3
bashizip Jun 12, 2025
1519f37
updated cloudbuild docs and created a Secret Manager Accessor IAM pe…
bashizip Jun 14, 2025
3fa1bed
fix synthax error
bashizip Jun 14, 2025
98d0ae3
fix synthax error
bashizip Jun 14, 2025
1c8f93d
fix golang version format
bashizip Jun 14, 2025
a55cf1a
fix golang version in gomod
bashizip Jun 14, 2025
a8ae57d
fix snarqube project id
bashizip Jun 14, 2025
3dcd8af
debugging sonar token
bashizip Jun 14, 2025
01c5898
debugging sonar token
bashizip Jun 14, 2025
dade0f8
debugging sonar token
bashizip Jun 14, 2025
24e647b
debugging sonar token
bashizip Jun 14, 2025
02c5cd7
debugging sonar token
bashizip Jun 14, 2025
71da914
set sonar working directory
bashizip Jun 15, 2025
7c48f1a
set sonar working directory
bashizip Jun 15, 2025
a16ab96
prepare a sonarqube working dir with permission before running the scam
bashizip Jun 15, 2025
0bfc88a
factorize the project_id
bashizip Jun 15, 2025
7fadac0
added a code smell on purpose
bashizip Jun 18, 2025
d801907
added hardcoded creds by purpose
bashizip Jun 18, 2025
3931d84
make cloud build fail if sonar scan fais
bashizip Jun 18, 2025
8ba6167
fix var naming
bashizip Jun 18, 2025
23bf26d
fix typo
bashizip Jun 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added cloudbuild-debug.yaml
Empty file.
98 changes: 76 additions & 22 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,80 @@
# Cloud Build configuration to build a Go application and run a SonarQube scan.

options:
# Configure logging for the build.
# CLOUD_LOGGING_ONLY sends logs exclusively to Cloud Logging,
# useful for centralized log management.
logging: CLOUD_LOGGING_ONLY

steps:
# Step 1: Compile the Go Application
- name: 'gcr.io/cloud-builders/go'
id: 'Compile application'
env: ['GOPATH=/gopath']
args: ['build', '-o', 'main', 'main.go']
# Step 1: Build the Go application
# Uses the official (but old version by purpose) Golang Docker image to manage dependencies and compile the binary.
- name: 'golang:1.19'
entrypoint: 'sh'
args:
# 'go mod tidy' ensures module dependencies are consistent.
# 'go build -o opsgo' compiles the Go application into an executable named 'opsgo'.
- '-c'
- 'go mod tidy && go build -o opsgo'
id: 'build-go-app' # A unique identifier for this step, helpful for dependencies.

# Step 2: Build the Docker image for the Go application
- name: 'gcr.io/cloud-builders/docker'
id: 'Build Docker image'
args: ['build', '-t', '<your-region>-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:<version>', '.']
# Step 2.1: Prepare SonarQube working directory
# Creates the .sonar directory with proper permissions before running the scan.
- name: 'alpine'
entrypoint: 'sh'
args:
- '-c'
- 'mkdir -p .sonar && chmod 777 .sonar'
id: 'prepare-sonar-dir'

# Step 3: Push the Docker image to Artifact Registry
- name: 'gcr.io/cloud-builders/docker'
id: 'Push Docker image'
args: ['push', '<your-region>-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:<version>']
# Step 2.2: Run SonarQube analysis
# Uses the SonarScanner CLI Docker image to perform code analysis.
- name: 'docker.io/sonarsource/sonar-scanner-cli'
entrypoint: 'sonar-scanner'
args:
# Define the SonarQube project key.
- '-Dsonar.projectKey=opsgo-sonar'
# Specify the source directory for analysis (current directory).
- '-Dsonar.sources=.'
# Set the working directory for SonarQube (where temp files are stored).
- '-Dsonar.working.directory=.sonar'
# Provide the URL of your SonarQube server.
- '-Dsonar.host.url=http://34.10.176.217:9000'
# Pass the SonarQube authentication token.
# '$$SONAR_TOKEN' is automatically populated from the availableSecret.
- '-Dsonar.token=$$SONAR_TOKEN'
secretEnv: ['SONAR_TOKEN']
id: 'run-sonar-scan' # A unique identifier for this step.

# Step 4: Apply the production deployment YAML file to the production namespace
- name: 'gcr.io/cloud-builders/kubectl'
id: 'Deploy'
args: ['-n', 'prod', 'apply', '-f', 'prod/deployment.yaml']
env:
- 'CLOUDSDK_COMPUTE_REGION=<your-zone>'
- 'CLOUDSDK_CONTAINER_CLUSTER=hello-cluster'
options:
logging: CLOUD_LOGGING_ONLY
# Step 2.3: Verify SonarQube Quality Gate Status
# Checks if the SonarQube quality gate passed (fails build if not).
- name: 'curlimages/curl'
entrypoint: 'sh'
args:
- '-c'
- |
# Wait a few seconds for SonarQube to process the analysis.
sleep 10
# Fetch the quality gate status using the SonarQube API.
RESULT=$(curl -s -u "$$SONAR_TOKEN:" "http://34.10.176.217:9000/api/qualitygates/project_status?projectKey=opsgo-sonar" | jq -r '.projectStatus.status')
# Fail the build if the quality gate status is not "OK".
if [ "$$RESULT" != "OK" ]; then
echo "❌ SonarQube Quality Gate Failed: $$RESULT !!"
exit 1
else
echo "✅ SonarQube Quality Gate Passed !"
fi
secretEnv: ['SONAR_TOKEN']
id: 'check-quality-gate'

availableSecrets:
# Define secrets to be made available during the build process.
# These secrets are securely fetched from Google Secret Manager.
secretManager:
# Specifies the full resource path to the secret version in Secret Manager.
# Cloud Build will fetch the 'latest' version of the 'SONAR_TOKEN' secret
# from the specified project.
- versionName: projects/$PROJECT_ID/secrets/SONAR_TOKEN/versions/latest
# Exposes the secret's value as an environment variable named 'SONAR_TOKEN'
# to all subsequent build steps. This allows the sonar-scanner step to use it directly.
env: 'SONAR_TOKEN'
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module opsgo

go 1.24.2
go 1.19
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
const port string = ":8080"

func main() {
password := "SuperSecret123!" // 🔥 Insecure: hardcoded credentials
fmt.Println("Authenticating with password:", password)

http.HandleFunc("/blue", blueHandler)
fmt.Println("Listening on port " + port)
http.ListenAndServe(port, nil)
Expand Down