error-pages/.github/workflows/tests.yml

211 lines
6.2 KiB
YAML
Raw Normal View History

2020-07-08 11:12:21 +00:00
name: tests
on:
push:
2021-09-29 15:38:50 +00:00
branches: [master, main]
tags-ignore: ['**']
paths-ignore: ['**.md']
2020-07-08 11:12:21 +00:00
pull_request:
2021-09-29 15:38:50 +00:00
paths-ignore: ['**.md']
2020-07-08 11:12:21 +00:00
jobs: # Docs: <https://git.io/JvxXE>
2021-04-13 13:55:03 +00:00
gitleaks:
name: Gitleaks
runs-on: ubuntu-20.04
steps:
2021-09-29 15:38:50 +00:00
- uses: actions/checkout@v2
with: {fetch-depth: 0}
- uses: zricethezav/gitleaks-action@v1 # Action page: <https://github.com/zricethezav/gitleaks-action>
golangci-lint:
name: Golang-CI (lint)
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Run linter
uses: golangci/golangci-lint-action@v2 # Action page: <https://github.com/golangci/golangci-lint-action>
2021-04-13 13:55:03 +00:00
with:
2021-09-29 15:38:50 +00:00
version: v1.42 # without patch version
only-new-issues: false # show only new issues if it's a pull request
2021-04-13 13:55:03 +00:00
2021-09-29 15:38:50 +00:00
go-test:
name: Unit tests
runs-on: ubuntu-20.04
steps:
- uses: actions/setup-go@v2
with: {go-version: 1.17}
2021-04-13 13:55:03 +00:00
2021-09-29 15:38:50 +00:00
- uses: actions/checkout@v2
with: {fetch-depth: 2} # Fixes codecov error 'Issue detecting commit SHA'
- name: Go modules Cache # Docs: <https://git.io/JfAKn#go---modules>
uses: actions/cache@v2
id: go-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-
- if: steps.go-cache.outputs.cache-hit != 'true'
run: go mod download
- name: Run Unit tests
run: go test -race -covermode=atomic -coverprofile /tmp/coverage.txt ./...
- uses: codecov/codecov-action@v2 # https://github.com/codecov/codecov-action
continue-on-error: true
with:
file: /tmp/coverage.txt
token: ${{ secrets.CODECOV_TOKEN }}
build:
name: Build for ${{ matrix.os }} (${{ matrix.arch }})
2021-03-04 06:28:48 +00:00
runs-on: ubuntu-20.04
2021-09-29 15:38:50 +00:00
strategy:
fail-fast: false
matrix:
os: [linux, darwin] # linux, freebsd, darwin, windows
arch: [amd64] # amd64, 386
needs: [golangci-lint, go-test]
2020-07-08 11:12:21 +00:00
steps:
2021-09-29 15:38:50 +00:00
- uses: actions/setup-go@v2
with: {go-version: 1.17}
- uses: actions/checkout@v2
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
- uses: gacts/github-slug@v1
id: slug
- name: Go modules Cache # Docs: <https://git.io/JfAKn#go---modules>
uses: actions/cache@v2
2020-07-08 11:12:21 +00:00
with:
2021-09-29 15:38:50 +00:00
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-
- run: go mod download
- name: Build application
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
CGO_ENABLED: 0
LDFLAGS: -s -w -X github.com/tarampampam/error-pages/internal/version.version=${{ steps.slug.outputs.branch-name-slug }}@${{ steps.slug.outputs.commit-hash-short }}
run: go build -trimpath -ldflags "$LDFLAGS" -o ./error-pages ./cmd/error-pages/
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
- name: Try to execute
if: matrix.os == 'linux'
run: ./error-pages version && ./error-pages -h
- uses: actions/upload-artifact@v2
2020-07-08 11:12:21 +00:00
with:
2021-09-29 15:38:50 +00:00
name: error-pages-${{ matrix.os }}-${{ matrix.arch }}
path: error-pages
if-no-files-found: error
retention-days: 1
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
generate:
name: Run templates generator
runs-on: ubuntu-20.04
needs: [build]
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: error-pages-linux-amd64
path: .artifact
- name: Prepare binary file to run
working-directory: .artifact
run: mv ./error-pages ./../error-pages && chmod +x ./../error-pages
2020-07-08 11:12:21 +00:00
- name: Run generator
2021-09-29 15:38:50 +00:00
run: ./error-pages build ./out --verbose --index
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
- name: Test files creation
2021-04-13 13:55:03 +00:00
run: |
test -f ./out/index.html
test -f ./out/ghost/404.html
test -f ./out/l7-dark/404.html
test -f ./out/l7-light/404.html
test -f ./out/shuffle/404.html
test -f ./out/noise/404.html
test -f ./out/hacker-terminal/404.html
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
docker-image:
2020-07-08 11:22:29 +00:00
name: Build docker image
2021-03-04 06:28:48 +00:00
runs-on: ubuntu-20.04
2021-09-29 15:38:50 +00:00
needs: [golangci-lint, go-test]
steps:
- uses: actions/checkout@v2
- uses: gacts/github-slug@v1
id: slug
- uses: docker/build-push-action@v2 # Action page: <https://github.com/docker/build-push-action>
with:
context: .
file: Dockerfile
push: false
build-args: "APP_VERSION=${{ steps.slug.outputs.branch-name-slug }}@${{ steps.slug.outputs.commit-hash-short }}"
tags: app:ci
- run: docker save app:ci > ./docker-image.tar
- uses: actions/upload-artifact@v2
with:
name: docker-image
path: ./docker-image.tar
retention-days: 1
scan-docker-image:
name: Scan the docker image
runs-on: ubuntu-20.04
needs: [docker-image]
2020-07-08 11:12:21 +00:00
steps:
2021-09-29 15:38:50 +00:00
- uses: actions/download-artifact@v2
with:
name: docker-image
path: .artifact
2020-07-08 11:12:21 +00:00
2021-09-29 15:38:50 +00:00
- working-directory: .artifact
run: docker load < docker-image.tar
2020-07-08 11:22:29 +00:00
2021-09-29 15:38:50 +00:00
- uses: anchore/scan-action@v3 # action page: <https://github.com/anchore/scan-action>
2021-04-13 13:55:03 +00:00
with:
2021-09-29 15:38:50 +00:00
image: app:ci
2021-04-13 13:55:03 +00:00
fail-build: true
2021-09-29 15:38:50 +00:00
severity-cutoff: low # negligible, low, medium, high or critical
poke-docker-image:
name: Run the docker image
runs-on: ubuntu-20.04
needs: [docker-image]
timeout-minutes: 2
steps:
- uses: actions/download-artifact@v2
with:
name: docker-image
path: .artifact
- working-directory: .artifact
run: docker load < docker-image.tar
2021-04-13 13:55:03 +00:00
2021-09-29 15:38:50 +00:00
- name: Run container with the app
run: docker run --rm -d -p "8080:8080/tcp" --name app app:ci
2020-07-08 11:22:29 +00:00
2021-09-29 15:38:50 +00:00
- name: Wait for container "healthy" state
run: until [[ "`docker inspect -f {{.State.Health.Status}} app`" == "healthy" ]]; do echo "wait 1 sec.."; sleep 1; done
2020-07-08 17:18:30 +00:00
2021-09-29 15:38:50 +00:00
- run: curl --fail http://127.0.0.1:8080/500.html
- run: curl --fail http://127.0.0.1:8080/400.html
- run: curl --fail http://127.0.0.1:8080/health/live
- run: test $(curl --write-out %{http_code} --silent --output /dev/null http://127.0.0.1:8080/) -eq 404
2020-07-10 07:43:48 +00:00
2021-09-29 15:38:50 +00:00
- name: Stop the container
if: always()
run: docker kill app