# Add Go Test Coverage to CI/CD pipeline

Before proceeding further:

  • Go to Settings page of the repository dashboard in DeepSource and copy the DSN from the Reporting section.

    WARNING

    Do not add the DEEPSOURCE_DSN variable as part of any publicly visible configuration file. It contains sensitive information.

  • Make sure that "Test Coverage" Analyzer is added to .deepsource.toml.

# With Travis CI

  1. On Travis CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

  2. Add this to .travis.yml:

after_success:
  # Generate test coverage report
  - go test -coverprofile=cover.out

  # Install deepsource CLI
  - curl https://deepsource.io/cli | sh

  # From the root directory, run the report coverage command
  - ./bin/deepsource report --analyzer test-coverage --key go --value-file ./cover.out

# With Circle CI

  1. On Circle CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

  2. Add the following step in .circleci/config.yml:

- run:
  name: Report results to DeepSource
  command: |
    # Generate test coverage report
    go test -coverprofile=cover.out

    # Install deepsource CLI
    curl https://deepsource.io/cli | sh

 # From the root directory, run the report coverage command
    ./bin/deepsource report --analyzer test-coverage --key  go --value-file ./cover.out

# With GitHub Actions

  1. On GitHub, navigate to the main page of the repository. Under your repository name, click "Settings". In the left sidebar, click Secrets.

    • Type DEEPSOURCE_DSN in the "Name" input box.
    • Add the value copied above.
  2. When you checkout code, ensure that you use pull request HEAD commit instead of merge commit:

  - uses: actions/checkout@v2
    with:
      ref: ${{ github.event.pull_request.head.sha }}
  1. Add the following step in .github/workflows/main.yml:
  - name: Report results to DeepSource
    run: |
    # Generate coverage report
      go test -coverprofile=cover.out

    # Install deepsource CLI
      curl https://deepsource.io/cli | sh

  # From the root directory, run the report coverage command
      ./bin/deepsource report --analyzer test-coverage --key go --value-file ./cover.out

    env:
      DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}

# WIth GitLab CI

  1. Navigate to the project page of the repository on GitLab. Under project settings, in the sidebar, click on "CI/CD". Expand the variable section, and add the following:

    • Type: "Variable"`
    • Key: DEEPSOURCE_DSN
    • Value: The DSN value copied above
    • State: Protected (Yes)
    • Masked: No
    • Scope: All Environments
  2. Add the following job in .gitlab-ci.yml:

deepsource:
  script:
    # Run tests and generate coverage report
    - go test -coverprofile=cover.out

    # Install deepsource CLI
    - curl https://deepsource.io/cli | sh

  # From the root directory, run the report coverage command
    - ./bin/deepsource report --analyzer test-coverage --key go --value-file ./cover.out
Last Updated: 7/8/2020, 10:03:47 AM