diff --git a/Go/.env.example b/Go/.env.example index 37a44f981ef59fbe383a4b4be7cb9cf639b64b61..3718be99e94c25f8dfcbf25d269eebbeca075ea9 100644 --- a/Go/.env.example +++ b/Go/.env.example @@ -1,3 +1,3 @@ PORT=8080 TOKEN=your_api_token -FIREBASE_CREDENTIALS_FILE=path_to_Firebase_credentials_file \ No newline at end of file +SECRETS=path_to_Firebase_credentials_file \ No newline at end of file diff --git a/Go/Dockerfile-test b/Go/Dockerfile-test index 2d0ca1280822e38ef9b3223905abb7d7dcc49d2c..7dfc3ac2675cf2b27cf7098a2dff5659ab523044 100644 --- a/Go/Dockerfile-test +++ b/Go/Dockerfile-test @@ -13,18 +13,21 @@ COPY go.mod go.sum ./ COPY ../ ./ # Build -RUN CGO_ENABLED=0 GOOS=linux go test -c -installsuffix cgo -o main ./cmd/globeboard +RUN CGO_ENABLED=0 GOOS=linux go test -c -installsuffix cgo -o test ./cmd/globeboard # Use a minimal alpine image for the final build stage. -FROM alpine:3.19 +FROM golang:1.22-alpine -# Install CA certificates. +# Install CA certificates and Go. RUN apk --no-cache add ca-certificates +# Install Report Tool. +RUN go install github.com/jstemmer/go-junit-report/v2@latest + WORKDIR /root/ # Copy the pre-built binary file from the previous stage. -COPY --from=builder /app/main . +COPY --from=builder /app/test . # Run -CMD ["./main"] \ No newline at end of file +CMD ./test -test.v | go-junit-report > report.xml && ./test -test.v \ No newline at end of file diff --git a/Go/cmd/globeboard/app.go b/Go/cmd/globeboard/app.go index 2bedc4213bf454cc33fa265ae9412bbf72ce630b..c384d3933c7be683be8e20df2448eacd260feaa5 100644 --- a/Go/cmd/globeboard/app.go +++ b/Go/cmd/globeboard/app.go @@ -23,8 +23,7 @@ func fileExists(filename string) bool { func main() { if !fileExists(os.Getenv("FIREBASE_CREDENTIALS_FILE")) { - log.Fatal("Firebase Credentials file is not mounted") - return + log.Panic("Firebase Credentials file is not mounted") } // Get the port from the environment variable or set default to 8080 diff --git a/Go/cmd/globeboard/app_test.go b/Go/cmd/globeboard/app_test.go index c0ed62b59537965f23cec01e8e9ee8185964185b..3f7bfbdd1b6836a63de748c6a8b3b569615dbde2 100644 --- a/Go/cmd/globeboard/app_test.go +++ b/Go/cmd/globeboard/app_test.go @@ -5,6 +5,7 @@ import ( "globeboard/internal/handlers/endpoint/dashboard" "globeboard/internal/utils/constants/Endpoints" "globeboard/internal/utils/constants/Paths" + "log" "net/http" "net/http/httptest" "os" @@ -22,7 +23,18 @@ var ( ID = "" ) +func fileExistsTest(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + func init() { + if !fileExistsTest(os.Getenv("FIREBASE_CREDENTIALS_FILE")) { + log.Panic("Firebase Credentials file is not mounted:", os.Getenv("FIREBASE_CREDENTIALS_FILE")) + } err := os.Setenv("GO_ENV", "test") if err != nil { panic("Unable to set GO_ENV") diff --git a/Go/docker-compose.yml b/Go/docker-compose.yml index 9b5d34181a0a98752701f81724f3fb32f3c43bf6..4cd856deb02731f64be11beedfa533dd20724f78 100644 --- a/Go/docker-compose.yml +++ b/Go/docker-compose.yml @@ -1,20 +1,4 @@ -version: "3.8" - services: - globeboard-test: - image: go-globeboard-test - build: - context: . - dockerfile: Dockerfile-test - restart: no - environment: - TOKEN: ${TOKEN} - FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials - secrets: - - firebase_credentials - volumes: - - ./web:/root/web:ro - globeboard: image: go-globeboard build: @@ -25,15 +9,26 @@ services: restart: unless-stopped environment: PORT: ${PORT} - FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials + FIREBASE_CREDENTIALS_FILE: /run/secrets/Firebase + volumes: + - ./web:/root/web:ro secrets: - - firebase_credentials + - Firebase + + globeboard-test: + image: go-globeboard-test + build: + context: . + dockerfile: Dockerfile-test + restart: no + environment: + TOKEN: ${TOKEN} + FIREBASE_CREDENTIALS_FILE: /run/secrets/Firebase volumes: - ./web:/root/web:ro - depends_on: - globeboard-test: - condition: service_completed_successfully + secrets: + - Firebase secrets: - firebase_credentials: - file: ${FIREBASE_CREDENTIALS_FILE} \ No newline at end of file + Firebase: + file: ${SECRETS} \ No newline at end of file diff --git a/gitlab-ci.yml b/gitlab-ci.yml index 9c6340c9e31296e6569b6cf8034a4a35327a471f..0f0cd64b6fe3a4e4e5fadb683befbe45ba8c3ca9 100644 --- a/gitlab-ci.yml +++ b/gitlab-ci.yml @@ -1,17 +1,52 @@ stages: - - test + - Build + - Test + - Deploy -test_globeboard: - stage: test - image: docker:19.03.12 - services: - - docker:19.03.12-dind - variables: - DOCKER_HOST: tcp://docker:2375 - DOCKER_DRIVER: overlay2 - DOCKER_TLS_CERTDIR: "" +image: docker + +services: + - name: 'docker:dind' + command: [ '--tls=false', '--host=tcp://0.0.0.0:2375' ] + +variables: + DOCKER_TLS_CERTDIR: '' + SECURE_FILES_DOWNLOAD_PATH: './.secrets/' + +Build: + stage: Build + before_script: + - cd ./Go/ + script: + - docker compose build + +Test: + stage: Test + before_script: + - cd ./Go/ + - apk add --no-cache curl bash go + - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash > /dev/null 2>&1 + script: + - docker compose up globeboard-test --exit-code-from globeboard-test + - docker compose cp globeboard-test:/root/report.xml ./report.xml + after_script: + - cd ./Go/ + - docker compose down globeboard-test + artifacts: + when: always + paths: + - ./Go/report.xml + reports: + junit: ./Go/report.xml + +Deploy: + stage: Deploy + before_script: + - cd ./Go/ + - apk add --no-cache curl bash + - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash > /dev/null 2>&1 script: - - apk add --no-cache docker-compose - - docker-compose -f Go/docker-compose.yml up --build globeboard-test - only: - - main + - docker compose up globeboard -d + after_script: + - cd ./Go/ + - docker compose down \ No newline at end of file