Skip to content

Commit

Permalink
Optimize GitHub action workflows for Go and Node
Browse files Browse the repository at this point in the history
Before all jobs were summarized in the `ci` workflow [1] but not
separated by their scope, i.e. Go and Node specific tasks. The workflow
was also not optimized to only run when specific files have been changed
which resulted in false-positive executions and wasted limited free tier
and developer time.
Therefore the `ci` workflow has been optimized by splitting it into new
`ci-go` and `ci-node` workflows.

>> CI Go

The new `ci-go` workflow...

- only runs when any `*.go` file has been modified. See the extensive
  GitHub action documentations about `on.<push|pull_request>.paths` [4]
  and the filter pattern cheat sheet [5] for more details.
- only runs for `ubuntu-latest` instead of a matrix with `macos-latest`
  and `windows-latest` which should be added for projects with platform
  specific code.

>> CI Node

The new `ci-node` workflow...

- only runs when any `*.js`, `*.json`, `*.md`, `*.yaml` and `*.yml` file
  has been modified. This matches the lint-staged [2], Prettier and
  remark configurations. See the extensive GitHub action documentations
  about `on.<push|pull_request>.paths` [4] and the filter pattern cheat
  sheet [5] for more details.
- only runs for `ubuntu-latest` instead of a matrix with `macos-latest`
  and `windows-latest` which should be added for projects with platform
  specific code.
- uses cache `npm` dependencies which is possible as of
  `actions/[email protected]` [3].

>> Silent linting errors for CI/CD environments

When running the configured linting tasks [6] the Prettier CLI [7]
prints matches to the standard output with a visual preview of the file
content and a marker at the specific element. When files that store
secret data, e.g. when encrypted with `git-crypt` [8], are decrypted in
the GitHub Actions [9] this could leak this data when Prettier finds
errors in these files.
To prevent these case new CI specific linting tasks have been added with
a `silent` `loglevel` [10]. This however comes with the drawback that
possible linting errors must be analyzed locally, but the code quality
is still ensured by blocking subsequent workflows.

[1]: https://github.com/svengreb/tmpl-go/blob/c20ba3bd/.github/workflows/ci.yml
[2]: https://github.com/svengreb/tmpl-go/blob/c20ba3bd/lint-staged.config.js#L12
[3]: https://github.com/actions/setup-node/releases/tag/v2.2.0
[4]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestpaths
[5]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet
[6]: https://github.com/svengreb/tmpl-go/blob/c20ba3bd/package.json#L28
[7]: https://prettier.io/docs/en/cli.html
[8]: svengreb/antarctica#170
[9]: https://github.com/svengreb/antarctica/blob/0e6abe44/.github/workflows/ci-go.yaml#L29-L32
[10]: https://prettier.io/docs/en/cli.html#--loglevel

GH-68
  • Loading branch information
svengreb committed Nov 19, 2021
1 parent c20ba3b commit d8fa563
Show file tree
Hide file tree
Showing 5 changed files with 8,257 additions and 35 deletions.
42 changes: 12 additions & 30 deletions .github/workflows/ci.yml → .github/workflows/ci-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
# GitHub Action Workflow for continuous integration jobs.
# See https://docs.github.com/en/actions and https://github.com/features/actions for more details.

name: ci
name: ci-go
on:
push:
paths:
- "**.go"
branches:
- main
tags:
- v*
pull_request:
paths:
- "**.go"

jobs:
lint-node:
lint:
runs-on: ubuntu-latest
steps:
- name: Print metadata and context information
Expand All @@ -24,25 +28,7 @@ jobs:
echo "Workflow Actor: $GITHUB_ACTOR"
- name: Checkout repository
uses: actions/checkout@v2
- name: "Setup Node.js version 15"
uses: actions/[email protected]
with:
node-version: "15"
- name: Install Node modules
run: yarn --frozen-lockfile
- name: Run linters
run: yarn lint
lint-go:
runs-on: ubuntu-latest
steps:
- name: Print metadata and context information
run: |
echo "Git SHA: $GITHUB_SHA"
echo "Git Ref: $GITHUB_REF"
echo "Workflow Actor: $GITHUB_ACTOR"
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
- name: Install Go 1.17
uses: actions/setup-go@v2
with:
go-version: "1.17.x"
Expand All @@ -53,13 +39,9 @@ jobs:
with:
version: v1.43
test:
strategy:
matrix:
go-version: ["1.17.x"]
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
needs:
- lint-go
- lint
steps:
- name: Print metadata and context information
run: |
Expand All @@ -68,9 +50,9 @@ jobs:
echo "Workflow Actor: $GITHUB_ACTOR"
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
- name: Install Go 1.17
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: "Run tests with coverage and race detector"
go-version: "1.17.x"
- name: Run tests with coverage and race detector
run: go test -cover -race -v ./...
47 changes: 47 additions & 0 deletions .github/workflows/ci-node.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) 2020-present Sven Greb <[email protected]>
# This source code is licensed under the MIT license found in the LICENSE file.

