Skip to content

Feature: Workflow now Deploys Functions + some edits #2

Feature: Workflow now Deploys Functions + some edits

Feature: Workflow now Deploys Functions + some edits #2

Workflow file for this run

name: Invoke All Functions
on:
pull_request:
paths-ignore:
- '.github/**'
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
languages: ${{ steps.prep-matrix.outputs.languages }}
language_paths: ${{ steps.prep-matrix.outputs.language_paths }}
env:
FUNC_VERSION: "knative-v1.16.1"
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Prep Matrix
id: prep-matrix
run: |
## NOTE: ls -d returns absolute path
## GITHUB_WORKSPACE is the root directory
language_paths="$(ls -d ${GITHUB_WORKSPACE}/*/ | jq -R -s 'split("\n")[:-1]')"
languages="$(ls -d ${GITHUB_WORKSPACE}/*/ | xargs -n 1 basename | jq -R -s 'split("\n")[:-1]')"
# filter out 'hack/' directory
language_paths=$(printf "%s\n" "${language_paths[@]}" | grep -v "hack")
echo "filtered list: $language_paths"
languages=$(printf "%s\n" "${languages[@]}" | grep -v "hack")
echo "filtered languages: $languages"
# set output
echo language_paths=$language_paths >> $GITHUB_OUTPUT
echo languages=$languages >> $GITHUB_OUTPUT
# - name: Set up evironment
# run: hack/install-binaries.sh
deploy:
needs: prepare
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
language: ${{ fromJSON(needs.prepare.outputs.languages) }}
env:
ACTIONS_STEP_DEBUG: true
language_paths: ${{needs.prepare.outputs.language_paths}}
# UPDATE THIS IF HOST BUILDER IS ENABLED FOR MORE LANGUAGES
HOST_ENABLED_LANGUAGES: '["go"]'
HEADREF: ${{github.head_ref}}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install func
uses: gauron99/knative-func-action@main
with:
version: ${{ env.FUNC_VERSION }}
name: f
- name: Build & Invoke Function
run: |
# series of commands to build & invoke the Function (with specifics per language)
# Might change this to <one "run" per language> scenario because then
# I would just need to check the matrix language for which one to run
# DETERMINE IF HOST BUILDER SHOULD BE USED
## NOTE: HOST_ENABLED_LANGUAGES MUST BE UP TO DATE
if echo '${{env.HOST_ENABLED_LANGUAGES}}' | jq -r ".[] | select(. == \"${{matrix.language}}\")" | grep -q .; then
HOST_ENABLED=true
else
HOST_ENABLED=false
fi
language=${{matrix.language}}
echo "Current language is $language"
echo "host enabled? $HOST_ENABLED"
# This takes the array of paths, wraps it in single quotes for jq, then
# selects only the value that matches with language to get current
# language AND its full path (where the func template is)
language_path=$(echo '${{env.language_paths}}' | jq -r ".[] | select(contains(\"${{matrix.language}}\"))")
## use the Pull request environment so that the changes are included in testing
url="https://github.com/gauron99/func-templates#${{ env.HEADREF }}"
WORKDIR=$(mktemp -d)
cd $WORKDIR
for template_dir_abs in $(ls -d $language_path*/); do
echo "ls -la ${GITHUB_WORKSPACE}"
ls -la ${GITHUB_WORKSPACE}
template=$(basename "$template_dir_abs")
############################# FUNC CREATE #############################
echo "> FUNC CREATE"
echo "f create $language-$template -r=$url -l=$language -t=$template"
f create $language-$template -r "$url" -l "$language" -t "$template"
cd $language-$template
############################# FUNC BUILD #############################
echo "> FUNC BUILD"
if [ "$HOST_ENABLED" == "true" ]; then
echo "build with host"
FUNC_ENABLE_HOST_BUILDER=1 FUNC_BUILDER=host FUNC_CONTAINER=false FUNC_REGISTRY=docker.io/4141gauron3268 f build
else
echo "build with pack"
#TODO this might be moved up above the if statement for HOSTBUILDER?
if [ ${{matrix.language}} == "typescript" ];then
npm install
elif [ ${{matrix.language}} == "rust" ]; then
cargo build
fi
FUNC_REGISTRY=docker.io/4141gauron3268 f build
fi
############################## FUNC RUN ##############################
if [ "$HOST_ENABLED" == "true" ]; then
FUNC_ENABLE_HOST_BUILDER=1 FUNC_CONTAINER=false f run &
else
FUNC_CONTAINER=true f run &
fi
RUN_PID=$!
if ps -p $RUN_PID > /dev/null; then
echo "'func run' is running with PID $RUN_PID"
else
echo "Failed to start 'func run'. Exiting"
exit 1
fi
sleep 5
############################# FUNC INVOKE #############################
MAX_RETRIES=5
RETRY_COUNT=0
SUCCESS=false
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
echo "Attempt $RETRY_COUNT of $MAX_RETRIES"
echo "Invoking 'func invoke' with current PID $$"
if f invoke; then
echo "'func invoke' succeeded."
SUCCESS=true
break
else
echo "'func invoke' failed."
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 2
fi
done
kill $RUN_PID
if [ "$SUCCESS" = true ]; then
echo "all good"
else
echo "'func invoke' failed."
exit 1
fi
done