added make script and vars

This commit is contained in:
Bryan Joshua Pedini 2024-08-17 10:25:11 +02:00
parent 3f03347990
commit 0078e38e2f
5 changed files with 203 additions and 0 deletions

15
.vars Normal file
View File

@ -0,0 +1,15 @@
#/usr/bin/env bash
export GIT_HOST=git.bjphoster.com
export DEPLOYMENT_HOST=docker.infra.bjphoster.cloud
export DEPLOYMENT_PATHS=/opt/yaskm.bjphoster.com
export GO_BUILDER=git.bjphoster.com/docker/gobuilder
export GO_VERSION=1.22.2-alpine3.19
export GOOS=linux
export GOARCH=amd64
export REPO_ORG=source
export REPO_NAME=yaskm
export CONTAINER_ORG=git.bjphoster.com/source
export CONTAINER_IMAGE=yaskm
export CONTAINER_IP=127.0.0.1
export CONTAINER_PORT=3000

59
deploy.sh Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
###
# FLOW
###
#
# convert deployment paths (string) into array of strings
# if path is not already set
# if number of paths > 1 print the strings with corresponding index
# get deployment path index from user
# in every case (number of paths = 1 OR index left blank from user), index is 0
# if version is not already set
# get version from user
# if version is left balnk, version is "latest"
# get deployment path from index X of deployments
# ssh into deployment host
# cd into deployment path
# git pull last changes
# set correct version in .env file
# pull latest docker image and spin up containers
#
# Convert deployment paths into array
ENVIRONMENTS=($DEPLOYMENT_PATHS)
# Check if the DEPLOYMENT_PATH is not already set
if [ -z "${DEPLOYMENT_PATH}" ]; then
# Print and ask for deployment environment (if more than one)
if [ "${#ENVIRONMENTS[@]}" -gt 1 ]; then
for i in "${!ENVIRONMENTS[@]}"; do
echo "$i: ${ENVIRONMENTS[$i]}"
done
read -p "Deployment environment: " DEPLOYMENT_ENVIRONMENT
fi
if [ -z "${DEPLOYMENT_ENVIRONMENT}" ]; then
DEPLOYMENT_ENVIRONMENT=0
fi
# Select correct path
DEPLOYMENT_PATH="${ENVIRONMENTS[$DEPLOYMENT_ENVIRONMENT]}"
fi
# Check if the DEPLOYMENT_VERSION is not already set
if [ -z "${DEPLOYMENT_VERSION}" ]; then
# Ask for deployment version
read -p "Version [latest]: " DEPLOYMENT_VERSION
if [ -z "${DEPLOYMENT_VERSION}" ]; then
DEPLOYMENT_VERSION=latest
fi
fi
echo "${DEPLOYMENT_PATH}"
echo "${DEPLOYMENT_VERSION}"
ssh $DEPLOYMENT_HOST \
"cd ${DEPLOYMENT_PATH} && \
git pull && \
sed -i "s/VERSION=.*/VERSION=${DEPLOYMENT_VERSION}/" .env && \
docker compose pull && \
docker compose up -d"

33
dockerfile Normal file
View File

@ -0,0 +1,33 @@
# Stage 1 · go builder
ARG GO_BUILDER=golang
ARG GO_VERSION=latest
FROM ${GO_BUILDER}:${GO_VERSION} AS build
ARG GO_OS
ARG GO_ARCH
ARG GIT_HOST
ARG REPO_ORG
ARG REPO_NAME
ARG APP_VERSION
# Copy the project inside the builder container
WORKDIR $GOPATH/src/${GIT_HOST}/$REPO_ORG/$REPO_NAME/
COPY . .
# Build the binary
RUN CGO_ENABLED=0 GOOS=${GO_OS} GOARCH=${GO_ARCH} \
go build \
-installsuffix cgo \
-ldflags="-w -s -X 'main.APP_VERSION=${APP_VERSION}' -X 'main.COMMIT_ID=$(git log HEAD --oneline | awk '{print $1}' | head -n1)'" \
--o /app
# Stage 2 · scratch image
FROM scratch
# Copy the necessary stuff from the build stage
COPY --from=build /app /app
# Copy the certificates - in case of fetches
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/cert.pem
# Execute the binary
ENTRYPOINT ["/app"]

54
makefile Normal file
View File

@ -0,0 +1,54 @@
#!make
include .vars
default: version
clean:
if [ "$$(docker images "$${CONTAINER_ORG}/$${CONTAINER_IMAGE}" --format "{{.Repository}}:{{.Tag}}")" != "" ]; then \
docker image rm $$(docker images "$${CONTAINER_ORG}/$${CONTAINER_IMAGE}" --all --format "{{.Repository}}:{{.Tag}}"); \
fi
docker: clean
docker build \
--build-arg GO_BUILDER=$${GO_BUILDER} \
--build-arg GO_VERSION=$${GO_VERSION} \
--build-arg GO_OS=$${GO_OS} \
--build-arg GO_ARCH=$${GO_ARCH} \
--build-arg GIT_HOST=$${GIT_HOST} \
--build-arg REPO_ORG=$${REPO_ORG} \
--build-arg REPO_NAME=$${REPO_NAME} \
--build-arg APP_VERSION=$${APP_VERSION} \
-t $${CONTAINER_ORG}/$${CONTAINER_IMAGE}:$${APP_VERSION} .; \
if [ "$$(docker images --filter "dangling=true" --quiet --no-trunc)" != "" ]; then \
docker image rm $$(docker images --filter "dangling=true" --quiet --no-trunc); \
fi
dockerpush:
docker push \
$${CONTAINER_ORG}/$${CONTAINER_IMAGE}:$${APP_VERSION}
deploy:
bash -c "./deploy.sh"
version:
bash -c "./version.sh"
run:
docker run \
--rm \
--tty \
--interactive \
--publish $${CONTAINER_IP}:$${CONTAINER_PORT}:80 \
--workdir /go/src/$${GIT_HOST}/$${REPO_ORG}/$${REPO_NAME} \
--volume $(shell pwd):/go/src/$${GIT_HOST}/$${REPO_ORG}/$${REPO_NAME} \
$${GO_BUILDER}:$${GO_VERSION} \
go run .
dockerrun:
docker run \
--rm \
--tty \
--interactive \
--publish $${CONTAINER_IP}:$${CONTAINER_PORT}:80 \
--volume $(shell pwd)/config.yml:/config.yml \
$${CONTAINER_ORG}/$${CONTAINER_IMAGE}:latest

42
version.sh Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
###
# FLOW
###
#
# get version from user
# if version is left balnk, version is "latest"
# get docker push from user
# if docker push is left blank, it's negative
# if version != "latest" create version tag at current commit
# make the app
# if docker push, push docker image
#
# Get version from user
read -p "Version [latest]: " VERSIONINPUT
# If version was not provided, use the latest commit short hash as version
if [ -z ${VERSIONINPUT} ]; then
APP_VERSION="latest"
else
APP_VERSION=${VERSIONINPUT}
fi
# Get docker push option from user
read -p "Docker push? [n]: " DOCKERPUSH
if [ -z ${DOCKERPUSH} ]; then
DOCKERPUSH=n
fi
# Create version tag (if provided)
if [ ! -z ${VERSIONINPUT} ]; then
git tag ${APP_VERSION}
fi
# Build the app
export APP_VERSION
make docker
# If wanted, push the docker image
if [ ${DOCKERPUSH} = "y" ]; then
make dockerpush
fi