# GitHub Action Workflow for continuous integration jobs.
# See https://docs.github.com/en/actions and https://github.com/features/actions for more details.

name: ci-node
on:
push:
paths:
- "**.js"
- "**.json"
- "**.md"
- "**.yaml"
- "**.yml"
branches:
- main
tags:
- v*
pull_request:
paths:
- "**.js"
- "**.json"
- "**.md"
- "**.yaml"
- "**.yml"

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Print metadata and context information
run: |
echo "Git SHA: $GITHUB_SHA"
echo "Git Ref: $GITHUB_REF"
echo "Workflow Actor: $GITHUB_ACTOR"
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js version 16
uses: actions/[email protected]
with:
node-version: "16"
cache: "npm"
- name: Install Node modules
run: npm ci
- name: Run linters in CI/CD mode
run: npm run lint:ci
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center"><a href="https://github.com/svengreb/tmpl-go/releases/latest"><img src="https://img.shields.io/github/release/svengreb/tmpl-go.svg?style=flat-square&label=Release&logo=github&logoColor=eceff4&colorA=4c566a&colorB=88c0d0"/></a> <a href="https://github.com/svengreb/tmpl-go/blob/main/CHANGELOG.md"><img src="https://img.shields.io/github/release/svengreb/tmpl-go.svg?style=flat-square&label=Changelog&logo=github&logoColor=eceff4&colorA=4c566a&colorB=88c0d0"/></a></p>

<p align="center"><a href="https://github.com/svengreb/tmpl-go/actions?query=workflow%3Aci" target="_blank"><img src="https://img.shields.io/github/workflow/status/svengreb/tmpl-go/ci.svg?style=flat-square&label=CI&logo=github&logoColor=eceff4&colorA=4c566a"/></a></p>
<p align="center"><a href="https://github.com/svengreb/tmpl-go/actions?query=workflow%3Aci-go" target="_blank"><img src="https://img.shields.io/github/workflow/status/svengreb/tmpl-go/ci-go.svg?style=flat-square&label=CI%20Go&logo=github&logoColor=eceff4&colorA=4c566a"/></a> <a href="https://github.com/svengreb/tmpl-go/actions?query=workflow%3Aci-node" target="_blank"><img src="https://img.shields.io/github/workflow/status/svengreb/tmpl-go/ci-node.svg?style=flat-square&label=CI%20Node&logo=github&logoColor=eceff4&colorA=4c566a"/></a></p>

<p align="center"><a href="https://golang.org/doc/effective_go.html#formatting" target="_blank"><img src="https://img.shields.io/static/v1?style=flat-square&label=Go%20Style%20Guide&message=gofmt&logo=go&logoColor=eceff4&colorA=4c566a&colorB=88c0d0"/></a> <a href="https://github.com/arcticicestudio/styleguide-markdown/releases/latest" target="_blank"><img src="https://img.shields.io/github/release/arcticicestudio/styleguide-markdown.svg?style=flat-square&label=Markdown%20Style%20Guide&logoColor=eceff4&colorA=4c566a&colorB=88c0d0&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzOSIgaGVpZ2h0PSIzOSIgdmlld0JveD0iMCAwIDM5IDM5Ij48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiNEOERFRTkiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLW1pdGVybGltaXQ9IjEwIiBkPSJNMS41IDEuNWgzNnYzNmgtMzZ6Ii8%2BPHBhdGggZmlsbD0iI0Q4REVFOSIgZD0iTTIwLjY4MyAyNS42NTVsNS44NzItMTMuNDhoLjU2Nmw1Ljg3MyAxMy40OGgtMS45OTZsLTQuMTU5LTEwLjA1Ni00LjE2MSAxMC4wNTZoLTEuOTk1em0tMi42OTYgMGwtMTMuNDgtNS44NzJ2LS41NjZsMTMuNDgtNS44NzJ2MS45OTVMNy45MzEgMTkuNWwxMC4wNTYgNC4xNnoiLz48L3N2Zz4%3D"/></a> <a href="https://github.com/arcticicestudio/styleguide-git/releases/latest" target="_blank"><img src="https://img.shields.io/github/release/arcticicestudio/styleguide-git.svg?style=flat-square&label=Git%20Style%20Guide&logoColor=eceff4&colorA=4c566a&colorB=88c0d0&logo=git"/></a></p>

