From bf340d280bbfd31beb6a3194124f003637cad470 Mon Sep 17 00:00:00 2001 From: hoonoh Date: Thu, 31 Oct 2019 20:31:56 +0900 Subject: [PATCH] ci: npmrc update & add ec2-types sync schedule (#24) * ci: add default npmrc and remove npmrc generation * chore: rename util to scripts * ci: add ec2-types sync schedule * ci: revert changes related with npmrc * ci: remove git status checks & add aws secrets * ci(scripts): exit(1) on credential failure * ci: reorder steps and update snapshot * ci: update exclude patterns --- .azure-pipelines.yml | 2 +- .codeclimate.yml | 2 +- .github/workflows/ec2-gen.yml | 45 +++++++++++++++++++ package.json | 2 +- {util => scripts}/generate-ec2-types.ts | 9 +++- .../generate-spot-prices-mock-data.ts | 0 src/lib/lib.ts | 26 +++++++---- 7 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/ec2-gen.yml rename {util => scripts}/generate-ec2-types.ts (96%) rename {util => scripts}/generate-spot-prices-mock-data.ts (100%) diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index 24302538..0b08320f 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -20,7 +20,7 @@ jobs: - bash: | echo "##vso[task.setvariable variable=PACKAGE_VERSION]$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]')" echo "##vso[task.setvariable variable=CURRENT_GIT_TAG]$(git describe --tags | tr -d '[:space:]')" - echo "//registry.npmjs.org/:_authToken=$(NPM_TOKEN)" >> .npmrc + echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n" >> .npmrc - script: npm dist-tag add aws-spot-price@$(PACKAGE_VERSION) $(CURRENT_GIT_TAG) displayName: "NPM dist-tag $(CURRENT_GIT_TAG)" env: diff --git a/.codeclimate.yml b/.codeclimate.yml index 0042efca..dd3e0c50 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -17,10 +17,10 @@ exclude_patterns: - "package.json" - "patches/" - "README.md" + - "scripts/" - "src/**/*.spec.ts" - "test/" - "tsconfig.json" - - "util/" - "yarn.lock" - sonar-project.properties - sonar-project.properties diff --git a/.github/workflows/ec2-gen.yml b/.github/workflows/ec2-gen.yml new file mode 100644 index 00000000..dfc229a3 --- /dev/null +++ b/.github/workflows/ec2-gen.yml @@ -0,0 +1,45 @@ +name: keep ec2 types up to date + +on: + schedule: + - cron: "0 0 * * *" + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + + - name: Install dependencies + run: yarn --frozen-lockfile + + - name: Generate ec2-types + run: yarn build:ec2-types + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + + - name: Build + run: yarn build + + - name: Check linting + run: yarn lint + + - name: Check types + run: yarn lint:types + + - name: Run tests and update snapshots + run: yarn test -u + + - name: Log git diffs + run: git diff + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v1.6.0-multi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + COMMIT_MESSAGE: "fix: update ec2-types" + PULL_REQUEST_TITLE: "fix: update ec2-types" + PULL_REQUEST_BRANCH: ec2-types/patch + BRANCH_SUFFIX: none diff --git a/package.json b/package.json index 3612d7bc..14743e37 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "scripts": { "prepublishOnly": "yarn clean && yarn build", "clean": "rm -rf dist && rm -rf coverage", - "build:ec2-types": "ts-node -T util/generate-ec2-types.ts", + "build:ec2-types": "ts-node -T scripts/generate-ec2-types.ts", "build": "patch-package && webpack", "changelog": "conventional-changelog -i CHANGELOG.md -s -p angular", "test": "patch-package && jest --runInBand --verbose", diff --git a/util/generate-ec2-types.ts b/scripts/generate-ec2-types.ts similarity index 96% rename from util/generate-ec2-types.ts rename to scripts/generate-ec2-types.ts index a1916021..46236151 100644 --- a/util/generate-ec2-types.ts +++ b/scripts/generate-ec2-types.ts @@ -90,7 +90,14 @@ const sortInstances = (i1: string, i2: string): number => { }; const getEc2Types = async (): Promise => { - const allInstances = (await getGlobalSpotPrices({ silent: true })).reduce( + let prices; + try { + prices = await getGlobalSpotPrices({ silent: true }); + } catch (error) { + console.log(`getGlobalSpotPrices error: ${error}`); + process.exit(1); + } + const allInstances = prices.reduce( (list, cur) => { if (cur.InstanceType && !list.includes(cur.InstanceType)) list.push(cur.InstanceType); return list; diff --git a/util/generate-spot-prices-mock-data.ts b/scripts/generate-spot-prices-mock-data.ts similarity index 100% rename from util/generate-spot-prices-mock-data.ts rename to scripts/generate-spot-prices-mock-data.ts diff --git a/src/lib/lib.ts b/src/lib/lib.ts index 9f845707..e721b6b7 100644 --- a/src/lib/lib.ts +++ b/src/lib/lib.ts @@ -38,7 +38,7 @@ class Ec2SpotPriceError extends Error { Object.setPrototypeOf(this, Ec2SpotPriceError.prototype); } - readonly region: string; + readonly region: Region; readonly code: string; } @@ -87,7 +87,13 @@ const getEc2SpotPrice = async (options: { rtn = list.filter(history => history.InstanceType).sort(sortSpotPrice); } } catch (error) { - if (error && error.code && (error.code === 'AuthFailure' || error.code === 'OptInRequired')) { + if ( + error && + error.code && + (error.code === 'AuthFailure' || + error.code === 'OptInRequired' || + error.code === 'CredentialsError') + ) { throw new Ec2SpotPriceError(error.message, region, error.code); } else { console.error( @@ -173,12 +179,16 @@ export const getGlobalSpotPrices = async (options?: { return regionsPrices; } catch (error) { /* istanbul ignore if */ - if (error instanceof Ec2SpotPriceError && spinner) { - spinner.fail(`Failed to retrieve data from ${error.region}. (${error.code})`); - spinner = ora({ - text: spinnerText || spinner.text, - discardStdin: false, - }).start(); + if (error instanceof Ec2SpotPriceError) { + if (spinner) { + spinner.fail(`Failed to retrieve data from ${error.region}. (${error.code})`); + spinner = ora({ + text: spinnerText || spinner.text, + discardStdin: false, + }).start(); + } else { + throw error; + } } else { console.error(error); }