Skip to content

Commit

Permalink
chore: Create new build-repository action
Browse files Browse the repository at this point in the history
- Use shared build-repository action
- Update upload/download artifacts to v4
- Simplify unlock-dependencies custom action
  • Loading branch information
michaeldowseza committed Oct 23, 2024
1 parent b29f076 commit 92ef351
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 384 deletions.
125 changes: 0 additions & 125 deletions .github/actions/build-package/action.yml

This file was deleted.

127 changes: 127 additions & 0 deletions .github/actions/build-repository/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: Build repository
description: Builds a repository
inputs:
repository:
description: Name of the repository
required: true
skip-lint:
required: false
type: boolean
default: true
skip-tests:
required: false
type: boolean
default: false
cache-working-directory:
required: false
type: boolean
default: false
artifact-path:
type: string
description: An optional file, directory or wildcard pattern that describes what to upload
default: ''
artifact-name:
type: string
description: An optional artifact name. Required when artifact-path is specified
default: ''

defaults:
run:
shell: bash

runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ inputs.repository }}
- name: Set-up node
uses: actions/setup-node@v4
with:
node-version: 20

- name: Download dependencies artifacts
id: download-artifacts
uses: actions/download-artifact@v4
with:
pattern: cloudscape-design-*
path: ./.build-cache
merge-multiple: true

- name: Patch local dependencies
uses: cloudscape-design/actions/.github/actions/patch-local-dependencies@test/use-build-repository
` with:
path: ${{ github.workspace }}

- name: Unlock dependencies
uses: cloudscape-design/actions/.github/actions/unlock-dependencies@test/use-build-repository
- name: Install
shell: bash
run: npm install
- name: Build
shell: bash
run: npm run build
- name: Lint
if: ${{ inputs.skip-lint != 'true' }}
shell: bash
run: npm run lint
- name: Test
if: ${{ inputs.skip-tests != 'true' && inputs.repository != 'cloudscape-design/components' }}
shell: bash
run: npm run test
- name: Test:Unit
if: ${{ inputs.skip-tests != 'true' && inputs.repository == 'cloudscape-design/components' }}
shell: bash
run: npm run test:unit

- name: Cache working directory folder
if: ${{ inputs.cache-working-directory == 'true' }}
uses: actions/cache@v4
with:
path: ${{ github.workspace }}
key: ${{ inputs.repository }}-${{ github.event.pull_request.head.sha || github.sha }}

- name: Package lib
shell: bash
run: |
cd ./lib
if [ -f "package.json" ]; then
# If there's a package.json in ./lib, just pack it here
echo "Found package.json in ./lib, running npm pack in ./lib"
npm pack
else
# Otherwise, iterate through subdirectories and pack if package.json exists
echo "No package.json found in ./lib, iterating through subdirectories"
for dir in */; do
if [ -f "$dir/package.json" ]; then
cd "$dir"
npm pack
mv *.tgz ../
cd ..
else
echo "No package.json in $dir, skipping npm pack"
fi
done
fi
- name: Sanitize package name
shell: bash
id: sanitize
run: |
SANITIZED_NAME=$(echo "${{ inputs.repository }}" | sed 's/@//g' | sed 's/\//-/g')
echo "sanitized_name=$SANITIZED_NAME" >> $GITHUB_OUTPUT
echo "SANITIZED_NAME=$SANITIZED_NAME"
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ steps.sanitize.outputs.sanitized_name }}
path: lib/*.tgz

- name: Upload additional artifacts
if: ${{ inputs.artifact-path != '' && inputs.artifact-name != '' }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.artifact-name }}
path: ${{ inputs.artifact-path }}
2 changes: 1 addition & 1 deletion .github/actions/deploy-static/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runs:
using: composite
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ inputs.role-to-assume }}
aws-region: us-west-2
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/download-artifact/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inputs:
runs:
using: composite
steps:
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: ${{ inputs.name }}
path: ${{ inputs.path }}
Expand Down
18 changes: 6 additions & 12 deletions .github/actions/patch-local-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ inputs:
path:
description: Root directory of the package that should be updated
required: true
type:
description: 'How the dependencies should change. Possible values: "local" (to consume local tarballs), and "next" (to consume from pre-release CodeArtifact)'
default: local
required: false
tarball-dir:
description: Path to the directory containing the local tarballs
required: true
default: './.build-cache'
runs:
using: composite
steps:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- run: INPUT_PATH=${{ inputs.path }} INPUT_TYPE=${{ inputs.type }} node ${{ github.action_path }}/local.mjs
shell: bash
using: 'node20'
main: 'index.js'
18 changes: 18 additions & 0 deletions .github/actions/patch-local-dependencies/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Adapted from https://github.com/actions/toolkit/blob/main/packages/core/src/core.ts#L126C1-L138C2
function getInput(name, options) {
const val = process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''
if (options && options.required && !val) {
throw new Error(`Input required and not supplied: ${name}`)
}

if (options && options.trimWhitespace === false) {
return val
}

return val.trim()
}

module.exports = { getInput };
51 changes: 51 additions & 0 deletions .github/actions/patch-local-dependencies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

const fs = require('fs');
const path = require('path');

const core = require('./core');

const rootPath = core.getInput('path');
const tarballDir = core.getInput('tarball-dir');

const packageJsonFullPath = path.resolve(path.join(rootPath, 'package.json'));
const tarballDirFullPath = path.resolve(tarballDir);

if (!fs.existsSync(tarballDirFullPath)) {
console.log('No local tarball directory found. No local dependencies will be loaded.');
return;
}

if (!fs.existsSync(packageJsonFullPath)) {
throw new Error(`package.json not found at path: ${packageJsonFullPath}`);
}

const packageJsonData = JSON.parse(fs.readFileSync(packageJsonFullPath, 'utf8'));
const tarballFiles = fs.readdirSync(tarballDirFullPath).filter(file => file.endsWith('.tgz'));

const updateCloudscapeDependencies = dependencies => {
if (!dependencies) return {};
return Object.keys(dependencies).reduce((updatedDeps, key) => {
if (key.startsWith('@cloudscape-design/')) {
const tarball = tarballFiles.find(file => file.replace('cloudscape-design-', '').startsWith(key.replace('@cloudscape-design/', '')));
if (tarball) {
console.log(`Updating ${key} to tarball file: ${tarball}`);
updatedDeps[key] = `file:${path.join(tarballDirFullPath, tarball)}`;
} else {
console.log(`No tarball found for ${key}, skipping update.`);
updatedDeps[key] = dependencies[key];
}
} else {
updatedDeps[key] = dependencies[key];
}
return updatedDeps;
}, {});
};

packageJsonData.dependencies = updateCloudscapeDependencies(packageJsonData.dependencies);
packageJsonData.devDependencies = updateCloudscapeDependencies(packageJsonData.devDependencies);

fs.writeFileSync(packageJsonFullPath, JSON.stringify(packageJsonData, null, 2));

console.log(`Successfully updated @cloudscape-design/* dependencies to point to local tarballs.`);
5 changes: 0 additions & 5 deletions .github/actions/patch-local-dependencies/local.mjs

This file was deleted.

Loading

0 comments on commit 92ef351

Please sign in to comment.