Skip to content

Commit

Permalink
Add actions
Browse files Browse the repository at this point in the history
  • Loading branch information
edlu77 committed Feb 13, 2024
1 parent 9cb7db9 commit 762c165
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/actions/deploy-comment/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Create Deploy Comment
description: Creates and Updates the deploy Comment
inputs:
deploy-url:
description: Url for deployed preview
required: true
runs:
using: composite
steps:
- name: Find Comment
uses: peter-evans/find-comment@v2
id: 'fc'
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: '# :rocket: Preview Deploy Report'

- name: Create Comment
if: steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
# :rocket: Preview Deploy Report
✅ Successfully deployed preview [here](${{ inputs.deploy-url }})
- name: Update Comment
if: steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{steps.fc.outputs.comment-id}}
edit-mode: replace
body: |
# :rocket: Preview Deploy Report Updated
✅ Successfully deployed preview [here](${{ inputs.deploy-url }})
75 changes: 75 additions & 0 deletions .github/actions/prepare-deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Prepare a deploy build
description: Build apps, storybook, and compodoc and generates deploy files

inputs:
main-branch-name:
description: Main branch name against which nx builds
required: true
app-configuration:
description: App build target configuration
required: true
nx-command:
description: Command used to build artifacts ('affected' or 'run-many')
required: false
default: affected
deploy-directory:
description: Deployment directory
required: false
default: deploy
skip-node-js:
description: Skip Node.js setup
required: false
default: 'false'
build-compodoc:
description: Whether to build code documentation
required: false
default: 'true'
build-storybook:
description: Whether to build storybook
required: false
default: 'true'

runs:
using: composite
steps:
- name: Node.js setup
if: ${{ inputs.skip-node-js == 'false' }}
uses: ./.github/actions/setup-node-js
- name: Configure nx
uses: nrwl/nx-set-shas@v3
with:
main-branch-name: ${{ inputs.main-branch-name }}

- name: Build apps
shell: bash
run: npx nx ${{ inputs.nx-command }} --targets=build --configuration=${{ inputs.app-configuration }}
- name: Build compodoc
if: ${{ inputs.build-compodoc == 'true' }}
shell: bash
run: npx nx ${{ inputs.nx-command }} --targets=compodoc --configuration=ci

- name: Build storybook
if: ${{ inputs.build-storybook == 'true' }}
shell: bash
run: |
# Run one at a time due to https://github.com/nrwl/nx/issues/6842
# Will hopefully be fixed by backport of https://github.com/storybookjs/storybook/pull/19307
npx nx ${{ inputs.nx-command }} --targets=build-storybook --configuration=ci --parallel=false
env:
STORYBOOK_DISABLE_TELEMETRY: '1'

- name: Collect artifacts
id: collect
shell: bash
run: ${{ github.action_path }}/collect_artifacts.sh ${{ inputs.deploy-directory }} dist/{apps,compodoc,storybook}

- name: Generate index file
uses: cuchi/[email protected]
with:
template: ${{ github.action_path }}/index.html.j2
output_file: ${{ inputs.deploy-directory }}/index.html
strict: true
variables: |
apps=${{ steps.collect.outputs.apps }}
storybook=${{ inputs.build-storybook == 'true' && steps.collect.outputs.storybook || '' }}
compodoc=${{ inputs.build-compodoc == 'true' && steps.collect.outputs.compodoc || '' }}
40 changes: 40 additions & 0 deletions .github/actions/prepare-deploy/collect_artifacts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
#
# Collects built artifacts and moves them to the specified directory
# Arguments:
# Output directory
# One or more input directories to search
# Outputs:
# Writes an output for each input directory to github outputs
# Each entry contains the name of the directory as the key and
# a comma separated list of copied directories as the value

shopt -s extglob

#######################################
# Main logic
#######################################
out_dir="$1"
mkdir -p "${out_dir}"
shift

while (( "$#" )); do
if [[ -d $1 ]]; then
# Extract directory name (https://stackoverflow.com/a/1371283)
dir_name="${1%%+(/)}"
dir_name="${dir_name##*/}"
dir_name="${dir_name:-/}"

# Copy directory
cp -r "$1" "${out_dir}"

# List subdirectories as a comma separated list
sub_dirs=$(echo "${out_dir}/${dir_name}"/*)
sub_dirs="${sub_dirs[*]//$out_dir\/}"
sub_dirs="${sub_dirs[*]//$' '/,}"

# Write outputs
echo "${dir_name}=${sub_dirs}" >> "${GITHUB_OUTPUT}"
fi
shift
done
34 changes: 34 additions & 0 deletions .github/actions/prepare-deploy/index.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html>
<head>
<title>Build preview for {{ env['GITHUB_REF_NAME'] }}</title>
</head>
<body>
{% macro createLinkEntry(dir) %}
<li>
<a href="{{ dir }}/index.html">{{ dir.split('/')[-1] }}</a>
</li>
{% endmacro %}

{% macro createLinks(header, dirs_str) %}
{% if dirs_str %}
<h1>{{ header }}</h1>
<ul>
{% for dir in dirs_str.split(',') %}
{{ createLinkEntry(dir) }}
{% endfor %}
</ul>
{% endif %}
{% endmacro %}

{% if not (apps or storybook or compodoc) %}
<p>
No app, storybook, or compodoc were affected by the current changes!
</p>
{% endif %}

{{ createLinks('Apps', apps) }}
{{ createLinks('Storybook', storybook) }}
{{ createLinks('Compodoc', compodoc) }}
</body>
</html>
33 changes: 33 additions & 0 deletions .github/actions/setup-node-js/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Setup node environment
description: Install Node.js and all dependencies using the cache when available

inputs:
node-version:
description: Node.js version
required: false
default: '16'

runs:
using: composite
steps:
- name: Use Node.js ${{ inputs.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ inputs.node-version }}
- name: Cache .npm
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Cache node_modules
id: modules_cache
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
shell: bash
if: ${{ steps.modules_cache.outputs.cache-hit != 'true' }}
run: npm ci --ignore-scripts

0 comments on commit 762c165

Please sign in to comment.