Continuous Deployment (CD)
Continuous Deployment (CD) involves automatically deploying code to a production environment after successful testing and building. The objective is to automate the release process, so new changes are immediately deployed to production once merged or pushed to the main branch.
Steps for Setting up the CD Workflow
- Create a new workflow file:
cd.ymlin the.github/workflowsdirectory. - Define the workflow trigger: It should trigger on
pushevents to themainbranch. - Create a
Deployjob with the following steps:- Checkout code.
- Set up the Go toolchain.
- Build the application using the
scripts/buildprod.shscript.
Sample cd.yml Workflow File
# .github/workflows/cd.yml
name: Continuous Deployment
on:
push:
branches: [main]
jobs:
Deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23.0"
- name: Build the app
run: scripts/buildprod.shWorkflow Explanation
-
Trigger Configuration:
- The workflow triggers on
pushevents to themainbranch. - This ensures the deployment workflow runs whenever code is pushed directly to
mainor merged intomainvia a pull request.
- The workflow triggers on
-
Deploy Job Setup:
- Job Name:
Deploy. - Runs on:
ubuntu-latest(the latest Ubuntu runner available in GitHub Actions).
- Job Name:
-
Steps:
- Check out code: Uses
actions/checkout@v4to clone the repository into the runner. - Set up Go: Uses
actions/setup-go@v5to install the specified Go version. - Build the app: Executes the custom script
scripts/buildprod.shto build the production version of the application. - Note: Despite the job name, this sample only builds. Nothing is pushed or deployed yet. Real CD adds a step that pushes the artifact/image to a registry and a deploy step (using secrets or OIDC credentials).
- Check out code: Uses
Running the CD Workflow
- Create or update a pull request targeting the
mainbranch. - Once merged into the
mainbranch, thecd.ymlworkflow should automatically run. - Alternatively, you can push directly to
mainto trigger the workflow.
Key Differences Between ci.yml and cd.yml
ci.yml: Executes on pull requests and ensures the code changes are tested and formatted correctly before merging.cd.yml: Executes on push to main to build the code (and, once push + deploy steps are added, ship it to production).
Troubleshooting Tips
-
Check the
scripts/buildprod.shPath:- Ensure the
scripts/buildprod.shscript exists and has executable permissions (chmod +x scripts/buildprod.sh).
- Ensure the
-
Ensure Correct Go Version:
- If there are Go version issues, verify the Go version specified in the workflow matches your project’s Go version.
-
Workflow Debugging:
- If the workflow fails, click on the
Actionstab in GitHub, navigate to thecdworkflow run, and review the logs for any error messages.
- If the workflow fails, click on the
Notes
- Always review the deployment process and environment configurations to avoid unexpected downtime.
- Consider adding deployment to a staging environment before pushing directly to production.
This setup should help automate the deployment of your Notely project efficiently. Let me know if you’d like to expand the workflow to include additional deployment steps, such as Docker builds or Kubernetes deployments!