Automating ZDK CLI commands using shell scripting

Automating ZDK CLI commands using shell scripting


Hello everyone,
Welcome back to Kaizen.  ZDK CLI's command-line nature allows you to easily incorporate it within a shell script, to achieve any custom action that you want to execute.  It allows you to execute multiple ZDK CLI commands seamlessly, enhancing productivity.

In this week's Kaizen post, we will discuss a shell script designed to automate the process of pushing locally modified metadata to our CRM environment. Before executing the push operation, ensure that the local metadata is up-to-date and conflicts (same file has been modified in both the local ZDK environment and the remote UI environment) are resolved. To achieve this, we will perform the following steps for files in conflict where manual intervention is required:
  • Retrieve the latest metadata from Zoho CRM to our local environment using zdk org:pull command
  • Address conflicts that may arise during the pull operation using zdk org:pull:resolve command 
  • Update our local directory with the newly pulled file using zdk org:pull:update command
  • Push changes from your local environment to the CRM using zdk org:push command
  • Update local directory and check status of push command using zdk org:push:result command
This script automates the sequential execution of these commands.

Prerequisites
  • ZDK CLI Version 1.0.2-beta
To update ZDK CLI, execute the following command in your terminal


  npm install -g @zohocrm/zdk-cli


This will initiate the installation process for the latest version.

Crafting the Shell Script

 
1.Create a new shell script file zdk_automation.sh in your ZDK project's root directory.
#!/bin/bash
touch stdout.txt
#This script is used to automate ZDK CLI conflict resolve flow
conflict_flag=false
# Clear stdout.txt at the start
> stdout.txt
#Step 1: Initial Push Command
echo "====Start of the script ====" >> stdout.txt
echo "**Executing the initial zdk org:push -j" >> stdout.txt
json_content=$(zdk org:push -j)
echo "Output  $json_content" >> stdout.txt
next_command=$(echo "$json_content" | jq -r '.next_command_sequence // empty')
echo "next_command after initial push  is $next_command">> stdout.txt
# Check if the JSON is valid
if echo "$json_content" | jq empty 2>/dev/null; then
#Step 2: Conflict Detection
    message=$(echo "$json_content" | jq -r '.message')
    echo "message: $message" >>stdout.txt
    if [[ "$message" == *"Merge Conflicts!"* ]]; then
        #Step 3: Resolving Conflicts
        conflict_flag=true
        # Pull changes to the local repository and store the output in a variable
        echo "**Executing zdk org:pull -j" >> stdout.txt
        json_content=$(zdk org:pull -j)
        echo "Output  $json_content" >> stdout.txt
        if echo "$json_content" | jq empty 2>/dev/null; then
            message=$(echo "$json_content" | jq -r '.result')
            echo "message $message" >>stdout.txt
            IFS=$'\n' read -r -d '' -a filenames <<< "$message"
            # Iterate over each filename and execute the resolve command
            for filename in "${filenames[@]}"; do
                echo "**Executing zdk org:pull:resolve for $filename" >> stdout.txt
                # Executing zdk org:pull:resolve 
                json_content=$(zdk org:pull:resolve -f "$filename" -j)
                echo "Output  $json_content" >> stdout.txt
            done
            echo "***Executing zdk org:pull:update" >> stdout.txt
            json_content=$(zdk org:pull:update -j)
            echo "Output  $json_content" >> stdout.txt
        fi
    fi
fi
#Step 4: Second Push Attempt after conflict
if [ "$conflict_flag" = true ]; then
# Push changes to the org and store the output in a variable again
echo "***Executing: zdk org:push -j after conflict resolution" >> stdout.txt
json_content=$(zdk org:push -j)
echo "Output  $json_content" >> stdout.txt
# Extract the line containing "next_command_sequence" from the push output
next_command=$(echo "$json_content" | jq -r '.next_command_sequence')
echo "Next command $next_command " >> stdout.txt
fi
#Step 5: Push result command
# Execute the next command if it exists
counter=0  # Initialize a counter
if [ -n "$next_command" ]; then
    while true; do
    # Execute the command stored in next_command and capture the output
    echo "***Executing: $next_command -j"  >> stdout.txt
    output=$(eval "$next_command -j")
    echo "Output $output" >>stdout.txt
    # Check if the output contains "Process in progress"
    if echo "$output" | grep -q "Process in progress"; then
        echo "Push process in progress. Waiting for 5 seconds..."  >> stdout.txt
        sleep 5  # Wait for 5 seconds
    else
        # If not found, break the loop and handle the output
        echo "Command executed successfully or completed."  >> stdout.txt
        break
    fi
     # Increment the counter
    counter=$((counter + 1))

    # Break the loop after 10 iterations
    if [ "$counter" -ge 10 ]; then
        echo "Maximum number of attempts(10) reached. Exiting the loop." >> stdout.txt
        echo "Try executing $next_command after some time" >> stdout.txt
        break
    fi
done
fi

Explanation of the script

Step 1: Initial Push Command
  • The script starts by executing the zdk org:push -j command. 
  • The commands executed using -j option ensures that the output is in a machine-readable JSON format, which facilitates easier parsing and processing.
  • The conflict_flag is set to false.
Step 2: Conflict Detection
  • The script checks the output for any merge conflicts. 
  • If conflicts are detected, it executes the zdk org:pull -j  command to retrieve the latest changes from the organization. The conflict_flag is set to true.
  • If conflicts are not present, the script proceeds to Step 5: Executing the push:result command.
Step 3: Resolving Conflicts
  • If conflicts are present, the script extracts a list of filenames associated with these conflicts. 
  • It iterates through this list and executes the zdk org:pull:resolve -j command for each filename, allowing for manual resolution of conflicts as needed. 
  • This opens a three-way editor in the browser where the conflicts can be resolved.
Step 4: Second Push Attempt after conflict
  • Once all conflicts are resolved, the script attempts to push any changes back to Zoho CRM by executing the zdk org:push -j command. 
Step 5: Executing push:result command 
  • This command should be followed by zdk org:push:result --jobid {id} command (indicated by a next_command_sequence) which displays the result of the push process with the mentioned job id and updates the local directory. 
  • During this process, if the result is shown as "Process in progress", a subsequent push:result command is required. In this case, the script waits for five seconds before trying again to ensure that operations complete successfully. 

2.Make the script executable
chmod +x zdk_automation.sh

3.Run your script
./zdk_automation.sh & tail -f stdout.txt

Logging ActionsAll actions taken by the script are logged in stdout.txt. This serves as a record for later reference and troubleshooting.







Using ZDK CLI with shell script ensures that your ZDK CLI operations are automated daily, minimizing manual intervention while effectively managing conflicts. 
Notes
For the current beta release, ZDK CLI is exclusively available for Sandbox environment and is not operational in Production environments. 

We hope you found this post useful. We will meet you next week with another interesting topic!
If you have any questions, let us know in the comment section.

Happy Exploring!


    Access your files securely from anywhere

        Zoho Developer Community







                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Tips

                                                                            Tips

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now