Expand All @@ -21,7 +21,7 @@ It is built on top of the [base _tmpl_ repository][tmpl] which provides essentia

- [Build on top of the base _tmpl_ template repository][tmpl] that provides essential features for any project
- Extended configurations for [GitHub specific features](#github) that are already provided in the base _tmpl_ template repository:
- [CI/CD action workflow](#cicd-action-workflow) for Go specific tasks
- [CI/CD action workflows](#cicd-action-workflows) for Go specific tasks
- [Automated updates and security vulnerability alerts](#automated-dependency-updates) for [Go Module][go-doc-mod] and [Yarn][]/[NPM][] dependencies through the native [Dependabot][] integration
- [Go Module](#go-module) to ensure a stable dependency management
- [golangci-lint][] to [check the Go code quality](#go-code-quality-linting)
Expand Down Expand Up @@ -78,9 +78,9 @@ The sections below contain more detailed information about each feature, the san

This template repository has partially been designed for repositories hosted on GitHub and makes use of many of [its fantastic features][gh-features].

#### CI/CD Action Workflow
#### CI/CD Action Workflows

The [GitHub Actions][gh-feat-actions] `.github/workflows` directory includes a basic [CI/CD workflow file][gh-docs-act-ref-syntax] that runs for changes in the Git `main` branch and `v*` tags. The `lint-node` job is [derived from the _tmpl_ template repository][gh-tmpl#gh_act_cicd] and runs all [Node.js based linters][gh-tmpl#overview] that are also included in this template repository. The `lint-go` job runs all [configured `golangci-lint` linters](#go-code-quality-linting) while the `test` job runs all [tests with coverage report][go-doc-cmd-cover] and enabled [race detector][go-doc-race_detector].
The [GitHub Actions][gh-feat-actions] `.github/workflows` directory includes a basic [CI/CD workflow files][gh-docs-act-ref-syntax] that run for changes in the Git `main` branch and `v*` tags. The `lint-node` job is [derived from the _tmpl_ template repository][gh-tmpl#gh_act_cicd] and runs all [Node.js based linters][gh-tmpl#overview] that are also included in this template repository. The `ci-go` workflow runs all [configured `golangci-lint` linters](#go-code-quality-linting) and the `test` job runs all [tests with coverage report][go-doc-cmd-cover] and enabled [race detector][go-doc-race_detector].

#### Automated Dependency Updates

Expand All @@ -98,7 +98,7 @@ To ensure a good code quality the Go ecosystem has hundreds of linters, each wit
The actual runner is open source and can be used locally as well in any private CI/CD pipeline.
The [`.golangci.yml` configuration file][gh-blob-dot_golangci] is located in the root of this template repository and comes with sane default configurations that can be simply adjusted and extended.

The runner is also used in the [the existing _CI_ GitHub action workflow][gh-act-ci] through the [golangci-lint-action][] GitHub action that has been created by the golangci-lint maintainers. See the [“CI/CD Action Workflow](#cicd-action-workflow) section for more details.
The runner is also used in the [the existing _CI_ GitHub action workflows][gh-act-ci] through the [golangci-lint-action][] GitHub action that has been created by the golangci-lint maintainers. See the [“CI/CD Action Workflows](#cicd-action-workflows) section for more details.

## Usage

Expand Down
Loading

0 comments on commit d8fa563

Please sign in to comment.