Update package.json #37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Add Changes Based on Logic | |
on: | |
push: | |
branches: [ 'test-action-commit' ] | |
paths: | |
- 'micro-ui/web/micro-ui-internals/**' | |
jobs: | |
auto_increment_version: | |
runs-on: ubuntu-latest | |
env: | |
SOURCE_JSON_FILE: "micro-ui/web/micro-ui-internals/example/package.json" | |
DESTIN_JSON_FILE: "micro-ui/web/package.json" | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Install jq | |
run: sudo apt-get install jq | |
- name: Debug Information | |
run: | | |
git branch -a | |
git show | |
git show "$GITHUB_SHA" | |
- name: Check Dependency Change | |
id: check_change | |
run: | | |
dependency="@egovernments/digit-ui-module-workbench" | |
branch="test-action" # replace with the branch you want to compare against | |
# Fetch the branch from the remote repository | |
git fetch origin "$branch" | |
# Check if the dependency is present in the package.json file | |
if jq --arg dep "$dependency" '.devDependencies | has($dep)' "$SOURCE_JSON_FILE" >/dev/null; then | |
# Get the current version | |
current_version=$(jq -r --arg dep "$dependency" '.devDependencies[$dep]' "$SOURCE_JSON_FILE") | |
echo "CURRENT_VERSION=$current_version" | |
# Get the version from the specified branch for the specific file | |
branch_version=$(git show "origin/$branch":"$DESTIN_JSON_FILE" | jq -r --arg dep "$dependency" '.dependencies[$dep]') | |
echo "branch_version=$branch_version" | |
# Check if the versions are different | |
if [[ "$current_version" != "$branch_version" ]]; then | |
echo "Dependency version incremented" | |
echo "VERSION_CHANGED=true" >> $GITHUB_ENV | |
else | |
echo "Dependency version not incremented" | |
echo "VERSION_CHANGED=false" >> $GITHUB_ENV | |
fi | |
echo "CURRENT_VERSION=$current_version" >> $GITHUB_ENV | |
else | |
echo "Dependency not found in $SOURCE_JSON_FILE" | |
echo "VERSION_CHANGED=false" >> $GITHUB_ENV | |
fi | |
- name: Increment Version | |
run: | | |
current_version=${{ steps.check_change.outputs.CURRENT_VERSION }} | |
target_dependency="@egovernments/digit-ui-module-workbench" | |
# Increment the version manually | |
new_version=$(echo "$current_version" | awk -F. '{$NF++; OFS="."; print $0}') | |
# Update the package.json file with the new version | |
jq --arg dep "$target_dependency" --arg new_version "$new_version" '.dependencies[$dep] = $new_version' "$DESTIN_JSON_FILE" > "$DESTIN_JSON_FILE.tmp" && mv "$DESTIN_JSON_FILE.tmp" "$DESTIN_JSON_FILE" | |
# Commit the updated package.json | |
git config user.name "${{ github.actor }}" | |
git config user.email "${{ github.actor }}@users.noreply.github.com" | |
git add "$DESTIN_JSON_FILE" | |
git commit -m "Auto increment $target_dependency version to $new_version" | |
git push |