From ae95ec4834cc66e6b271e478e4383d769ad91bb7 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 20 Mar 2018 10:02:58 -0700 Subject: [PATCH] chore: setup nightly builds workflow (#1074) * chore: setup nightly builds * use CircleCI token to authenticate * never fail, always return something * we don't have package-lock.json in samples --- .circleci/config.yml | 25 ++++++++++- .circleci/get_workflow_name.py | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 .circleci/get_workflow_name.py diff --git a/.circleci/config.yml b/.circleci/config.yml index 6187d990d0a..631eea585bc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,8 +1,22 @@ --- # "Include" for unit tests definition. + +remove_package_lock: &remove_package_lock + name: Remove package-lock.json if needed. + command: | + WORKFLOW_NAME=`python .circleci/get_workflow_name.py` + echo "Workflow name: $WORKFLOW_NAME" + if [ "$WORKFLOW_NAME" = "nightly" ]; then + echo "Nightly build detected, removing package-lock.json" + rm -f package-lock.json + else + echo "Not a nightly build, skipping this step." + fi + unit_tests: &unit_tests steps: - checkout + - run: *remove_package_lock - run: name: Install modules and dependencies. command: npm install @@ -22,6 +36,7 @@ unit_tests: &unit_tests test_samples: &test_samples steps: - checkout + - run: *remove_package_lock - run: npm install - run: mkdir ~/.npm-packages - run: echo "prefix = $HOME/.npm-packages" >> ~/.npmrc @@ -32,7 +47,7 @@ version: 2.0 workflows: version: 2 tests: - jobs: + jobs: &workflow_jobs - node4: filters: tags: @@ -60,6 +75,14 @@ workflows: ignore: /.*/ tags: only: /^v[\d.]+$/ + nightly: + triggers: + - schedule: + cron: 0 7 * * * + filters: + branches: + only: master + jobs: *workflow_jobs jobs: node4: diff --git a/.circleci/get_workflow_name.py b/.circleci/get_workflow_name.py new file mode 100644 index 00000000000..3f338ab1d67 --- /dev/null +++ b/.circleci/get_workflow_name.py @@ -0,0 +1,79 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Get workflow name for the current build using CircleCI API. +Would be great if this information is available in one of +CircleCI environment variables, but it's not there. +https://circleci.ideas.aha.io/ideas/CCI-I-295 +""" + +import json +import os +import sys +import urllib2 + + +def main(): + try: + username = os.environ['CIRCLE_PROJECT_USERNAME'] + reponame = os.environ['CIRCLE_PROJECT_REPONAME'] + build_num = os.environ['CIRCLE_BUILD_NUM'] + except: + sys.stderr.write( + 'Looks like we are not inside CircleCI container. Exiting...\n') + print 'undefined' + return 0 + + try: + token = os.environ['CIRCLECI_TOKEN'] + except: + sys.stderr.write( + 'Looks like we we don\'t have CircleCI environment set up. Exiting...\n' + ) + print 'undefined' + return 0 + + try: + request = urllib2.Request( + "https://circleci.com/api/v1.1/project/github/%s/%s/%s?circle-token=%s" + % (username, reponame, build_num, token), + headers={"Accept": "application/json"}) + contents = urllib2.urlopen(request).read() + except: + sys.stderr.write('Cannot query CircleCI API. Exiting...\n') + print 'undefined' + return 0 + + try: + build_info = json.loads(contents) + except: + sys.stderr.write( + 'Cannot parse JSON received from CircleCI API. Exiting...\n') + print 'undefined' + return 0 + + try: + workflow_name = build_info['workflows']['workflow_name'] + except: + sys.stderr.write( + 'Cannot get workflow name from CircleCI build info. Exiting...\n') + print 'undefined' + return 0 + + print workflow_name + return 0 + + +retval = main() +exit(retval)