CI setup I use for almost all Go Applications


A simple CI configuration can save you hours of debugging and stress.
If you're a beginner in Go development, Believe me, setting up CI from day one will make your projects more reliable and help you catch issues before they reach production.
Benefits of Using CI in Your Application
CI helps you catch issues before code is merged into your main branch:
- Detect syntax errors
- Check code formatting issues
- Identify known vulnerabilities in dependencies
- Run all test cases automatically
- Prevent broken code from reaching production
- Enforce consistent code quality across the team
- Get fast feedback on every pull request
If you are new to CI, It's very easy to start with.
Create a folder in your repository at .github/workflows
You can run this command in your root to create it
In workflows folder, you can create multiple CI files, that will run based on the configuration you define in them.
If you are using Vs code, You can use Github Actions extension which will show errors for incorrect configs in your CI files.
For GitHub CI we use yml file
Config
Create a file named ci.yml in .github/workflows
First, we specify the name of our CI
Then, we specify when the CI should run.
Based on the configuration you can see the CI will run when code is pushed or a pull request is opened againts main.
After, We need to specify the jobs which will run to check the code.
The runs-on tells GitHub to allocate a fresh ubuntu virtual machine (Windows and MacOS also supported)
The VM is temporary and isolated, and already comes with some common tools like Git, Docker, etc.
In steps we define the actions or scripts that run sequentially
First, we have:
This step :
- Clones the repo to the Ubuntu machine
- Check out the commit/branches that triggered this action
- Setup credentials in case it needs to interact with the repo
Next:
actions/setup-go@v6 installs and configures Go on the runner.
with is used to pass input parameters to an action using:
we specify to install the Go version defined in our go.mod file, so we don't need to change the CI if we change Go version in our app
Then we have:
Instead of using thousands of linter (like we do in TS),we use one tool: golangci
Golangci Can:
- Check for Unused variables/imports
- Check for code formatting issues
- Detect code style violations
- results for faster run
etc...
Next, we have the dependency vulnerability checking job:
govaulcheck is a standalone tool created and maintained by the official Go Security Team.
Behind the scenes it uses to check if any dependencies has any vulnerabilities you are using.
Finally, we have:
As you can see, this job runs all the test cases written in our codebase, and then builds the project to verify that everything works correctly.
You can check the full CI file
Comments
@biisal to fir ye go wala janwan inke saath kya kr raha
@abh Modiji Javascript developer hai
Modi ji bhi Go lang karte?