|
| 1 | +alias: |
| 2 | + default: &default |
| 3 | + working_directory: /go/src/github.com/kpango/glg |
| 4 | + docker: |
| 5 | + - image: circleci/golang:1.11.3 |
| 6 | + environment: |
| 7 | + GOPATH: "/go" |
| 8 | + GO111MODULE: "on" |
| 9 | + REPO_NAME: "kpango" |
| 10 | + IMAGE_NAME: "glg" |
| 11 | + GITHUB_API: "https://api.github.com/" |
| 12 | + DOCKER_USER: "kpango" |
| 13 | + setup_remote_docker: &setup_remote_docker |
| 14 | + version: 18.06.0-ce |
| 15 | + docker_layer_caching: true |
| 16 | + |
| 17 | +version: 2 |
| 18 | +jobs: |
| 19 | + test: |
| 20 | + <<: *default |
| 21 | + steps: |
| 22 | + - checkout |
| 23 | + - restore_cache: |
| 24 | + key: gosum-{{ .Branch }}-{{ checksum "go.sum" }} |
| 25 | + - run: |
| 26 | + name: preparation |
| 27 | + command: | |
| 28 | + go mod vendor |
| 29 | + - run: |
| 30 | + name: run tests |
| 31 | + command: | |
| 32 | + go test -v -race -covermode=atomic -coverprofile=coverage.out ./... |
| 33 | + go tool cover -html=coverage.out -o coverage.html |
| 34 | + bash <(curl -s https://codecov.io/bash) |
| 35 | + - store_artifacts: |
| 36 | + path: ./coverage.html |
| 37 | + - save_cache: |
| 38 | + key: gosum-{{ .Branch }}-{{ checksum "go.sum" }} |
| 39 | + paths: |
| 40 | + - ./vendor |
| 41 | + versioning: |
| 42 | + <<: *default |
| 43 | + steps: |
| 44 | + - checkout |
| 45 | + - run: |
| 46 | + name: check |
| 47 | + command: | |
| 48 | + mkdir -p $HOME/.ssh/ && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config |
| 49 | + LAST_COMMIT=`git log -1 --pretty=%B` |
| 50 | + VERSION=`git describe --abbrev=0 --tags` |
| 51 | + touch ./.tag |
| 52 | + if [ ! -z "`git diff $VERSION`" -o -z "$VERSION" ]; then |
| 53 | + VERSION=${VERSION:-'0.0.0'} |
| 54 | + MAJOR="${VERSION%%.*}"; VERSION="${VERSION#*.}" |
| 55 | + MINOR="${VERSION%%.*}"; VERSION="${VERSION#*.}" |
| 56 | + PATCH="${VERSION%%.*}"; VERSION="${VERSION#*.}" |
| 57 | + if echo $LAST_COMMIT | grep "\[\(major\|MAJOR\)\]" > /dev/null; then |
| 58 | + MAJOR=$((MAJOR+1)) |
| 59 | + echo "$MAJOR.0.0" > ./.tag |
| 60 | + elif echo $LAST_COMMIT | grep "\[\(minor\|MINOR\)\]" > /dev/null; then |
| 61 | + MINOR=$((MINOR+1)) |
| 62 | + echo "$MAJOR.$MINOR.0" > ./.tag |
| 63 | + elif echo $LAST_COMMIT | grep "\[\(patch\|PATCH\)\]" > /dev/null; then |
| 64 | + PATCH=$((PATCH+1)) |
| 65 | + echo "$MAJOR.$MINOR.$PATCH" > ./.tag |
| 66 | + fi |
| 67 | + fi |
| 68 | + - persist_to_workspace: |
| 69 | + root: . |
| 70 | + paths: |
| 71 | + - . |
| 72 | + push: |
| 73 | + <<: *default |
| 74 | + steps: |
| 75 | + - attach_workspace: |
| 76 | + at: . |
| 77 | + - run: |
| 78 | + name: push tag and check PR body |
| 79 | + command: | |
| 80 | + mkdir -p $HOME/.ssh/ && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config |
| 81 | + TAG=`cat ./.tag` |
| 82 | + if [ ! -z "$TAG" ]; then |
| 83 | + echo $TAG |
| 84 | + git tag $TAG |
| 85 | + git push https://${GITHUB_ACCESS_TOKEN}:[email protected]/${REPO_NAME}/${IMAGE_NAME} --tags |
| 86 | + fi |
| 87 | + - persist_to_workspace: |
| 88 | + root: . |
| 89 | + paths: |
| 90 | + - . |
| 91 | + gh_release: |
| 92 | + <<: *default |
| 93 | + steps: |
| 94 | + - attach_workspace: |
| 95 | + at: . |
| 96 | + - run: |
| 97 | + name: release |
| 98 | + command: | |
| 99 | + mkdir -p $HOME/.ssh/ && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config |
| 100 | + TAG=`cat ./.tag` |
| 101 | + if [ ! -z "$TAG" ]; then |
| 102 | + echo "Create release: ${TAG}" |
| 103 | + curl -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \ |
| 104 | + -X POST \ |
| 105 | + -d "{\"tag_name\": \"${TAG}\"}" \ |
| 106 | + ${GITHUB_API}repos/${REPO_NAME}/${IMAGE_NAME}/releases |
| 107 | + fi |
| 108 | +
|
| 109 | +workflows: |
| 110 | + version: 2 |
| 111 | + build: |
| 112 | + jobs: |
| 113 | + - test |
| 114 | + - versioning: |
| 115 | + filters: |
| 116 | + branches: |
| 117 | + only: |
| 118 | + - master |
| 119 | + - push: |
| 120 | + requires: |
| 121 | + - versioning |
| 122 | + - gh_release: |
| 123 | + requires: |
| 124 | + - push |
0 commit comments