Skip to content

Instantly share code, notes, and snippets.

@Hecatoncheir
Last active October 1, 2018 19:05
Show Gist options
  • Select an option

  • Save Hecatoncheir/cfa48ebcaf63817d84ff6ff8f2c9b37d to your computer and use it in GitHub Desktop.

Select an option

Save Hecatoncheir/cfa48ebcaf63817d84ff6ff8f2c9b37d to your computer and use it in GitHub Desktop.
golang in dind
stages:
- install
- test
- build
before_script:
# Prepare git by added private ssh key
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
# Prepare git for use private gitlab repository
- git config --global --add url."git@gitlab.com:".insteadOf "https://gitlab.com/"
# Install golang dep package and back to project path
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- cp -r . $GOPATH/src/$CI_PROJECT_NAME
- cd $GOPATH/src/$CI_PROJECT_NAME
install:
stage: install
image: golang:$GOLANG_VERSION
script:
- cd $CI_PROJECT_NAME
- dep ensure
test:
stage: test
image: golang:$GOLANG_VERSION
script:
- cd $CI_PROJECT_NAME
- dep ensure
# Install Dgraph database
- curl https://get.dgraph.io -sSf | bash
- dgraph zero &
- dgraph server --lru_mb 2048 --zero localhost:5080 &
- dgraph-ratel &
# Run unit and integration tests if it needed
- go test ./... -v -cover
build:
stage: build
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
before_script:
# Prepare git by added private ssh key
- apk add --no-cache openssh git
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
# Prepare git for use private gitlab repository
- git config --global --add url."git@gitlab.com:".insteadOf "https://gitlab.com/"
# Install go and other packages
- apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl curl go
- go version
- go env
- export GOPATH=/builds/$CI_PROJECT_NAMESPACE
- mkdir $GOPATH/bin
- export PATH=$PATH:$GOPATH/bin
# Move project to right place
- mkdir $GOPATH/src
- cp -r . $GOPATH/src/$CI_PROJECT_NAME
- cd $GOPATH/src/$CI_PROJECT_NAME
script:
# Install golang dep tool
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
# Get packages with dep tool
- cd $CI_PROJECT_NAME
- dep ensure
- cd ..
# Login for docker repository
- docker version
- docker login --username=$CI_REGISTRY_USER --password=$CI_REGISTRY_PASSWORD $CI_REGISTRY
# Change image name int yaml for buld
- go get github.com/mikefarah/yq
- cd $GOPATH/src/github.com/mikefarah/yq
- go install
- cd $GOPATH/src/$CI_PROJECT_NAME
- yq w -i $CI_PROJECT_NAME.yml functions.$CI_PROJECT_NAME.image $CI_REGISTRY_IMAGE
# Install OpenFaasCLI
- curl -sSL https://cli.openfaas.com | sh
# Build docker function image
- faas-cli build -f ./$CI_PROJECT_NAME.yml
# Push new image to docker repository
- docker push $CI_REGISTRY/$CI_REGISTRY_IMAGE
only:
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment