CI/CD Workflow
Overview
This section explains the CI/CD workflow using Docker and the associated files from the project.
Setting Up Continuous Integration (CI)
Docker Configuration: Ensure the
docker-compose.yml
defines your services correctly. It includes:version: "2" services: server: build: context: . dockerfile: server.Dockerfile ... signer: build: context: . dockerfile: signer.Dockerfile ... mysql: ...
Building the Docker Image: Use a Dockerfile for defining how the images should be built. The provided Dockerfile is set up to use multi-stage builds. An example step to build the binary can be executed within a CI pipeline:
FROM build-${BASE_VARIANT} AS build ... RUN TARGET=/out ./scripts/build/binary
This step compiles the Go source code and creates the final Docker image, ensuring the binary is available for the CI/CD pipeline.
Running Tests for Continuous Integration
Unit Tests: Utilize the provided Makefile to define a function for running unit tests. The following command can be employed within a CI pipeline:
make test-unit
The
test-unit
function can be implemented to run the tests inside your Docker environment, confirming the integrity of your codebase before deploying.
Continuous Deployment
Deploying with Docker Compose: Once the image is built and tests pass successfully, deploy the services using Docker Compose. For example, the following command will ensure all conditions are met and services are up:
docker-compose up -d
GitHub Actions or CI/CD Pipeline: Integrate your CI/CD workflow with a CI service provider such as GitHub Actions. A
.github/workflows/ci.yml
file can be created similar to:name: CI on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Docker Build run: docker-compose build - name: Run Tests run: make test-unit - name: Deploy run: docker-compose up -d
Next Steps if CI/CD is Not Yet Set Up
If a CI/CD pipeline has not been established in the project:
Define the Docker Configuration: Ensure your
docker-compose.yml
and Dockerfile are set up correctly.Create a Makefile for Unit Testing: Implement unit tests in your Go application and define a
test-unit
function in your Makefile.Integrate a CI/CD Tool: Choose a CI service (e.g., GitHub Actions, GitLab CI, CircleCI) and create the pipeline configuration for your workflow.
Document the Process: Ensure to document the newly created workflow for all developers and contributors in the project.
Conclusion
The outlined steps provide a structured approach to implementing a CI/CD workflow for a Dockerized Go application. Following these principles ensures efficiency and reliability in your development and deployment processes.