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
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
......@@ -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
......@@ -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
......
......@@ -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")
......
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
secrets:
- firebase_credentials
FIREBASE_CREDENTIALS_FILE: /run/secrets/Firebase
volumes:
- ./web:/root/web:ro
depends_on:
secrets:
- Firebase
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:
firebase_credentials:
file: ${FIREBASE_CREDENTIALS_FILE}
\ No newline at end of file
Firebase:
file: ${SECRETS}
\ No newline at end of file
stages:
- test
- Build
- Test
- Deploy
image: docker
test_globeboard:
stage: test
image: docker:19.03.12
services:
- docker:19.03.12-dind
- name: 'docker:dind'
command: [ '--tls=false', '--host=tcp://0.0.0.0:2375' ]
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment