gitlab.com/sparetimecoders/build-tools@v0.1.0/.gitlab-ci.yml (about)

     1  # Copyright (c) 2019 sparetimecoders
     2  #
     3  # Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  # this software and associated documentation files (the "Software"), to deal in
     5  # the Software without restriction, including without limitation the rights to
     6  # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
     7  # the Software, and to permit persons to whom the Software is furnished to do so,
     8  # subject to the following conditions:
     9  #
    10  # The above copyright notice and this permission notice shall be included in all
    11  # copies or substantial portions of the Software.
    12  #
    13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    15  # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    16  # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    17  # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    18  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    19  
    20  variables:
    21    GOCACHE: "${CI_PROJECT_DIR}/_go/cache"
    22  
    23  before_script:
    24    - mkdir -p ${CI_PROJECT_DIR}/_go/{pkg,bin,cache}
    25    - rm -rf /go/pkg || true
    26    - mkdir -p /go
    27    - ln -s ${CI_PROJECT_DIR}/_go/pkg /go/pkg
    28    - ln -s ${CI_PROJECT_DIR}/_go/bin /go/bin
    29  
    30  cache:
    31    key: "$CI_COMMIT_REF_NAME"
    32    paths:
    33      - _go
    34    untracked: true
    35  
    36  stages:
    37    - deps
    38    - test
    39    - build
    40    - docker
    41  
    42  deps:
    43    stage: deps
    44    image: golang:1.12
    45    script:
    46      - go get -mod=readonly
    47  
    48  test:
    49    stage: test
    50    dependencies:
    51      - deps
    52    image: golang:1.12
    53    script:
    54      - go fmt $(go list ./...)
    55      - go vet $(go list ./...)
    56      - unset "${!CI@}"
    57      - CGO_ENABLED=1 go test -p 1 -mod=readonly -race -coverprofile=.testCoverage.txt -covermode=atomic -coverpkg=$(go list ./... | tr '\n' , | sed 's/,$//') ./...
    58      - go tool cover -html=.testCoverage.txt -o coverage.html
    59      - go tool cover -func=.testCoverage.txt
    60    artifacts:
    61      paths:
    62        - coverage.html
    63  
    64  build:
    65    stage: build
    66    image: golang:1.12
    67    script: |
    68      for file in $(find ./cmd -name *.go ! -name *test*); do
    69       echo "go build -o ./$(basename $(dirname ${file})) ${file}"
    70       GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build --tags prod -ldflags="-s -w" -o ./$(basename $(dirname ${file})) ${file}
    71      done
    72    artifacts:
    73      when: always
    74      paths:
    75        - build
    76        - deploy
    77        - kubecmd
    78        - push
    79        - service-setup
    80  
    81  docker:
    82    stage: docker
    83    services:
    84      - docker:dind
    85    image: docker:stable
    86    variables:
    87      DOCKER_HOST: tcp://docker:2375/
    88      DOCKER_DRIVER: overlay2
    89    before_script:
    90      - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    91    script: |
    92      docker build -t registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_SHA} .
    93      docker tag registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_SHA} registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_REF_SLUG}
    94      if [ "${CI_COMMIT_REF_SLUG}" == "master" ]; then
    95        docker tag registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_SHA} registry.gitlab.com/sparetimecoders/build-tools:latest
    96      fi
    97      docker push registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_SHA}
    98      docker push registry.gitlab.com/sparetimecoders/build-tools:${CI_COMMIT_REF_SLUG}
    99      if [ "${CI_COMMIT_REF_SLUG}" == "master" ]; then
   100        docker push registry.gitlab.com/sparetimecoders/build-tools:latest
   101      fi