Using Private Go Modules with golangci-lint in GitHub Actions
Tags: go, github, ci, lint
Published: 2024-01-05 15:54:07

---
title: Using Private Go Modules with golangci-lint in GitHub Actions
published: true
description: Learn how to use golangci-lint and GitHub Actions to automate common coding mistakes checking and ensure consistent and maintainable Go code. We'll guide you through the setup process for projects with private modules.
tags: go, github, ci, lint
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-01-04 13:01 +0000
---

## Intro
We're at a point in the software development evolution when everybody recognizes the importance of *Continuous Integration (CI)* in the *Software Development Life Cycle* - or at least I hope. To be honest, I can't remember a project I've worked on that didn't have at least a basic CI pipeline in place.

[**golangci-lint**](https://golangci-lint.run/) is an amazing open-source tool for CI in Go projects. Basically, it's an aggregator and a Go linters runner that makes life easier for developers. It includes all the well-known liners by default but also provides an easy way to integrate new ones.  

============


## Steps
In the following, we will go through the steps required for running **golangci-lint** via **GitHub Actions** for projects with private modules.

#### 1. Setup personal access tokens in GitHub 

That can be done in both, personal accounts and organizations - depending on where the code is.

You have to go to **Settings -> Developer Settings** and click on **Generate new token** button
![Developer settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6njj4orcesm5its6f7ok.png)

In the page opened you can choose between a personal or an organisation account. Here depends on your use case. Is it the project inside your account or inside of an organization account?

Select the repository/repositories or even all the repositories (again, depending on your use case)
![Repositories](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywfcpm0srlms5wm0m5yg.png)

and grant **Read-only** access for **Actions**. 
![Grant access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7k8hc9flaz4de9n3q1s.png)

The access token will be shown. Copy it and keep it somewhere for the next step.

Now, let's go back to the repository where the GH Action should run. Go to **Settings -> Security -> Secrets and variables**

![Secrets and variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4vwbz2kvk2xa1pckre2.png)

click on **New repository secret**, fill in the secret name, and paste the access token from the previous step

![Repository secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsdf8gg8t7o71hmh4sza.png)

#### 2. Setup GitHub Actions workflow

##### 2.1. Add golangci-lint.yml to your workflows

If it is not already exists.

```yaml
# source: https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#how-to-use

name: golangci-lint
on:
  push:
    branches:
      - master
      - main
  pull_request:

permissions:
  contents: read
  # Optional: allow read access to pull request. Use with `only-new-issues` option.
  # pull-requests: read

jobs:
  golangci:
    name: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: '1.21'
          cache: false
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          # Require: The version of golangci-lint to use.
          # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
          # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
          version: v1.54

          # Optional: working directory, useful for monorepos
          # working-directory: somedir

          # Optional: golangci-lint command line arguments.
          #
          # Note: By default, the `.golangci.yml` file should be at the root of the repository.
          # The location of the configuration file can be changed by using `--config=`
          # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 

          # Optional: show only new issues if it's a pull request. The default value is `false`.
          # only-new-issues: true

          # Optional: if set to true, then all caching functionality will be completely disabled,
          #           takes precedence over all other caching options.
          # skip-cache: true

          # Optional: if set to true, then the action won't cache or restore ~/go/pkg.
          # skip-pkg-cache: true

          # Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
          # skip-build-cache: true

          # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
          # install-mode: "goinstall"
``` 

##### 2.2. Add **GOPRIVATE** and **GH_ACCESS_TOKEN** environment variables. 

The first one is to let the workflow know the private dependencies and the second one is to specify the access token created above.

```yaml
...
jobs:
  golangci:
    name: lint
    runs-on: ubuntu-latest
    env:
+     GOPRIVATE: github.com/claudiunicolaa
+     GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: "1.21"
          cache: false
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
...
```

##### 2.3. Automatically include a GitHub access token in the URL when interacting with GitHub repositories

```yaml
jobs:
  golangci:
    name: lint
    runs-on: ubuntu-latest
    env:
      GOPRIVATE: github.com/claudiunicolaa
      GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: "1.21"
          cache: false
+     - run: git config --global url.https://$GH_ACCESS_TOKEN@github.com/.insteadOf https://github.com/
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
```

============

## Wrap-up


In this article, we learned how to use golangci-lint in GitHub Actions for projects with private modules. We went through the steps required to set up personal access tokens in GitHub and then used them to create repository secrets. We also added the required environment variables and created a workflow to run golangci-lint via GitHub Actions. By following these steps, you can now use golangci-lint to automatically check your Go projects for common coding mistakes and ensure that your code is consistent and maintainable.

Thank you for reading this article. I hope that the information provided here will help you use golangci-lint in GitHub Actions for your Go projects with private modules. 

All the examples can be found in [this repository](https://github.com/claudiunicolaa/project-a-go/tree/main) 

Good luck with your software development journey!