|
1 | | -version: 2.1 # use CircleCI 2.0 |
| 1 | +version: 2.1 # use CircleCI 2.1 |
2 | 2 | orbs: |
3 | | - codecov: codecov/codecov@1.0.5 |
| 3 | + codecov: codecov/codecov@4.0.1 |
4 | 4 | jobs: # basic units of work in a run |
5 | 5 | build: # runs not using Workflows must have a `build` job as entry point |
6 | 6 | docker: # run the steps with Docker |
7 | | - # CircleCI Go images available at: https://hub.docker.com/r/circleci/golang/ |
8 | | - - image: circleci/golang:1.13 # |
| 7 | + # Updated Go image with Go 1.21+ support for generics |
| 8 | + - image: cimg/go:1.21 |
9 | 9 | environment: |
10 | 10 | GO111MODULE: "on" |
11 | 11 | # CircleCI PostgreSQL images available at: https://hub.docker.com/r/circleci/postgres/ |
12 | | - # directory where steps are run. Path must conform to the Go Workspace requirements |
13 | | - working_directory: /go/src/github.com/thisisdevelopment/flashflood |
| 12 | + # directory where steps are run - updated for v2 module |
| 13 | + working_directory: ~/project |
14 | 14 |
|
15 | 15 | environment: # environment variables for the build itself |
16 | 16 | TEST_RESULTS: /tmp/test-results # path to where test results will be saved |
17 | 17 |
|
18 | 18 | steps: # steps that comprise the `build` job |
19 | | - - checkout # check out source code to working directory |
| 19 | + - run: |
| 20 | + name: Checkout code via HTTPS |
| 21 | + command: | |
| 22 | + git clone https://github.com/thisisdevelopment/flashflood.git . |
| 23 | + git checkout $CIRCLE_SHA1 |
20 | 24 | - run: mkdir -p $TEST_RESULTS # create the test results directory |
21 | 25 |
|
22 | 26 | - restore_cache: # restores saved cache if no changes are detected since last run |
23 | 27 | # Read about caching dependencies: https://circleci.com/docs/2.0/caching/ |
24 | 28 | keys: |
25 | | - - v1-pkg-cache |
| 29 | + - v2-pkg-cache-{{ checksum "go.sum" }} |
| 30 | + - v2-pkg-cache- |
| 31 | + |
| 32 | + # Install go-junit-report for test reporting |
| 33 | + - run: go install github.com/jstemmer/go-junit-report/v2@latest |
26 | 34 |
|
27 | | - # Normally, this step would be in a custom primary image; |
28 | | - # we've added it here for the sake of explanation. |
29 | | - - run: go get github.com/thisisdevelopment/flashflood |
30 | | - - run: go get github.com/jstemmer/go-junit-report |
| 35 | + # Download dependencies |
| 36 | + - run: |
| 37 | + name: Download dependencies |
| 38 | + command: go mod download |
| 39 | + |
| 40 | + # Verify dependencies and format |
| 41 | + - run: |
| 42 | + name: Verify and format code |
| 43 | + command: | |
| 44 | + go mod verify |
| 45 | + go fmt ./... |
31 | 46 |
|
| 47 | + # Run tests with our test runner |
32 | 48 | - run: |
33 | 49 | name: Run unit tests |
34 | 50 | command: | |
35 | | - trap "go-junit-report <${TEST_RESULTS}/go-test.out > ${TEST_RESULTS}/go-test-report.xml" EXIT |
36 | | - make test | tee ${TEST_RESULTS}/go-test.out |
| 51 | + # Use our test-runner.sh if available, otherwise use go test directly |
| 52 | + if [ -f "./test-runner.sh" ]; then |
| 53 | + chmod +x ./test-runner.sh |
| 54 | + ./test-runner.sh -v -c |
| 55 | + else |
| 56 | + go test -v -race ./... | tee ${TEST_RESULTS}/go-test.out |
| 57 | + fi |
| 58 | +
|
| 59 | + # Generate JUnit report if go test output exists |
| 60 | + - run: |
| 61 | + name: Generate JUnit report |
| 62 | + command: | |
| 63 | + if [ -f "${TEST_RESULTS}/go-test.out" ]; then |
| 64 | + cat ${TEST_RESULTS}/go-test.out | go-junit-report > ${TEST_RESULTS}/go-test-report.xml |
| 65 | + fi |
| 66 | + when: always |
37 | 67 |
|
| 68 | + # Run coverage separately to ensure we get the file |
38 | 69 | - run: |
39 | 70 | name: Run code coverage |
40 | 71 | command: | |
41 | | - go test -race -coverprofile=coverage.txt -covermode=atomic |
42 | | - mv coverage.txt /tmp/test-results/ |
| 72 | + go test -race -coverprofile=coverage.txt -covermode=atomic ./... |
| 73 | + if [ -f "coverage.txt" ]; then |
| 74 | + cp coverage.txt ${TEST_RESULTS}/coverage.txt |
| 75 | + fi |
43 | 76 |
|
44 | | - - run: make # pull and build dependencies for the project |
| 77 | + # Build the project to verify everything compiles |
| 78 | + - run: |
| 79 | + name: Build project |
| 80 | + command: | |
| 81 | + if [ -f "Makefile" ]; then |
| 82 | + make build-native |
| 83 | + else |
| 84 | + go build ./... |
| 85 | + fi |
45 | 86 |
|
46 | 87 | - save_cache: # Store cache in the /go/pkg directory |
47 | | - key: v1-pkg-cache |
| 88 | + key: v2-pkg-cache-{{ checksum "go.sum" }} |
48 | 89 | paths: |
49 | | - - "/go/pkg" |
| 90 | + - "/home/circleci/go/pkg" |
50 | 91 |
|
51 | 92 | - store_artifacts: # Upload test summary for display in Artifacts: https://circleci.com/docs/2.0/artifacts/ |
52 | 93 | path: /tmp/test-results |
|
0 commit comments