Skip to content

Instantly share code, notes, and snippets.

@mbruno-kr
Last active April 7, 2023 18:27
Show Gist options
  • Save mbruno-kr/3540d8c19ca933bcd9473f054c987780 to your computer and use it in GitHub Desktop.
Save mbruno-kr/3540d8c19ca933bcd9473f054c987780 to your computer and use it in GitHub Desktop.

Github Action/Workflow Updater

Requirements

  • yq
  • gh
  • gh logged into github

Usage

First arg is always .github folder

image

#!/usr/bin/env bash
GITHUB_DIR=$1
if ! command -v yq &> /dev/null; then
echo "yq could not be found"
echo "If on mac, try brew install yq"
exit 1
fi
if ! command -v gh &> /dev/null; then
echo "gh could not be found"
echo "If on mac, try brew install gh"
echo "After installation run, gh auth login"
exit 1
fi
function getLatest() {
local owner=$1
local repo=$2
gh release -R "$owner/$repo" list --exclude-drafts --exclude-pre-releases | grep Latest | awk -F '\t' '{if (match($3, "^v*\.*\.*")) print $3}'
}
echo ""
echo ""
function processWorkflow() {
local FILE=$1
echo "$FILE"
local CONTENTS
CONTENTS="$(cat "$FILE")"
local REUSABLE_WORKFLOWS
REUSABLE_WORKFLOWS=$(yq '.jobs[] .uses' < "$FILE")
local STEPS
STEPS=$(yq '.jobs[] .steps[] .uses' < "$FILE")
captures=( $REUSABLE_WORKFLOWS $STEPS )
for job in "${captures[@]}"; do
if [ "$job" != "null" ] && [[ ! "$job" =~ ^\./\.github ]]; then
OWNER=$(echo "$job" | cut -d '/' -f 1)
REPO=$(echo "$job" | cut -d '/' -f 2 | cut -d '@' -f 1)
JOB_INFO="$(echo "$job" | cut -d '@' -f 1)"
CURRENT_VERSION="$(echo "$job" | cut -d '@' -f 2)"
LATEST_VERSION=$(getLatest "$OWNER" "$REPO")
CONTENTS=${CONTENTS//"$JOB_INFO@$CURRENT_VERSION"/"$JOB_INFO@$LATEST_VERSION"}
fi
done
echo "$CONTENTS" > "$FILE"
}
function processAction() {
local FILE=$1
echo "$FILE"
local CONTENTS
CONTENTS="$(cat "$FILE")"
local STEPS
STEPS=$(yq '.runs.steps[] .uses' < "$FILE")
captures=( $STEPS )
for job in "${captures[@]}"; do
if [ "$job" != "null" ] && [[ ! "$job" =~ ^\./\.github ]]; then
OWNER=$(echo "$job" | cut -d '/' -f 1)
REPO=$(echo "$job" | cut -d '/' -f 2 | cut -d '@' -f 1)
JOB_INFO="$(echo "$job" | cut -d '@' -f 1)"
CURRENT_VERSION="$(echo "$job" | cut -d '@' -f 2)"
LATEST_VERSION=$(getLatest "$OWNER" "$REPO")
CONTENTS=${CONTENTS//"$JOB_INFO@$CURRENT_VERSION**"/"$JOB_INFO@$LATEST_VERSION"}
fi
done
echo "$CONTENTS" > "$FILE"
}
IFS=$'\n'
YML_WORKFLOWS=$(find -f $GITHUB_DIR/workflows/*.yml)
YAML_WORKFLOWS=$(find -f $GITHUB_DIR/workflows/*.yaml || "")
YML_ACTIONS=$(find -f $GITHUB_DIR/actions/**/*.yml)
#YAML_ACTIONS=$(find -f ./actions/**/*.yaml)
yaml_files=( $YML_WORKFLOWS $YAML_WORKFLOWS $YML_ACTIONS )
unset IFS
for i in "${yaml_files[@]}"; do
FILE=$(realpath "$i")
if [[ "$FILE" =~ "workflows" ]]; then
processWorkflow "$FILE"
elif [[ "$FILE" =~ "actions" ]]; then
processAction "$FILE"
else
echo "Unsupported"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment