1version: 2.1
2
3parameters:
4  ssh-fingerprint:
5    type: string
6    default: ${GITHUB_SSH_FINGERPRINT}
7
8aliases:
9  # Workflow filters
10  - &filter-only-master
11    branches:
12      only: master
13  - &filter-only-release
14    branches:
15      only: /^v[1-9]*[0-9]+\.[1-9]*[0-9]+\.x$/
16
17workflows:
18  plugin_workflow:
19    jobs:
20      - yarn_install
21      - build_docs:
22          requires:
23            - yarn_install
24      - build_frontend:
25          requires:
26            - yarn_install
27      - build_backend:
28          requires:
29            - yarn_install
30      - package:
31          requires:
32            - build_frontend
33            - build_backend
34            - build_docs
35      - provisioning:
36          requires:
37            - build_frontend
38            - build_backend
39            - build_docs
40      - e2e_canary:
41          requires:
42            - provisioning
43            - package
44      - report:
45          requires:
46            - package
47            - e2e_canary
48      - approve_release:
49          type: approval
50          requires:
51            - report
52          filters: *filter-only-release
53      - publish_github_release:
54          requires:
55            - approve_release
56          filters: *filter-only-release
57
58executors:
59  default_exec: # declares a reusable executor
60    docker:
61      - image: srclosson/grafana-plugin-ci-alpine:latest
62  e2e_exec:
63    docker:
64      - image: srclosson/grafana-plugin-ci-e2e:latest
65
66jobs:
67  yarn_install:
68    executor: default_exec
69    steps:
70      - checkout
71      - restore_cache:
72          name: restore yarn cache
73          keys:
74          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
75      - run:
76          name: Install dependencies
77          command: |
78            mkdir ci
79            yarn bin grafana-toolkit ] || yarn install --immutable
80      - save_cache:
81          name: save yarn cache
82          paths:
83            - ~/project/.yarn
84          key: build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
85      - save_cache:
86          name: save cypress cache
87          paths:
88            - ~/.cache/Cypress
89          key: cypress-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
90
91  build_docs:
92    executor: default_exec
93    steps:
94      - checkout
95      - restore_cache:
96          name: restore yarn cache
97          keys:
98          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
99      - run:
100          name: Build docs
101          command: |
102            yarn run grafana-toolkit plugin:ci-docs
103            [ -d "dist" ] || circleci-agent step halt
104      - persist_to_workspace:
105          root: .
106          paths:
107            - dist
108
109  build_frontend:
110    executor: default_exec
111    steps:
112      - checkout
113      - restore_cache:
114          name: restore yarn cache
115          keys:
116          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
117      - run:
118          name: Build and test frontend
119          command: yarn run grafana-toolkit plugin:ci-build
120      - persist_to_workspace:
121          root: .
122          paths:
123            - dist
124
125  build_backend:
126    executor: default_exec
127    steps:
128      - checkout
129      - restore_cache:
130          name: restore yarn cache
131          keys:
132          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
133      - run:
134          name: Exit if no backend
135          command: |
136            [ -f "Magefile.go" ] || circleci-agent step halt
137      - run:
138          name: Build backend
139          command: mage -v buildAll
140      - run:
141          name: Test backend
142          command: |
143            mage -v lint
144            mage -v coverage
145      - persist_to_workspace:
146          root: .
147          paths:
148            - dist
149
150  package:
151    executor: default_exec
152    steps:
153    - checkout
154    - restore_cache:
155        name: restore yarn cache
156        keys:
157        - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
158    - attach_workspace:
159        at: .
160    - run:
161        name: Move results to ci folder
162        command: yarn run grafana-toolkit plugin:ci-build --finish
163    - run:
164        name: Package distribution
165        command: |
166          yarn run grafana-toolkit plugin:ci-package
167    - persist_to_workspace:
168        root: .
169        paths:
170        - ci/jobs/package
171        - ci/packages
172        - ci/dist
173        - ci/grafana-test-env
174    - store_artifacts:
175        path: ci
176
177  provisioning:
178    executor: default_exec
179    steps:
180    - run:
181      name: Continue if this plugin has a provisioning path
182      command: |
183        [ -z "${PROVISIONING_REPO_PATH}" ] && circleci-agent step halt
184    - checkout
185    - add_ssh_keys:
186        fingerprints:
187          - << pipeline.parameters.ssh-fingerprint >>
188    - run:
189        name: Checkout provisioning files
190        command: git clone --depth 1 git@github.com:grafana/plugin-provisioning.git
191    - run:
192        name: Prepare task output dir
193        command: |
194          mkdir ci # Avoid error if not exists
195          mkdir ci/jobs # Avoid error if not exists
196          mkdir ci/jobs/provisioning
197          mv plugin-provisioning/${PROVISIONING_REPO_PATH}/* ci/jobs/provisioning
198    - persist_to_workspace:
199        root: .
200        paths:
201        - ci/jobs/provisioning
202
203  e2e_canary:
204    executor: e2e_exec
205    steps:
206    - run:
207      name: Continue if this plugin has a provisioning path
208      command: |
209        [ -z "${PROVISIONING_REPO_PATH}" ] && circleci-agent step halt
210    - checkout
211    - attach_workspace:
212        at: .
213    - restore_cache:
214        name: Restore yarn cache
215        keys:
216        - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
217    - restore_cache:
218        name: Restore cypress cache
219        keys:
220        - cypress-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
221    - run:
222        name: Exit if no e2e tests configured
223        command: |
224          [ -d cypress ] || circleci-agent step halt
225    - run:
226        name: Setup Grafana (local install)
227        command: |
228          ginstall canary
229          /opt/grafana/bin/grafana-server -config ci/grafana-test-env/custom.ini -homepath /opt/grafana &
230          ## To make sure grafana has started up
231          while ! $(netstat -tulpn | grep 3000 >/dev/null 2>&1); do sleep 1; done
232          /opt/grafana/bin/grafana-cli --version
233    - run:
234        name: Copy provisioning files
235        command: cp -r ci/jobs/provisioning provisioning/
236    - run:
237        name: Run e2e tests
238        command: |
239          # If the tests fail, but GRAFANACI_STRICT_E2E=0, don't worry about it
240          yarn run grafana-e2e run \
241          || (
242            [ "$GRAFANACI_STRICT_E2E" -eq 0 ] && echo "Bypassing fail. ci-nostrict enabled"
243          )
244    - run:
245        when: always
246        name: Prepare task output dir
247        command: |
248          # TODO: probably move all of this to `@grafana/toolkit plugin:ci-test`
249          mkdir ci/jobs/e2e_canary
250          # only copy if they exist
251          cp cypress/screenshots/*.* ci/jobs/e2e_canary 2>/dev/null || :
252          cp cypress/videos/*.* ci/jobs/e2e_canary 2>/dev/null || :
253    - persist_to_workspace:
254        root: .
255        paths:
256        - ci/jobs/e2e_canary
257    - store_test_results:
258        path: ci/jobs/e2e_canary
259    - store_artifacts:
260        path: ci/jobs/e2e_canary
261
262  report:
263    executor: default_exec
264    steps:
265    - checkout
266    - attach_workspace:
267        at: .
268    - restore_cache:
269        name: restore yarn cache
270        keys:
271          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
272    - run:
273        name: Toolkit report
274        command: |
275          yarn run grafana-toolkit plugin:ci-report
276    - store_artifacts:
277        path: ci
278
279  publish_github_release:
280    executor: default_exec
281    steps:
282      - checkout
283      - add_ssh_keys:
284          fingerprints:
285            - "dc:60:ab:c7:2d:8c:82:50:2a:2a:97:1a:c0:66:83:14"
286      - attach_workspace:
287          at: .
288      - restore_cache:
289          name: restore yarn cache
290          keys:
291          - build-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "yarn.lock" }}
292      - run:
293          name: "Publish Release on GitHub"
294          command: |
295            yarn run grafana-toolkit plugin:github-publish
296