l0nax/changelog-go

View on GitHub
.gitlab-ci.yml

Summary

Maintainability
Test Coverage
image: golang:1.16

variables:
  GOCACHE: "/go/.build_cache"

include:
  - template: SAST.gitlab-ci.yml
  - template: Dependency-Scanning.gitlab-ci.yml
  - template: License-Scanning.gitlab-ci.yml

stages:
  - build
  - test
  - deploy

# this job creates all relevant files which are needed in the 'build' AND
# 'release' job/ stage.
go_prepare:
  stage: .pre
  script:
  - export PATH=$PATH:$GOPATH/bin
  # get new Version from `git-semver`
  - go install -v github.com/mdomke/git-semver@master
  - git-semver -prefix v > `pwd`/.version
  # get build/ target environment
  - export VERSION=$(cat `pwd`/.version)
  - $(pwd)/.ci/scripts/setEnvironment.sh
  - export BUILD_ENVIRONMENT=$(cat `pwd`/ENVIRONMENT)
  - make build-dev
  artifacts:
    expire_in: 1 hour
    paths:
    - .version
    - ENVIRONMENT

build:
  stage: build
  script:
  # install goreleaser
  - curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh
  # get build/ target environment and build date
  - export VERSION=$(cat `pwd`/.version)
  - export BUILD_ENVIRONMENT=$(cat `pwd`/ENVIRONMENT)
  - export BUILD_DATE=$(date --utc +%Y-%m-%dT%H:%M:%S%Z)
  - >-
      echo "++ Build Date: \"${BUILD_DATE}\""
  # execute goreleaser but without pushing, signing or validating – so we can
  # use the artifacts ie. in Sentry
  - ./bin/goreleaser release --skip-validate --skip-publish --skip-sign --config="`pwd`/.ci/.goreleaser.develop.yml"
  # delete all archives from dist/
  - rm -rf `pwd`/dist/*.tar.gz
  cache:
    paths:
      - ${GOCACHE}
      - /go/pkg/mod
  except:
    # there is a "release" job which runs only when we are creating a new release
    - tags
  artifacts:
    expire_in: 1 hour
    paths:
    - dist/
    - .version
    - ENVIRONMENT

release:
  stage: build
  variables:
    # Disable shallow cloning so that goreleaser can diff between tags to
    # generate a changelog.
    #
    # TODO: Use custom CHANGELOG.md (stripped from previous releases)
    GIT_DEPTH: 0
  script:
  # install goreleaser
  - curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh
  # get build/ target environment and build date
  - export VERSION=$(cat `pwd`/.version)
  - export BUILD_ENVIRONMENT=$(cat `pwd`/ENVIRONMENT)
  - export BUILD_DATE=$(date --utc +%Y-%m-%dT%H:%M:%S%Z)
  - >-
      echo "++ Build Date: \"${BUILD_DATE}\""
  # execute goreleaser but without pushing, signing or validating – so we can
  # use the artifacts ie. in Sentry
  - ./bin/goreleaser release --skip-validate --config="`pwd`/.ci/.goreleaser.release.yml"
  # delete all archives from dist/
  - rm -rf `pwd`/dist/*.tar.gz
  cache:
    paths:
      - ${GOCACHE}
      - /go/pkg/mod
  only:
    - tags
  artifacts:
    expire_in: 1 hour
    paths:
    - dist/
    - .version
    - ENVIRONMENT

sentry_deploy:
  stage: deploy
  image: getsentry/sentry-cli
  only:
    # run only on release build OR on develop branch (testing "deployments")
    - tags
    - develop
  script:
    - export VERSION="${PRJ}@$(cat `pwd`/.version)"
    - export BUILD_ENVIRONMENT=$(cat `pwd`/ENVIRONMENT)
    # create new release in sentry
    - sentry-cli releases new -p ${SENTRY_PRJ} ${VERSION}
    # add commits
    - sentry-cli releases set-commits --commit "${SENTRY_GIT_REPO_NAME}" ${VERSION}
    # upload artifacts
    - export BINARIES=$(ls -d dist/*/* | grep -v prime)
    - ls -d dist/*/* | grep -v prime
    - sentry-cli upload-dif -p ${SENTRY_PRJ} --wait ${BINARIES}
    # finalize sentry release
    - sentry-cli releases finalize ${VERSION}
    # mark release as deployed if environment is production or "staging"
    - >-
        if [ "$BUILD_ENVIRONMENT" == "production" ] || [ "$BUILD_ENVIRONMENT" == "staging" ]; then
          sentry-cli releases deploys ${VERSION} new -e ${BUILD_ENVIRONMENT}
        fi

# upload new Version to GitLab Pages site
pages:
  stage: deploy
  script:
  # install go-selfupdate
  - go get -u github.com/sanbornm/go-selfupdate/...
  # generate content for 'public'
  # TODO: filter all other 'goreleaser' files out
  - ./.ci/getReleases.sh $(cat `pwd`/.version)
  # move 'index.html' to public/
  # TODO: create nice index.html
  artifacts:
    paths:
    - public
  only:
  - tags