From 060be8d867cae62c64ec05ab2558459feeea1637 Mon Sep 17 00:00:00 2001 From: Torgrim <sir_alexiner@hotmail.com> Date: Fri, 12 Apr 2024 15:14:59 +0200 Subject: [PATCH] Updated Docker files for CI/CD pipeline --- .gitignore | 2 +- Go/.dockerignore | 2 +- Go/.env.example | 3 ++- Go/Dockerfile | 4 ++-- Go/Dockerfile-test | 11 ++-------- Go/auth/auth.go | 8 ++++---- Go/cmd/globeboard/app.go | 3 +-- Go/db/db.go | 8 ++++---- Go/docker-compose.yml | 26 ++++++++++++++++-------- Go/internal/utils/constants/constants.go | 9 ++++---- gitlab-ci.yml | 17 ++++++++++++++++ 11 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 gitlab-ci.yml diff --git a/.gitignore b/.gitignore index d889399..9513bf1 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,6 @@ ehthumbs.db Thumbs.db # Personal ignore patterns -**/firebasecredentials.json +**/firebaseCredentials.json **/.secrets .env \ No newline at end of file diff --git a/Go/.dockerignore b/Go/.dockerignore index 9b2b668..002b2a5 100644 --- a/Go/.dockerignore +++ b/Go/.dockerignore @@ -88,7 +88,7 @@ ehthumbs.db Thumbs.db # Personal ignore patterns -**/firebasecredentials.json +**/firebaseCredentials.json **/.secrets .env diff --git a/Go/.env.example b/Go/.env.example index 056673c..37a44f9 100644 --- a/Go/.env.example +++ b/Go/.env.example @@ -1,2 +1,3 @@ PORT=8080 -TOKEN=your_api_token #For Testing +TOKEN=your_api_token +FIREBASE_CREDENTIALS_FILE=path_to_Firebase_credentials_file \ No newline at end of file diff --git a/Go/Dockerfile b/Go/Dockerfile index 93d3706..c7b2417 100644 --- a/Go/Dockerfile +++ b/Go/Dockerfile @@ -1,14 +1,14 @@ # syntax=docker/dockerfile:1.2 FROM golang:1.22 AS builder +LABEL authors="Torgrim Thorsen" + # Set destination for COPY WORKDIR /app # Download Go modules COPY go.mod go.sum ./ -RUN go mod download - # Copy the source code. COPY ../ ./ diff --git a/Go/Dockerfile-test b/Go/Dockerfile-test index 9160f12..2d0ca12 100644 --- a/Go/Dockerfile-test +++ b/Go/Dockerfile-test @@ -1,14 +1,14 @@ # syntax=docker/dockerfile:1.2 FROM golang:1.22 AS builder +LABEL authors="Torgrim Thorsen" + # Set destination for COPY WORKDIR /app # Download Go modules COPY go.mod go.sum ./ -RUN go mod download - # Copy the source code. COPY ../ ./ @@ -26,12 +26,5 @@ WORKDIR /root/ # Copy the pre-built binary file from the previous stage. COPY --from=builder /app/main . -# Optional: -# To bind to a TCP port, runtime parameters must be supplied to the docker command. -# But we can document in the Dockerfile what ports -# the application is going to listen on by default. -# https://docs.docker.com/reference/dockerfile/#expose -EXPOSE 8080 - # Run CMD ["./main"] \ No newline at end of file diff --git a/Go/auth/auth.go b/Go/auth/auth.go index 654f74d..f487b6c 100644 --- a/Go/auth/auth.go +++ b/Go/auth/auth.go @@ -4,9 +4,9 @@ import ( "context" firebase "firebase.google.com/go" "firebase.google.com/go/auth" - "globeboard/internal/utils/constants" "google.golang.org/api/option" "log" + "os" ) var ( @@ -16,10 +16,10 @@ var ( func GetFireBaseAuthClient() (*auth.Client, error) { // Using the credential file - sa := option.WithCredentialsFile(constants.FirebaseCredentialPath) + sa := option.WithCredentialsFile(os.Getenv("FIREBASE_CREDENTIALS_FILE")) app, err := firebase.NewApp(ctx, nil, sa) if err != nil { - log.Println("Credentials not found: " + constants.FirebaseCredentialPath) + log.Println("Credentials not found: " + os.Getenv("FIREBASE_CREDENTIALS_FILE")) log.Println("Error on getting the application") return nil, err } @@ -28,7 +28,7 @@ func GetFireBaseAuthClient() (*auth.Client, error) { client, err := app.Auth(ctx) if err != nil { // Logging the error - log.Println("Credentials file: '" + constants.FirebaseCredentialPath + "' lead to an error.") + log.Println("Credentials file: '" + os.Getenv("FIREBASE_CREDENTIALS_FILE") + "' lead to an error.") return nil, err } diff --git a/Go/cmd/globeboard/app.go b/Go/cmd/globeboard/app.go index b929882..2bedc42 100644 --- a/Go/cmd/globeboard/app.go +++ b/Go/cmd/globeboard/app.go @@ -4,7 +4,6 @@ import ( "globeboard/internal/handlers" "globeboard/internal/handlers/endpoint/dashboard" "globeboard/internal/handlers/endpoint/util" - "globeboard/internal/utils/constants" "globeboard/internal/utils/constants/Endpoints" "globeboard/internal/utils/constants/Paths" "log" @@ -23,7 +22,7 @@ func fileExists(filename string) bool { } func main() { - if !fileExists(constants.FirebaseCredentialPath) { + if !fileExists(os.Getenv("FIREBASE_CREDENTIALS_FILE")) { log.Fatal("Firebase Credentials file is not mounted") return } diff --git a/Go/db/db.go b/Go/db/db.go index 917c375..4fb7f68 100644 --- a/Go/db/db.go +++ b/Go/db/db.go @@ -7,7 +7,6 @@ import ( firebase "firebase.google.com/go" "fmt" authenticate "globeboard/auth" - "globeboard/internal/utils/constants" "globeboard/internal/utils/constants/Firestore" "globeboard/internal/utils/structs" "google.golang.org/api/iterator" @@ -16,6 +15,7 @@ import ( "google.golang.org/grpc/status" "log" "net/http" + "os" ) const ( @@ -32,10 +32,10 @@ var ( func getFirestoreClient() (*firestore.Client, error) { // Using the credential file - sa := option.WithCredentialsFile(constants.FirebaseCredentialPath) + sa := option.WithCredentialsFile(os.Getenv("FIREBASE_CREDENTIALS_FILE")) app, err := firebase.NewApp(ctx, nil, sa) if err != nil { - log.Println("Credentials not found: " + constants.FirebaseCredentialPath) + log.Println("Credentials not found: " + os.Getenv("FIREBASE_CREDENTIALS_FILE")) log.Println("Error on getting the application") return nil, err } @@ -44,7 +44,7 @@ func getFirestoreClient() (*firestore.Client, error) { client, err := app.Firestore(ctx) if err != nil { // Logging the error - log.Println("Credentials file: '" + constants.FirebaseCredentialPath + "' lead to an error.") + log.Println("Credentials file: '" + os.Getenv("FIREBASE_CREDENTIALS_FILE") + "' lead to an error.") return nil, err } diff --git a/Go/docker-compose.yml b/Go/docker-compose.yml index ae4e68f..9b5d341 100644 --- a/Go/docker-compose.yml +++ b/Go/docker-compose.yml @@ -2,28 +2,38 @@ version: "3.8" services: globeboard-test: - image: go-globeboard-test:latest + image: go-globeboard-test build: context: . dockerfile: Dockerfile-test + restart: no environment: - - TOKEN=${TOKEN} + TOKEN: ${TOKEN} + FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials + secrets: + - firebase_credentials volumes: - - ./.secrets:/root/.secrets:ro - ./web:/root/web:ro + globeboard: - image: go-globeboard:latest + image: go-globeboard build: context: . dockerfile: Dockerfile ports: - '${PORT}:${PORT}' + restart: unless-stopped environment: - - PORT=${PORT} - - TOKEN=${TOKEN} + PORT: ${PORT} + FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials + secrets: + - firebase_credentials volumes: - - ./.secrets:/root/.secrets:ro - ./web:/root/web:ro depends_on: globeboard-test: - condition: service_completed_successfully \ No newline at end of file + condition: service_completed_successfully + +secrets: + firebase_credentials: + file: ${FIREBASE_CREDENTIALS_FILE} \ No newline at end of file diff --git a/Go/internal/utils/constants/constants.go b/Go/internal/utils/constants/constants.go index 91f5a81..a79df75 100644 --- a/Go/internal/utils/constants/constants.go +++ b/Go/internal/utils/constants/constants.go @@ -3,9 +3,8 @@ package constants // APIVersion represents the version of the API. const ( - APIVersion = "v1" - FirebaseCredentialPath = "./.secrets/firebaseCredentials.json" - ApiKeyLength = 20 - DocIdLength = 24 - IdLength = 16 + APIVersion = "v1" + ApiKeyLength = 20 + DocIdLength = 24 + IdLength = 16 ) diff --git a/gitlab-ci.yml b/gitlab-ci.yml new file mode 100644 index 0000000..9c6340c --- /dev/null +++ b/gitlab-ci.yml @@ -0,0 +1,17 @@ +stages: + - test + +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: "" + script: + - apk add --no-cache docker-compose + - docker-compose -f Go/docker-compose.yml up --build globeboard-test + only: + - main -- GitLab