Skip to content

fix: try fix issue got Invalid type of on #5

fix: try fix issue got Invalid type of on

fix: try fix issue got Invalid type of on #5

name: Build and push assets
on:
workflow_call:
inputs:
NPM_REGISTRY_DOMAIN:
description: Domain of the private npm registry.
default: https://npm.pkg.github.com/
required: false
type: string
NODE_VERSION:
description: Node version with which the assets will be compiled.
default: 16
required: false
type: string
WORKING_DIRECTORY:
description: Working directory path.
default: './'
required: false
type: string
PACKAGE_MANAGER:
description: Package manager with which the dependencies should be installed (`npm` or `yarn`).
type: string
default: 'auto'
required: false
DEPS_INSTALL:
description: Whether or not to install dependencies before compiling.
type: boolean
default: true
required: false
COMPILE_SCRIPT_PROD:
description: Script added to "npm run" or "yarn" to build production assets.
type: string
default: 'encore prod'
required: false
COMPILE_SCRIPT_DEV:
description: Script added to "npm run" or "yarn" to build development assets.
type: string
default: 'encore dev'
required: false
ASSETS_TARGET_PATHS:
description: Target path(s) for compiled assets.
default: './assets'
required: false
type: string
NODE_OPTIONS:
description: Space-separated list of command-line Node options.
type: string
default: ''
required: false
MAIN_BRANCH_NAME:
description: Main repository branch ("main" or "master" usually).
type: string
required: true
RELEASE_BRANCH_NAME:
description: Branch name where consequentially tags are moved.
type: string
default: ''
required: true
INSTALL_WP_CLI:
description: Whether to Install WP cli or not.
default: false
required: false
type: boolean
PHP_VERSION:
description: PHP version with which the WP CLI is going to be executed.
default: "8.1"
required: false
type: string
BRANCHES_USING_COMPILE_SCRIPT_PROD:
description: Branches that should use the COMPILE_SCRIPT_PROD script
default: []
required: false
type: choice
secrets:
NPM_REGISTRY_TOKEN:
description: Authentication for the private npm registry.
required: false
GITHUB_USER_EMAIL:
description: Email address for the GitHub user configuration.
required: true
GITHUB_USER_NAME:
description: Username for the GitHub user configuration.
required: true
GITHUB_USER_SSH_KEY:
description: Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME`.
required: false # it's required if we need to bypass branch protection
jobs:
checks:
runs-on: ubuntu-latest
outputs:
is_development_branch_last_commit: ${{ github.sha == steps.detect_development_branch_last_commit.outputs.development_branch_last_commit && 'yes' || 'no' }}
is_moved_tag: ${{ (github.ref_type == 'tag' && contains(steps.detect_tag_annotation.outputs.tag_annotation, github.workflow)) && 'yes' || 'no' }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ inputs.MAIN_BRANCH_NAME }} # it is not possible to get tag annotation from detached state https://github.com/actions/runner/issues/712
- name: Find last commit
id: detect_development_branch_last_commit
run: |
echo "development_branch_last_commit=$(git --no-pager log -n 1 --pretty=tformat:'%H')" >> "$GITHUB_OUTPUT"
- name: Find tag message
id: detect_tag_annotation
if: ${{ github.ref_type == 'tag' }}
run: |
echo $(git --no-pager tag -l --format='%(contents)' ${{ github.ref_name }})
echo "tag_annotation=$(git --no-pager tag -l --format='%(contents)' ${{ github.ref_name }})" >> "$GITHUB_OUTPUT"
compile-assets:
needs: checks
if: ${{ (github.ref_type == 'branch' && !contains(github.ref_name, '-built') ) || (github.ref_type == 'tag' && needs.checks.outputs.is_moved_tag == 'no') }}
defaults:
run:
working-directory: ${{ inputs.WORKING_DIRECTORY }}
timeout-minutes: 10
runs-on: ubuntu-latest
env:
NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
GITHUB_USER_SSH_KEY: ${{ secrets.GITHUB_USER_SSH_KEY }}
COMPILE_SCRIPT: ${{ inputs.COMPILE_SCRIPT_DEV }} # we'll override if the push is for tag
TAG_NAME: '' # we'll override if the push is for tag
TAG_BRANCH_NAME: '' # we'll override if the push is for tag
LOCK_FILE: '' # we'll override after checking files
PACKAGE_MANAGER: 'yarn' # we'll override based on env/inputs
BRANCHES_USING_COMPILE_SCRIPT_PROD: ${{ inputs.BRANCHES_USING_COMPILE_SCRIPT_PROD }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
ssh-key: ${{ secrets.GITHUB_USER_SSH_KEY }}
- name: Set up SSH
if: ${{ env.GITHUB_USER_SSH_KEY != '' }}
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.GITHUB_USER_SSH_KEY }}
- name: Setup Git
run: |
git config --global user.email "${{ secrets.GITHUB_USER_EMAIL }}"
git config --global user.name "${{ secrets.GITHUB_USER_NAME }}"
git config --global advice.addIgnoredFile false
git config --global push.autoSetupRemote true
- name: Set branch environment variables
if: ${{ github.ref_type == 'branch' }}
run: |
echo "BUILT_BRANCH_NAME=${{ github.ref_name }}-built" >> $GITHUB_ENV
- name: Set tag environment variables
if: ${{ github.ref_type == 'tag' }}
run: |
echo "TAG_NAME=$(echo ${GITHUB_REF#refs/*/})" >> $GITHUB_ENV
echo "COMPILE_SCRIPT=${{ inputs.COMPILE_SCRIPT_PROD }}" >> $GITHUB_ENV
- name: Set branch COMPILE_SCRIPT
if: ${{ github.ref_type == 'branch' && contains(env.BRANCHES_USING_COMPILE_SCRIPT_PROD, github.ref_name) }}
run: |
echo "COMPILE_SCRIPT=${{ inputs.COMPILE_SCRIPT_PROD }}" >> $GITHUB_ENV
- name: Checkout and merge the built branch
if: ${{ github.ref_type == 'branch' }}
run: |
git checkout ${{ env.BUILT_BRANCH_NAME }} || git checkout -b ${{ env.BUILT_BRANCH_NAME }}
git merge ${{ github.ref_name }}
- name: Checkout and merge the release branch
if: ${{ github.ref_type == 'tag' && needs.checks.outputs.is_development_branch_last_commit == 'yes' && inputs.RELEASE_BRANCH_NAME != ''}}
run: |
git checkout ${{ inputs.MAIN_BRANCH_NAME }}
git checkout ${{ inputs.RELEASE_BRANCH_NAME }} || git checkout -b ${{ inputs.RELEASE_BRANCH_NAME }}
git merge ${{ inputs.MAIN_BRANCH_NAME }}
- name: Checkout temporary tag branch
if: ${{ github.ref_type == 'tag' && (needs.checks.outputs.is_development_branch_last_commit == 'no' || inputs.RELEASE_BRANCH_NAME == '') }}
run: |
git checkout -b bot/compiled-assets/${{ github.sha }}
echo "TAG_BRANCH_NAME=bot/compiled-assets/${{ github.sha }}" >> $GITHUB_ENV
- name: Prepare directories
run: |
mkdir -p ${{ inputs.ASSETS_TARGET_PATHS }}
- name: Try determining package manager by lock file
if: ${{ inputs.PACKAGE_MANAGER == 'auto' }}
run: |
[ -f "${GITHUB_WORKSPACE}/package-lock.json" ] && echo "LOCK_FILE=npm" >> $GITHUB_ENV || echo "No package-lock.json found"
[ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ] && echo "LOCK_FILE=npm" >> $GITHUB_ENV || echo "No npm-shrinkwrap.json found"
[ -f "${GITHUB_WORKSPACE}/yarn.lock" ] && echo "LOCK_FILE=yarn" >> $GITHUB_ENV || echo "No yarn.lock found"
- name: Warning for fallback package manager
if: ${{ (inputs.PACKAGE_MANAGER == 'auto') && (env.LOCK_FILE == '') }}
run: echo "::warning::PACKAGE_MANAGER input not defined, and no lock file found, using Yarn as default."
- name: Determine package manager
if: ${{ (env.LOCK_FILE == 'npm') || (inputs.PACKAGE_MANAGER == 'npm') }}
run: echo "PACKAGE_MANAGER=npm" >> $GITHUB_ENV
- name: Set up node
uses: actions/setup-node@v3
with:
node-version: ${{ inputs.NODE_VERSION }}
registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }}
cache: ${{ env.PACKAGE_MANAGER }}
- name: Install WP CLI
if: ${{ inputs.INSTALL_WP_CLI }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.PHP_VERSION }}
tools: wp-cli
coverage: none
- name: Install dependencies
if: ${{ inputs.DEPS_INSTALL }}
run: ${{ ((env.PACKAGE_MANAGER == 'yarn') && 'yarn') || 'npm install' }}
- name: Compile assets
run: ${{ ((env.PACKAGE_MANAGER == 'yarn') && 'yarn') || 'npm run' }} ${{ env.COMPILE_SCRIPT }}
- name: Git add, commit, push
run: |
declare -a TARGET_PATHS_ARRAY=(${{ inputs.ASSETS_TARGET_PATHS }})
for path in "${TARGET_PATHS_ARRAY[@]}"; do git add -f "${path}/*"; done
git add -A
git commit -m "[BUILD] ${{ github.sha }}" --no-verify || echo "No changes to commit"
git push
- name: Move tag
if: ${{ github.ref_type == 'tag' }}
run: |
git tag -d ${{ env.TAG_NAME }}
git push origin :refs/tags/${{ env.TAG_NAME }}
git tag -a -m "[RELEASE] ${{ github.sha }} // Released by '${{ github.workflow }}'" ${{ env.TAG_NAME }}
git push origin --tags
- name: Delete temporary tag branch
if: ${{ always() && (env.TAG_BRANCH_NAME != '') }}
run: |
git checkout --detach
git branch -d ${{ env.TAG_BRANCH_NAME }}
git push origin --delete ${{ env.TAG_BRANCH_NAME }}