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

  1. Create a new workflow file: cd.yml in the .github/workflows directory.
  2. Define the workflow trigger: It should trigger on push events to the main branch.
  3. Create a Deploy job with the following steps:
    • Checkout code.
    • Set up the Go toolchain.
    • Build the application using the scripts/buildprod.sh script.

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.sh

Workflow Explanation

  1. Trigger Configuration:

    • The workflow triggers on push events to the main branch.
    • This ensures the deployment workflow runs whenever code is pushed directly to main or merged into main via a pull request.
  2. Deploy Job Setup:

    • Job Name: Deploy.
    • Runs on: ubuntu-latest (the latest Ubuntu runner available in GitHub Actions).
  3. Steps:

    • Check out code: Uses actions/checkout@v4 to clone the repository into the runner.
    • Set up Go: Uses actions/setup-go@v5 to install the specified Go version.
    • Build the app: Executes the custom script scripts/buildprod.sh to 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).

Running the CD Workflow

  1. Create or update a pull request targeting the main branch.
  2. Once merged into the main branch, the cd.yml workflow should automatically run.
  3. Alternatively, you can push directly to main to 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

  1. Check the scripts/buildprod.sh Path:

    • Ensure the scripts/buildprod.sh script exists and has executable permissions (chmod +x scripts/buildprod.sh).
  2. Ensure Correct Go Version:

    • If there are Go version issues, verify the Go version specified in the workflow matches your project’s Go version.
  3. Workflow Debugging:

    • If the workflow fails, click on the Actions tab in GitHub, navigate to the cd workflow run, and review the logs for any error messages.

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!