Deploy scripts are automated scripts that run before or after you deploy code changes to your website or application using Git. They help automate tasks such as:
• Testing your code
• Setting up configurations
• Cleaning up files after deployment
Using deploy scripts ensures that your deployments are smooth, consistent, and less prone to errors.
Understanding Deploy Scripts
Before diving into the specifics, it’s important to understand how deploy scripts can enhance your deployment process. These scripts help automate essential tasks, ensuring a smooth and error-free deployment. Depending on your needs, you can implement either or both types of deploy scripts: Pre-deploy and Post-deploy.
Pre-deploy Script
The script is executed before you deploy
• Validating and preparing your code.
• Running tests.
• Checking for errors.
• Ensuring you’re on the correct branch.
Post-deploy Script
The script is executed after you deploy
• Cleaning up temporary files.
• Initializing databases.
• Sending notifications.
• Logging deployment details.
You can use either or both types of a Deploy script based on your needs.
Choose Deployment Type
There are two primary ways to set up deploy scripts. One approach is to use the scripts manually in the environment paths. This method gives you control over the deployment process within your environment. The other option is to use the repository’s Continuous Integration (CI) pipeline, where scripts are stored in the repository’s ci directory
Default Bolt Environment Paths
Scripts should be auto-generated when Git Repository is added to Environment. Create if missing
- Pre-deploy Script is placed at
~/hooks/pre-deploy
- Post-deploy Script is placed at
~/hooks/post-deploy
Repository Paths (Continuous Integration).
- Pre-deploy Script can be placed at
REPOSITORY/ci/pre-deploy
- If a script exists here, the default Pre-deploy Script(
~/hooks/pre-deploy
) will not be used.
- If a script exists here, the default Pre-deploy Script(
- Post-deploy Script can be placed at
REPOSITORY/ci/post-deploy
- If a script exists here, the default Post-deploy Script(
~/hooks/post-deploy
) will not be used.
- If a script exists here, the default Post-deploy Script(
If using the repository paths, the Pre-deploy Script works on the version before the deploy. But it will be updated once deploy is finished.
Using the Deploy Scripts
If the pre-deploy script is exiting prematurely, it will cancel the deploy. This is to ensure that a deploy never takes place if a problem is encountered. Check example pre-deploy script below for an example of this:
Example pre-deploy script
#!/bin/bash
# Check PHP syntax for all code
find . -name "*.php" -exec php -l {} \;
if [ "$?" -ne "0" ]; then
echo "PHP syntax check failed. Please fix before deploying."
# This will cancel the deploy
exit 1
fi
echo "All checks passed"
Example post-deploy script
#!/bin/bash
# Post-deployment script for Git
# Run some tasks after successful push to staging
if [ -f package.json ]; then
npm ci
npm run build --if-present
fi
# Send a notification (replace with actual command to send notification)
echo "Post-deployment tasks completed successfully."
Logging
By using our API you can use the “Deploy Git Repository” endpoint. Documented here. To manually trigger deploy and receive the status of the deploy state.
The output generated by pre- and post-deploy scripts is logged in the file located at ~/logs/git/deploy.log.
Each time a script is executed during a deployment, the log is appended with the corresponding output.
- Successful Execution: When the deploy script runs without errors, the log will capture the standard output, which includes all messages, notifications, or actions that the script produces during its execution. This could include tasks like successful tests, completed build processes, or confirmation of environment readiness.
- Failed Execution: If the deploy script encounters any issues or fails to execute correctly, the log will capture the standard error instead. This will contain details about the failure, such as error messages, allowing you to identify what went wrong easily.
This log file is a useful resource for troubleshooting and auditing the deployment process, as it provides a clear, historical record of each deploy and its outcome. Monitoring this log regularly helps ensure that any potential issues with the deployment process are identified and addressed promptly.