CICD pipeline for Go app with Github actions and docker
This guide demonstrates publishing an API module and establishing a CI/CD pipeline for a Go application using GitHub Actions and Docker.
In this guide, we will demonstrate how to publish your API module and establish a continuous integration pipeline using GitHub Actions. We begin by handling an API request, initializing our Git repository, and then creating and configuring a workflow for your CI/CD pipeline.
Assuming your API server is ready, let’s publish the module. When a request is received, the JSON payload is decoded into a product structure, the product is updated in the database, and a response is sent. The following Go code illustrates this logic:
After verifying that your API server is functioning correctly, the next step is to create a new repository on GitHub. In our example, the repository is named “my-inventory”. The screenshot below shows the GitHub interface when creating a new repository:
For this demonstration, set the repository to public and copy its URL. Then, open the terminal and execute the following commands to initialize the repository locally:
The successful initialization of the repository is confirmed by output similar to:
Copy
Ask AI
Initialized empty Git repository in /Users/priyanka/Desktop/kodeKloud/my-inventory/.git/
After adding the remote origin URL, check your repository status with:
Copy
Ask AI
git status
You’ll see your untracked Go and test files, such as app.go, app_test.go, constants.go, go.mod, go.sum, main.go, and model.go. Add and commit these files using:
Our objective is to automate linting and testing every time new changes are pushed to the repository or when pull requests are created. This section outlines the process of setting up a GitHub Actions workflow for continuous integration.
Create a directory named .github in your project root, and within it, create a workflows directory. Inside the workflows directory, create a file named ci.yaml:
Open ci.yaml using your preferred editor, and begin by defining the workflow name along with the trigger events.
Below is an example workflow configuration file that sets up MySQL, checks out your repository code, sets up Go, performs linting, and runs tests:
Copy
Ask AI
name: Continuous Integrationon: pushenv: DB_NAME: test DB_USER: root DB_PASSWORD: Priyanka#123jobs: run_code_checks: runs-on: ubuntu-latest steps: - name: Set up MySQL run: | sudo /etc/init.d/mysql start sudo mysql -e "ALTER USER '${{ env.DB_USER }}'@'localhost' IDENTIFIED BY '${{ env.DB_PASSWORD }}';" -uroot -proot sudo mysql -e "CREATE DATABASE ${{ env.DB_NAME }};" -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }} - name: Checkout Repo Code uses: actions/checkout@v2 - name: Set Up Go uses: actions/setup-go@v2 with: go-version: 1.19 - name: Run linting run: | go fmt ./... go vet ./... - name: Run tests run: go test
This workflow is triggered on every push. It initiates by starting MySQL, modifying the root user’s password, and creating a “test” database. It then checks out the repository code, sets up Go (version 1.19), performs linting using go fmt and go vet, and finally runs the tests with go test.After adding the workflow file, stage and commit the changes using:
Next, navigate to your repository’s “Actions” tab on GitHub to see the workflow running. If the workflow is not detected, ensure that the ci.yaml file is placed exactly within the .github/workflows directory.The commit output may appear like this:
If you experience issues with the MySQL setup step in your workflow, it may be due to improper quote usage in the command that alters the user password. Ensure you are using matching quotes.
The corrected commands should look like this:
Copy
Ask AI
sudo /etc/init.d/mysql startsudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Priyanka#123';" -uroot -prootsudo mysql -e "CREATE DATABASE test;" -uroot -pPriyanka#123
After making these adjustments and pushing the changes, the workflow steps should execute successfully. A successful run is confirmed by logs indicating:
MySQL was properly started and configured.
The repository code was checked out.
Go was correctly set up.
Linting and tests were executed as expected.
For example, a successful test run might display:
Copy
Ask AI
Run go test2023/01/06 17:06:43 clearTable2023/01/06 17:06:43 clearTable2023/01/06 17:06:43 clearTable2023/01/06 17:06:43 clearTablePASSok example.com/my-inventory 0.046s
In this guide, we established a CI/CD pipeline to automate the linting and testing of a Go application using GitHub Actions. The key steps in this workflow include:
Starting and configuring MySQL.
Checking out the repository code.
Setting up Go on an Ubuntu container.
Running linting via go fmt and go vet.
Executing tests with go test.
Automating these processes helps maintain code quality and ensures consistent functionality throughout your development cycles. Congratulations on setting up your continuous integration pipeline for your Go application!