Skip to content

Commit

Permalink
Merge pull request #3 from GarnBarn/develop
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
nabhan-au authored Apr 22, 2023
2 parents def37db + 23713c6 commit 9db9353
Show file tree
Hide file tree
Showing 14 changed files with 816 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HTTP_SERVER_PORT=3007
GIN_MODE=release
MYSQL_CONNECTION_STRING=root:P@ssw0rd@tcp(127.0.0.1:3306)/garnbarn-tag?charset=utf8mb4&parseTime=True&loc=Local
FIREBASE_CONFIG_FILE=firebase-credential.json
78 changes: 78 additions & 0 deletions .github/workflows/build-and-release-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Build Docker Image and Release Github

on:
push:
branches: ["master"]

jobs:
release-on-github:
runs-on: ubuntu-latest
outputs:
newTag: ${{ steps.semver_rel.outputs.new_tag }}
previousTag: ${{ steps.semver_rel.outputs.previous_tag }}
changeLogs: ${{ steps.github_release.outputs.changelog }}
steps:
# Fetch all repository details (Including tag for semver).
- uses: actions/checkout@v3
with:
fetch-depth: 0

# Determan new version by using Semver logic.
- name: Semver Release
id: semver_rel
uses: hennejg/github-tag-action@v4.3.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

# Generate Change Log
- name: Release Changelog Builder
id: github_release
uses: mikepenz/release-changelog-builder-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fromTag: ${{ steps.semver_rel.outputs.previous_tag }}
toTag: ${{ steps.semver_rel.outputs.new_tag }}

# Create a release on Github Repo.
- name: Create Release
uses: mikepenz/action-gh-release@v0.2.0-a03 #softprops/action-gh-release
with:
body: ${{ steps.github_release.outputs.changelog }}
tag_name: ${{ steps.semver_rel.outputs.new_tag }}

build-docker-image:
runs-on: ubuntu-latest
needs: release-on-github
steps:
# Fetch all repository details (Including tag for semver).
- uses: actions/checkout@v3

- uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.SSH_COMMON_GO_PRIVATE_KEY }}

# Login to DockerHub by using the credentials.
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

# Setup BuildX
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Build Docker Image and push to docker hub.
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ format('ghcr.io/garnbarn/gb-account-service:{0}', needs.release-on-github.outputs.newTag) }},ghcr.io/garnbarn/gb-account-service:latest
github-token: ${{ secrets.GITHUB_TOKEN }}
platforms: |
linux/amd64
linux/arm64
ssh: |
default
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Env
.env
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Stage 1 - Builder: Import the golang container.
FROM golang:1.20-alpine as builder

ARG PORT=3000
ARG GRPCPORT=5002

# Install ssh client and git
RUN apk add --no-cache openssh-client git

# Download public key for github.com
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

# Set the work directory.
WORKDIR /app

# Copy go mod and sum files.
COPY go.mod ./
COPY go.sum ./

# Install the dependencies.
RUN git config --global --add url."ssh://git@github.com/".insteadOf "https://github.com/"
RUN export GOPRIVATE=github.com/GarnBarn/common-go
# This command will have access to the forwarded agent (if one is
# available)
RUN --mount=type=ssh go mod download

# Copy the source code into the container.
COPY ./ ./

# Build the source code
RUN CGO_ENABLED=0 go build -o ./out/application .


# Stage 2 - Runner.
FROM alpine:3.16.2
WORKDIR /app
COPY --from=builder /app/out/application application

EXPOSE $PORT
EXPOSE $GRPCPORT

CMD [ "/app/application" ]
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
run:
go run .

compose-up:
COMPOSE_PROJECT_NAME=garnbarn-account docker-compose up -d

compose-down:
COMPOSE_PROJECT_NAME=garnbarn-account docker-compose down
40 changes: 40 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"os"
)

type Config struct {
Env string
HTTP_SERVER_PORT string `envconfig:"HTTP_SERVER_PORT" default:"3000"`
GIN_MODE string `envconfig:"GIN_MODE" default:"release"`
MYSQL_CONNECTION_STRING string `envconfig:"MYSQL_CONNECTION_STRING"`
FIREBASE_CONFIG_FILE string `envconfig:"FIREBASE_CONFIG_FILE" default:"firebase-credential.json"`
}

func Load() Config {
var config Config
ENV, ok := os.LookupEnv("ENV")
if !ok {
// Default value for ENV.
ENV = "dev"
}
// Load the .env file only for dev env.
ENV_CONFIG, ok := os.LookupEnv("ENV_CONFIG")
if !ok {
ENV_CONFIG = "./.env"
}

err := godotenv.Load(ENV_CONFIG)
if err != nil {
logrus.Warn("Can't load env file")
}

envconfig.MustProcess("", &config)
config.Env = ENV

return config
}
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 3.0.0
services:
account-db:
image: mysql:8.0.32
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=P@ssw0rd
- MYSQL_DATABASE=garnbarn-account
phpmyadmin:
image: phpmyadmin:5.2.1
restart: always
ports:
- 8080:80
links:
- account-db
environment:
- PMA_HOST=account-db
68 changes: 68 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module github.com/GarnBarn/gb-account-service

go 1.20

require (
firebase.google.com/go v3.13.0+incompatible
github.com/GarnBarn/common-go v0.6.2
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.0
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/sirupsen/logrus v1.9.0
google.golang.org/api v0.119.0
gorm.io/gorm v1.25.0
)

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/firestore v1.9.0 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.0 // indirect
)
Loading

0 comments on commit 9db9353

Please sign in to comment.