Skip to content

Commit

Permalink
🏗 Automate bento publishing with a GitHub Action (ampproject#34430)
Browse files Browse the repository at this point in the history
  • Loading branch information
estherkim authored and rochapablo committed Aug 30, 2021
1 parent cd89511 commit cf488a1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/publish-npm-packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Publish Bento Packages on NPM
on:
workflow_dispatch:
inputs:
ampversion:
description: 'AMP version'
required: true
tag:
description: 'NPM package tag (latest | nightly)'
required: true
jobs:
setup:
runs-on: ubuntu-latest
outputs:
extensions: ${{ steps.get-extensions.outputs.extensions }}
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.events.inputs.ampversion }}
- name: Get extensions to publish
id: get-extensions
run: |
EXTENSIONS=$(node ./build-system/npm-publish/get-extensions.js)
echo "::set-output name=extensions::{\"include\":${EXTENSIONS}}"
publish:
if: github.repository == 'ampproject/amphtml'
environment: NPM_TOKEN #TODO: change environment name in repo settings
needs: setup
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.setup.outputs.extensions) }}
fail-fast: false
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.events.inputs.ampversion }}
- uses: actions/setup-node@v2
with:
check-latest: true
registry-url: https://registry.npmjs.org
- name: Build package
run: |
npm install
node ./build-system/npm-publish/build-npm-binaries.js ${{ matrix.extension }}
node ./build-system/npm-publish/write-package-jsons.js ${{ matrix.extension }} ${{ github.event.inputs.ampversion }}
- name: Publish v1
run: npm publish ./extensions/${{ matrix.extension }}/1.0 --access public --tag ${{ github.event.inputs.tag }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish v2 if it exists
run: |
if $(test -d ${{ github.workspace }}/extensions/${{ matrix.extension }}/2.0); then
npm publish ./extensions/${{ matrix.extension }}/2.0 --access public --tag ${{ github.event.inputs.tag }}
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

const [extension] = process.argv.slice(2);
const {timedExecOrDie} = require('../../../build-system/pr-check/utils');
const {updatePackages} = require('../../../build-system/common/update-packages');
const {timedExecOrDie} = require('../pr-check/utils');
const {updatePackages} = require('../common/update-packages');

updatePackages();
timedExecOrDie(`amp build --extensions=${extension} --core_runtime_only`);
Expand Down
27 changes: 27 additions & 0 deletions build-system/npm-publish/get-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* 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.
*/

/**
* @fileoverview
* Gets Bento components to publish.
*/

const bundles = require('../compile/bundles.config.extensions.json');
const extensions = bundles
.filter((bundle) => bundle.options?.npm)
.map((bundle) => ({'extension': bundle.name}));
console /*OK*/
.log(JSON.stringify(extensions));
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

const [extension, ampVersion] = process.argv.slice(2);
const {log} = require('../../../build-system/common/logging');
const {log} = require('../common/logging');
const {stat, writeFile} = require('fs/promises');
const {valid} = require('semver');

Expand All @@ -38,7 +38,11 @@ async function writePackageJson(extensionVersion) {
const minor = ampVersion.slice(0, 10);
const patch = Number(ampVersion.slice(-3)); // npm trims leading zeroes in patch number, so mimic this in package.json
const version = `${major}.${minor}.${patch}`;
if (!valid(version) || ampVersion.length != 13 || extensionVersionArr[1] !== '0') {
if (
!valid(version) ||
ampVersion.length != 13 ||
extensionVersionArr[1] !== '0'
) {
log(
'Invalid semver version',
version,
Expand Down

0 comments on commit cf488a1

Please sign in to comment.