Skip to content

Merge pull request #155 from edgio-docs/token-auth #577

Merge pull request #155 from edgio-docs/token-auth

Merge pull request #155 from edgio-docs/token-auth #577

name: Deploy Examples
on:
push:
branches:
- '**'
workflow_dispatch:
inputs:
deploy-all:
description: 'Deploys all examples to the live environment'
required: false
default: 'false'
type: boolean
# In case you push new commit into the MR / the previous runs will automatically stop
concurrency:
group: examples-${{ github.ref }}
cancel-in-progress: true
env:
# Node version for all the jobs
NODE_VERSION: 18
# Set env var based on branch name
GIT_REF: ${{ github.ref }}
EDGIO_ENV: ${{ github.ref == 'refs/heads/main' && 'live' || 'test' }}
EDGIO_TEAM: edgio-community
EDGIO_SITE: examples
EDGIO_DEPLOY_TOKEN: ${{secrets.EDGIO_EXAMPLES_DEPLOY_TOKEN}}
EDGIO_V7_DEPLOY_TOKEN: ${{secrets.EDGIO_V7_EXAMPLES_DEPLOY_TOKEN}}
COMMENT_ID: preview-${{ github.ref }}
DEPLOY_ALL: ${{ github.event.inputs.deploy-all }}
jobs:
prepare:
name: Prepare env for examples
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.setMatrix.outputs.matrix }}
branch: ${{ steps.setBranch.outputs.branch }}
pr: ${{ steps.finder.outputs.pr }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }} # NODE_VERSION
registry-url: https://registry.npmjs.org/
- name: Cache node modules
uses: actions/cache@v3
env:
cache-name: cache-packages-node-modules-node16-v1
with:
path: |
node_modules
*/*/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('package-lock.json', 'packages/**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- name: Extract branch name
id: setBranch
shell: bash
run: echo -e "branch=$(echo ${GITHUB_REF#refs/heads/} | sed 's/[^[:alnum:].-]/-/g')" >> $GITHUB_OUTPUT
- run: npm ci
- name: Get matrix of modified examples
id: setMatrix
run: |
matrix=$(node scripts/helpers/printModifiedExamples.js)
echo -e "matrix=$matrix" >> $GITHUB_OUTPUT
if [[ "$matrix" == "[]" ]]; then
echo "No jobs to run. Exiting workflow."
exit 0
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: jwalton/gh-find-current-pr@v1
id: finder
- name: Create comment for preview links
uses: marocchino/[email protected]
with:
number: ${{ steps.finder.outputs.pr }}
header: deploy-status
recreate: true
message: |
Preview links for commit ${{ github.sha }}:
examples:
name: Examples
needs: prepare
if: needs.prepare.outputs.matrix != '[]'
runs-on: ubuntu-latest
continue-on-error: true
strategy:
max-parallel: 5 # Prevent exceeding rate limit on Edgio platform during deployment
matrix:
example: ${{ fromJson( needs.prepare.outputs.matrix ) }}
env:
EXAMPLE_DIR: examples/${{ matrix.example }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }} # NODE_VERSION
registry-url: https://registry.npmjs.org/
# - uses: actions/setup-python@v2
# with:
# python-version: 2.7
- name: Cache node modules
uses: actions/cache@v3
env:
cache-name: cache-examples-node-modules-${{ matrix.example }}-node16-v2
with:
path: |
examples/${{ matrix.example }}/node_modules
*/*/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles(format('examples/{0}/package-lock.json', matrix.test), 'packages/*/package-lock.json') }}
# Install dependencies for this example
- name: Install dependencies
run: |
npm i -g @edgio/cli
# Check if package-lock.json file exists
if [ -f "package-lock.json" ]; then
# Use npm
npm ci --legacy-peer-deps
else
# Check if yarn.lock file exists
if [ -f "yarn.lock" ]; then
# Use yarn
yarn install --immutable
else
# No lock file found
echo "No lock file found. Please run 'npm ci' or 'yarn install --frozen-lockfile' to install dependencies."
fi
fi
working-directory: ${{ env.EXAMPLE_DIR }}
- name: Disable analytics
run: edgio config set-analytics false
- name: Get Edgio version and set environment variable
run: |
edgio_version=$(edgio --version)
echo "Edgio version is $edgio_version"
echo "EDGIO_VERSION=$edgio_version" >> $GITHUB_ENV
if [[ $edgio_version =~ ^7.* ]]; then
echo "ENV_CMD_ARGS=--site=$EDGIO_SITE --team=$EDGIO_TEAM --token=$EDGIO_V7_DEPLOY_TOKEN --ignore-error CONFIG_SOURCE_SWITCH" >> $GITHUB_ENV
else
echo "ENV_CMD_ARGS=--site=$EDGIO_SITE --team=$EDGIO_TEAM --token=$EDGIO_DEPLOY_TOKEN" >> $GITHUB_ENV
fi
- name: Prepare environments for the example
run: |
output=$(eval "edgio env list $ENV_CMD_ARGS")
# Get the lines that contain the list of environments
envs=$(echo "$output" | grep -A 1000 "Environments:")
# Remove the "Environments: " prefix from the first line and remove all lines before the first one
envs=$(echo "$envs" | tail -n +2)
# Replace all newline characters with a space
envs=$(echo "$envs" | tr '\n' ' ')
if [[ " $envs " =~ " $ENV_NAME " ]]; then
# environment exists in the list; skip it
echo "Environment '${ENV_NAME}' already exists in the list of environments and is being skipped."
else
# environment doesn't exist in the list; add it
eval "edgio env add $ENV_CMD_ARGS --environment=$ENV_NAME"
fi
env:
ENV_NAME: ${{ matrix.example }}-${{ env.EDGIO_ENV }}
- name: Validate package scripts
run: node scripts/helpers/validatePackageScripts.js ${{ env.EXAMPLE_DIR }}
- name: Deploy example
id: deploy
shell: bash
run: |
# Set branch argument only if BRANCH_NAME is not main
if [ "${{ env.BRANCH_NAME }}" != "main" ]; then
BRANCH_ARG="--branch=${{ env.BRANCH_NAME }}"
else
BRANCH_ARG=""
fi
# Run the command and redirect stderr to stdout; we return true
# to prevent the script from exiting if the command fails
DEPLOY=$(npm run edgio:deploy -- ${{ env.ENV_CMD_ARGS }} $BRANCH_ARG --environment=${{ env.ENV_NAME }} 2>&1) || true
# DEPLOY="${DEPLOY//'%'/'%25'}"
# DEPLOY="${DEPLOY//$'\n'/'%0A'}"
# DEPLOY="${DEPLOY//$'\r'/'%0D'}"
echo "$DEPLOY"
# Save the output of the command to the output variable
echo DEPLOY_OUTPUT=$DEPLOY >> $GITHUB_OUTPUT
# Get the permalink link from the output of the command
echo "$DEPLOY" |
sed -n 's/.*\(https.*-perma\.link\).*/\1/p' |
{
read permalink || permalink=""
if [[ -n $permalink ]]; then
echo "permalink=$permalink" >> $GITHUB_OUTPUT
else
echo "permalink=" >> $GITHUB_OUTPUT
fi
}
# Get the edge link from the output of the command
echo "$DEPLOY" |
sed -n 's/.*\(https.*\(-limelight\|\.edgio\)\.link\).*/\1/p' |
{
read edge || edge=""
if [[ -n $edge ]]; then
echo "edge=$edge" >> $GITHUB_OUTPUT
echo "status=✅ Successful!" >> $GITHUB_OUTPUT
exit 0
else
echo "edge=" >> $GITHUB_OUTPUT
echo "status=❌ See status check" >> $GITHUB_OUTPUT
exit 1
fi
}
env:
ENV_NAME: ${{ matrix.example }}-${{ env.EDGIO_ENV }}
BRANCH_NAME: ${{ needs.prepare.outputs.branch }}
working-directory: ${{ env.EXAMPLE_DIR }}
- name: Publish Preview Link
if: ${{ always() }}
uses: marocchino/[email protected]
with:
number: ${{ needs.prepare.outputs.pr }}
header: deploy-status
append: true
message: |
##### `${{ matrix.example }}`: ${{ steps.deploy.outputs.status }}
- Permalink: ${{ steps.deploy.outputs.permalink }}
- Edge: ${{ steps.deploy.outputs.edge }}