Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jenkins pipeline script #70

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@
APP_ADDRESS=:8081
APP_PORT=8081

# Fiber
FIBER_CASE_SENSITIVE=true
FIBER_STRICT_ROUTING=false

# Database
DB_DSN=root:root@tcp(posyandu-db:3306)/posyandu?charset=utf8mb4&parseTime=True&loc=Local
DB_USER=user
DB_PASSWORD=password
DB_PORT=3306
DB_NAME=posyandu

# Pool
DB_DSN=root:password@tcp(posyandu-db:3306)/posyandu?charset=utf8mb4&parseTime=True&loc=Asia%2FJakarta
DB_MAX_OPEN=100
DB_MAX_IDLE=10
DB_MAX_LIFE=30

# JWT
JWT_SECRET=bebek
JWT_SECRET=secret
JWT_EXPIRE=24
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
FROM golang:alpine
LABEL maintainer="mfathoor.23@gmail.com" \
name="mfathoor/posyandu-api" \
github="https://github.com/fathoor/posyandu-api" \
dockerhub="https://hub.docker.com/r/mfathoor/posyandu-api"

RUN apk update && apk add --no-cache git

WORKDIR /app

COPY . .
COPY go.mod go.sum ./

RUN go mod tidy

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o posyandu-api

EXPOSE ${APP_PORT}
Expand Down
80 changes: 80 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
pipeline {
agent any
stages {
stage('Environment') {
steps {
echo 'Populating environment variables..'
sh """
echo "APP_ADDRESS=${env.APP_ADDRESS}" > .env
echo "APP_PORT=${env.APP_PORT}" >> .env
echo "DB_DSN=${env.DB_DSN}" >> .env
echo "DB_USER=${env.DB_USER}" >> .env
echo "DB_PASSWORD=${env.DB_PASSWORD}" >> .env
echo "DB_PORT=${env.DB_PORT}" >> .env
echo "DB_NAME=${env.DB_NAME}" >> .env
echo "DB_MAX_OPEN=${env.DB_MAX_OPEN}" >> .env
echo "DB_MAX_IDLE=${env.DB_MAX_IDLE}" >> .env
echo "DB_MAX_LIFE=${env.DB_MAX_LIFE}" >> .env
echo "JWT_SECRET=${env.JWT_SECRET}" >> .env
echo "JWT_EXPIRE=${env.JWT_EXPIRE}" >> .env
"""
echo 'Environment variables populated.'
}
}
stage('Build') {
steps {
echo 'Building docker image..'
sh 'docker build --platform=linux/amd64 -t mfathoor/posyandu-api:latest .'
echo 'Docker image built.'
}
}
stage('Push') {
steps {
echo 'Pushing docker image..'
withCredentials([usernamePassword(credentialsId: 'fathoor-docker-hub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh 'docker login -u $USERNAME -p $PASSWORD'
}
sh 'docker push mfathoor/posyandu-api:latest'
echo 'Docker image pushed.'
}
}
stage('Deploy'){
stages {
stage('Down') {
steps {
echo 'Stopping and removing existing container..'
sh 'docker compose down'
echo 'Existing container stopped and removed.'
}
}
stage('Clean') {
steps {
echo 'Removing existing image..'
sh 'docker image rm mfathoor/posyandu-api:latest'
echo 'Existing image removed.'
}
}
stage('Up') {
steps {
echo 'Starting new container..'
sh 'docker compose up -d'
echo 'New container started.'
}
}
}
}
}
post {
success {
echo 'App is deployed successfully.'
}
failure {
echo 'App deployment failed.'
}
cleanup {
echo 'Cleaning up..'
deleteDir()
echo 'Cleaned up.'
}
}
}
25 changes: 3 additions & 22 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
services:
posyandu:
container_name: posyandu
depends_on:
- posyandu-db
image: mfathoor/posyandu-api:latest
networks:
- posyandu
ports:
- ${APP_PORT}:${APP_PORT}
restart: on-failure
volumes:
- posyandu:/usr/src/app

posyandu-db:
container_name: posyandu_mysql
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
image: mysql:5.7
networks:
- posyandu
ports:
- ${DB_PORT}:${DB_PORT}
posyandu-api:
restart: on-failure
volumes:
- posyandu-db:/var/lib/mysql

volumes:
posyandu:
posyandu-db:

networks:
posyandu:
driver: bridge
posyandu-api:
external: true
4 changes: 2 additions & 2 deletions core/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/gofiber/fiber/v2/middleware/recover"
)

func ProvideApp(cfg Config) *fiber.App {
app := fiber.New(*ProvideFiber(cfg))
func ProvideApp() *fiber.App {
app := fiber.New(*ProvideFiber())

app.Use(recover.New())
app.Use(cors.New())
Expand Down
8 changes: 0 additions & 8 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
type Config interface {
Get(key string) string
GetInt(key string) int
GetBool(key string) bool
}

type configImpl struct {
Expand All @@ -26,13 +25,6 @@ func (c *configImpl) GetInt(key string) int {
return value
}

func (c *configImpl) GetBool(key string) bool {
value, err := strconv.ParseBool(os.Getenv(key))
exception.PanicIfNeeded(err)

return value
}

func ProvideConfig() Config {
return &configImpl{}
}
6 changes: 3 additions & 3 deletions core/config/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"github.com/gofiber/fiber/v2"
)

func ProvideFiber(cfg Config) *fiber.Config {
func ProvideFiber() *fiber.Config {
return &fiber.Config{
CaseSensitive: cfg.GetBool("FIBER_CASE_SENSITIVE"),
StrictRouting: cfg.GetBool("FIBER_STRICT_ROUTING"),
CaseSensitive: true,
StrictRouting: false,
ErrorHandler: exception.Handler,
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func main() {
var (
cfg = config.ProvideConfig()
app = config.ProvideApp(cfg)
app = config.ProvideApp()
db = config.ProvideDB(cfg)
)

Expand Down