Last active
May 26, 2024 17:46
-
-
Save sovchinn/6c3d8397e3dbed754ba127a172925bd9 to your computer and use it in GitHub Desktop.
golang scratch docker builder - gitlab ci yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Review and update variables in each stage as needed | |
# Pipeline uses gitlab k8 integration and a couple of custom containers | |
## The code is based on the gitlab autodevops yaml | |
# The pipeline builds two docker images | |
## First image is for development using Ubuntu as base (so you can debug inside the container) | |
## Second image is for production built from scratch | |
# Currently only the dev stage is deployed | |
cache: | |
untracked: true | |
key: $CI_COMMIT_REF_SLUG | |
paths: | |
- vendor/ | |
variables: | |
REPO_NAME: gitlab.com/sovchinn/hello_world | |
stages: | |
- setup | |
- test | |
- compile | |
- build | |
- deploy | |
setup: | |
stage: setup | |
image: sovchinn/builder | |
before_script: | |
- mkdir -p $GOPATH/src/$REPO_NAME | |
- ln -svf $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME | |
- cd $GOPATH/src/$REPO_NAME | |
script: | |
- "[ ! -f glide.yaml ] || glide install -v" | |
artifacts: | |
paths: | |
- vendor/ | |
test: | |
stage: test | |
image: golang:latest | |
before_script: | |
- mkdir -p $GOPATH/src/$REPO_NAME | |
- ln -svf $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME | |
- cd $GOPATH/src/$REPO_NAME | |
script: | |
- go fmt $(go list ./... | grep -v /vendor/) | |
- go vet $(go list ./... | grep -v /vendor/) | |
- go test -race $(go list ./... | grep -v /vendor/) | |
build:go:dev: | |
stage: compile | |
image: golang:latest | |
variables: | |
CGO_ENABLED: 0 | |
GOOS: linux | |
before_script: | |
- mkdir -p $GOPATH/src/$REPO_NAME | |
- ln -svf $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME | |
- cd $GOPATH/src/$REPO_NAME | |
script: | |
- go build -a -installsuffix cgo -o $CI_PROJECT_DIR/release | |
artifacts: | |
paths: | |
- release | |
build:docker:dev: | |
stage: build | |
image: docker:latest | |
variables: | |
DOCKER_DRIVER: overlay2 | |
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/dev | |
CI_APPLICATION_TAG: dev_$CI_COMMIT_SHA | |
BASE: ubuntu:zesty | |
services: | |
- docker:dind | |
script: | |
- docker build --cache-from $CI_APPLICATION_REPOSITORY/dev --build-arg BASE=$BASE -t "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" -t $CI_APPLICATION_REPOSITORY:latest . | |
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY" | |
- docker push "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" | |
- docker push $CI_APPLICATION_REPOSITORY:latest | |
dependencies: | |
- build:go:dev | |
build:go:prod: | |
stage: compile | |
image: golang:latest | |
variables: | |
CGO_ENABLED: 0 | |
GOOS: linux | |
before_script: | |
- mkdir -p $GOPATH/src/$REPO_NAME | |
- ln -svf $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME | |
- cd $GOPATH/src/$REPO_NAME | |
script: | |
- go build -ldflags '-w -s' -a -installsuffix cgo -o $CI_PROJECT_DIR/release | |
artifacts: | |
paths: | |
- release | |
build:docker:prod: | |
stage: build | |
image: docker:latest | |
variables: | |
DOCKER_DRIVER: overlay2 | |
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/prod | |
CI_APPLICATION_TAG: prod_$CI_COMMIT_SHA | |
BASE: scratch | |
services: | |
- docker:dind | |
script: | |
- docker build --cache-from $CI_APPLICATION_REPOSITORY/prod --build-arg BASE=$BASE -t "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" -t $CI_APPLICATION_REPOSITORY:latest . | |
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY" | |
- docker push "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" | |
- docker push $CI_APPLICATION_REPOSITORY:latest | |
dependencies: | |
- build:go:prod | |
deploy:dev: | |
stage: deploy | |
image: sovchinn/deployer | |
variables: | |
APP_NAME: $CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG | |
CHART_TEMPLATE: sovchinn/deploy-app | |
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG/dev | |
CI_APPLICATION_TAG: dev_$CI_COMMIT_SHA | |
SVC_INTERNAL_PORT: 8080 | |
SVC_PUBLISH_PORT: 80 | |
SVC_URL: $CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG.local | |
SVC_PROTOCOL: http | |
REPLICAS: 1 | |
SERVICE_ENABLED: "true" | |
before_script: | |
- helm init --client-only | |
- helm repo add stable https://kubernetes-charts.storage.googleapis.com/ | |
- helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/ | |
- helm repo add sovchinn https://charts.sergeovchinnikov.com | |
- helm repo update | |
script: | |
- mkdir -p /root/.kube | |
- cp /files/config /root/.kube/ | |
- helm fetch $CHART_TEMPLATE --untar | |
- rm -Rf chart | |
- mv $(basename $CHART_TEMPLATE) chart | |
- helm dependency update chart | |
- helm dependency build chart | |
- helm upgrade --install | |
--wait | |
--set service.enabled=$SERVICE_ENABLED | |
--set releaseOverride=$CI_ENVIRONMENT_SLUG | |
--set image.repository=$CI_APPLICATION_REPOSITORY | |
--set image.tag=$CI_APPLICATION_TAG | |
--set image.pullPolicy=IfNotPresent | |
--set service.url=$CI_ENVIRONMENT_URL | |
--set service.internalPort=$SVC_INTERNAL_PORT | |
--set service.externalPort=$SVC_PUBLISH_PORT | |
--set replicaCount=$REPLICAS | |
--namespace=$KUBE_NAMESPACE | |
--version=$CI_PIPELINE_ID-$CI_JOB_ID | |
$APP_NAME | |
chart/ | |
environment: | |
name: dev | |
url: $SVC_PROTOCOL://$SVC_URL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment