Skip to content
Snippets Groups Projects
Commit 13af3b4e authored by Torgrim's avatar Torgrim
Browse files

Implemented Updated Docker & .gitlab-ci.yml files for CI/CD pipeline

parent 060be8d8
No related branches found
No related tags found
No related merge requests found
PORT=8080 PORT=8080
TOKEN=your_api_token TOKEN=your_api_token
FIREBASE_CREDENTIALS_FILE=path_to_Firebase_credentials_file SECRETS=path_to_Firebase_credentials_file
\ No newline at end of file \ No newline at end of file
...@@ -13,18 +13,21 @@ COPY go.mod go.sum ./ ...@@ -13,18 +13,21 @@ COPY go.mod go.sum ./
COPY ../ ./ COPY ../ ./
# Build # 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. # 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 RUN apk --no-cache add ca-certificates
# Install Report Tool.
RUN go install github.com/jstemmer/go-junit-report/v2@latest
WORKDIR /root/ WORKDIR /root/
# Copy the pre-built binary file from the previous stage. # Copy the pre-built binary file from the previous stage.
COPY --from=builder /app/main . COPY --from=builder /app/test .
# Run # Run
CMD ["./main"] CMD ./test -test.v | go-junit-report > report.xml && ./test -test.v
\ No newline at end of file \ No newline at end of file
...@@ -23,8 +23,7 @@ func fileExists(filename string) bool { ...@@ -23,8 +23,7 @@ func fileExists(filename string) bool {
func main() { func main() {
if !fileExists(os.Getenv("FIREBASE_CREDENTIALS_FILE")) { if !fileExists(os.Getenv("FIREBASE_CREDENTIALS_FILE")) {
log.Fatal("Firebase Credentials file is not mounted") log.Panic("Firebase Credentials file is not mounted")
return
} }
// Get the port from the environment variable or set default to 8080 // Get the port from the environment variable or set default to 8080
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"globeboard/internal/handlers/endpoint/dashboard" "globeboard/internal/handlers/endpoint/dashboard"
"globeboard/internal/utils/constants/Endpoints" "globeboard/internal/utils/constants/Endpoints"
"globeboard/internal/utils/constants/Paths" "globeboard/internal/utils/constants/Paths"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
...@@ -22,7 +23,18 @@ var ( ...@@ -22,7 +23,18 @@ var (
ID = "" ID = ""
) )
func fileExistsTest(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func init() { 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") err := os.Setenv("GO_ENV", "test")
if err != nil { if err != nil {
panic("Unable to set GO_ENV") panic("Unable to set GO_ENV")
......
version: "3.8"
services: 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: globeboard:
image: go-globeboard image: go-globeboard
build: build:
...@@ -25,15 +9,26 @@ services: ...@@ -25,15 +9,26 @@ services:
restart: unless-stopped restart: unless-stopped
environment: environment:
PORT: ${PORT} PORT: ${PORT}
FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials FIREBASE_CREDENTIALS_FILE: /run/secrets/Firebase
secrets:
- firebase_credentials
volumes: volumes:
- ./web:/root/web:ro - ./web:/root/web:ro
depends_on: secrets:
- Firebase
globeboard-test: globeboard-test:
condition: service_completed_successfully 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
secrets:
- Firebase
secrets: secrets:
firebase_credentials: Firebase:
file: ${FIREBASE_CREDENTIALS_FILE} file: ${SECRETS}
\ No newline at end of file \ No newline at end of file
stages: stages:
- test - Build
- Test
- Deploy
image: docker
test_globeboard:
stage: test
image: docker:19.03.12
services: services:
- docker:19.03.12-dind - name: 'docker:dind'
command: [ '--tls=false', '--host=tcp://0.0.0.0:2375' ]
variables: variables:
DOCKER_HOST: tcp://docker:2375 DOCKER_TLS_CERTDIR: ''
DOCKER_DRIVER: overlay2 SECURE_FILES_DOWNLOAD_PATH: './.secrets/'
DOCKER_TLS_CERTDIR: ""
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: script:
- apk add --no-cache docker-compose - docker compose up globeboard -d
- docker-compose -f Go/docker-compose.yml up --build globeboard-test after_script:
only: - cd ./Go/
- main - docker compose down
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment