Home

Automate Deployment Checks: Compare GitHub Commits with Server

14 views

Creating a deployment confirmation script involves setting up a system that verifies whether the version of the code on your server matches the latest code pushed to GitHub. Here’s how you can implement this:

Steps to Create a Deployment Confirmation Script:

  1. Determine the Latest Commit on GitHub:

    • Fetch the latest commit hash from your GitHub repository using GitHub’s API or git commands.
    • If you prefer using the API, you can send a GET request to https://api.github.com/repos/:owner/:repo/commits/main (replace main with your branch name if it’s different).
    • You can also use git ls-remote to obtain the latest commit hash without cloning the repository.
  2. Retrieve the Deployed Commit Hash:

    • Record the commit hash that is deployed on your server. This could be stored in a file or as part of your deployment logs.
    • Another approach is to have a convention where the deployed commit hash is noted in a specific file (like VERSION) each time a deployment occurs. This file is then updated as part of the deployment script with the new commit hash.
  3. Compare the Hashes:

    • After a deployment, write a script that compares the commit hash on your server against the latest hash in the GitHub repository.
    • If the hashes match, the deployment is likely successful, assuming no errors were encountered in the deployment process and the application is running correctly.
  4. Integrate with Deployment Workflow:

    • Make this comparison script a part of your continuous deployment pipeline so that every deployment automatically triggers a check.
    • Ensure the comparison script runs after all deployment steps are completed, including any testing and configuration updates.
  5. Handling Mismatches:

    • If the hashes do not match, signal a failure in the deployment process. This could involve logging an error, sending a notification, or even rolling back the deployment, depending on your system capabilities.
    • Ensure that your logging system captures detailed information about the mismatch incident to facilitate troubleshooting.
  6. Feedback and Alerts:

    • Set up feedback mechanisms to alert your team regarding the status of the deployment, especially in the event of a mismatch.
    • Use tools such as email notifications, chat alerts, or a dashboard that displays real-time deployment statuses.

Example Workflow with Scripts:

Here's a simplified example of how you might script this in a shell script:

#!/bin/bash

# Assuming you have a way to retrieve the latest commit from GitHub
LATEST_GITHUB_COMMIT=$(curl -s https://api.github.com/repos/:owner/:repo/commits/main | jq -r .sha)

# Read the deployed commit hash from a file
DEPLOYED_COMMIT=$(cat /path/to/your/deployment/VERSION)

if [ "$LATEST_GITHUB_COMMIT" == "$DEPLOYED_COMMIT" ]; then
  echo "Deployment successful: Server is running the latest code."
else
  echo "Deployment failed: Server code is outdated."
  # You can add additional error handling here such as alerts or rollbacks
fi

In this workflow, you have automation for checking that the code on your server is up-to-date with the repository. The process ensures transparency and quick detection of any discrepancies that might arise during deployment, allowing for immediate corrective action.