From 20cd69883a9ab499181f2a2ac39bf68bf6000947 Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Fri, 25 Aug 2023 22:33:29 +0900 Subject: [PATCH] build: fix misc --- .bin/direnv | 47 +++++++------- .bin/golangci-lint | 58 +++++++++-------- .github/workflows/ci.yml | 67 -------------------- .github/workflows/go-lint.yml | 115 ++++++++++++++++++++++++++++++++++ .github/workflows/go-test.yml | 112 +++++++++++++++++++++++++++++++++ .versenv.env | 1 + Makefile | 29 ++++++++- 7 files changed, 308 insertions(+), 121 deletions(-) delete mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/go-lint.yml create mode 100644 .github/workflows/go-test.yml diff --git a/.bin/direnv b/.bin/direnv index caea287..8f2ffea 100755 --- a/.bin/direnv +++ b/.bin/direnv @@ -1,6 +1,6 @@ #!/usr/bin/env bash # shellcheck disable=SC2155 -# MIT License Copyright (c) 2022 ginokent https://github.com/versenv/versenv +# LISENCE: https://github.com/versenv/versenv/blob/HEAD/LICENSE set -Eeu -o pipefail # versenv unique @@ -48,22 +48,23 @@ ExecProgram() { exec "${prog_version_exe:?}" "$@" <&0 } -# MIT License Copyright (c) 2021 ginokent https://github.com/rec-logger/rec.sh +# LISENCE: https://github.com/kunitsucom/rec.sh/blob/HEAD/LICENSE # Common +if [ -t 2 ]; then REC_COLOR=true; else REC_COLOR=''; fi _recRFC3339() { date "+%Y-%m-%dT%H:%M:%S%z" | sed "s/\(..\)$/:\1/"; } _recCmd() { for a in "$@"; do if echo "${a:-}" | grep -Eq "[[:blank:]]"; then printf "'%s' " "${a:-}"; else printf "%s " "${a:-}"; fi; done | sed "s/ $//"; } # Color -RecDefault() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;35m DEFAULT\\033[0m] \"\$0\"\"}" 1>&2; } -RecDebug() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;34m DEBUG\\033[0m] \"\$0\"\"}" 1>&2; } -RecInfo() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;32m INFO\\033[0m] \"\$0\"\"}" 1>&2; } -RecNotice() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;36m NOTICE\\033[0m] \"\$0\"\"}" 1>&2; } -RecWarning() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;33m WARNING\\033[0m] \"\$0\"\"}" 1>&2; } -RecError() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;31m ERROR\\033[0m] \"\$0\"\"}" 1>&2; } -RecCritical() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;31m CRITICAL\\033[0m] \"\$0\"\"}" 1>&2; } -RecAlert() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;41m ALERT\\033[0m] \"\$0\"\"}" 1>&2; } -RecEmergency() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;41mEMERGENCY\\033[0m] \"\$0\"\"}" 1>&2; } +RecDefault() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;35m} DEFAULT${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecDebug() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;34m} DEBUG${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecInfo() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;32m} INFO${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecNotice() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;36m} NOTICE${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecWarning() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;33m} WARNING${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecError() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;31m} ERROR${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecCritical() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;1;31m} CRITICAL${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecAlert() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;41m} ALERT${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecEmergency() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;1;41m}EMERGENCY${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } RecExec() { RecInfo "$ $(_recCmd "$@")" && "$@"; } -RecRun() { _dlm="####R#E#C#D#E#L#I#M#I#T#E#R####" _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return ${_rtn:-0}; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfo "$ ${_cmd:-}" && { [ -z "${_stdout:-}" ] || RecInfo "${_stdout:?}"; } && { [ -z "${_stderr:-}" ] || RecWarning "${_stderr:?}"; } && return ${_rtn:-0}; } +RecRun() { _dlm="####R#E#C#D#E#L#I#M#I#T#E#R####" && _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return "${_rtn:-0}"; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfo "$ ${_cmd:-}" && { [ -z "${_stdout:-}" ] || RecInfo "${_stdout:?}"; } && { [ -z "${_stderr:-}" ] || RecWarning "${_stderr:?}"; } && return "${_rtn:-0}"; } # versenv common DownloadURL() { @@ -79,16 +80,18 @@ DownloadURL() { fi } -GetURL() { - local url="${1:?}" - if command -v wget >/dev/null; then - wget --secure-protocol=TLSv1_2 --dns-timeout=2 --connect-timeout=2 -q -O- "${url:?}" - elif command -v curl >/dev/null; then - curl --tlsv1.2 --connect-timeout 2 -fLRSs "${url:?}" - else - RecCritical "command not found: curl or wget" - exit 127 - fi +GetURLs() { + for arg in "$@"; do + local url="${arg:?}" + if command -v wget >/dev/null; then + wget --secure-protocol=TLSv1_2 --dns-timeout=2 --connect-timeout=2 -q -O- "${url:?}" + elif command -v curl >/dev/null; then + curl --tlsv1.2 --connect-timeout 2 -fLRSs "${url:?}" + else + RecCritical "command not found: curl or wget" + exit 127 + fi + done } HeadURL() { diff --git a/.bin/golangci-lint b/.bin/golangci-lint index f747025..f2eb2b0 100755 --- a/.bin/golangci-lint +++ b/.bin/golangci-lint @@ -1,11 +1,11 @@ #!/usr/bin/env bash # shellcheck disable=SC2155 -# MIT License Copyright (c) 2022 ginokent https://github.com/versenv/versenv +# LISENCE: https://github.com/versenv/versenv/blob/HEAD/LICENSE set -Eeu -o pipefail # versenv unique exe_filename=golangci-lint -env_key_version=GOLANGCI_VERSION +env_key_version=GOLANGCI_LINT_VERSION git_url_prefix=https://github.com/golangci/golangci-lint GetProgramLatestStableVersion() { HeadURL "${git_url_prefix:?}/releases/latest" | awk -F"/tag/" "/^[Ll]ocation:/ {print \$2}" | tr -d "[:cntrl:]" | tr -d "^v" | tail -n 1; } @@ -17,14 +17,9 @@ FindTargetZipDownloadURL() { local prog_os="${2:?}" local prog_arch="${3:?}" local path_suffix && path_suffix=$( - if ! GetURL "${git_url_prefix:?}/releases" | grep -Eo "href=\"[^\"]+/v?${prog_version:?}/[^\"]*${prog_os:?}[^\"]*${prog_arch:?}[^\"]*\.(zip|tar|tgz|tar\.gz)" | sed 's/href="//'; then - i=2 - until { GetURL "${git_url_prefix:?}/releases?page=${i:?}" | grep -Eo "href=\"[^\"]+/v?${prog_version:?}/[^\"]*${prog_os:?}[^\"]*${prog_arch:?}[^\"]*\.(zip|tar|tgz|tar\.gz)" | sed 's/href="//'; }; do - i=$((i+1)) - done - fi + GetURLs "${git_url_prefix:?}/releases/expanded_assets/"{,v}"${prog_version:?}" | grep -Eo "href=\"[^\"]+/v?${prog_version:?}/[^\"]*${prog_os:?}[^\"]*${prog_arch:?}[^\"]*\.(zip|tar|tgz|tar\.gz)" | sed 's/href="//' ) - echo "https://github.com/${path_suffix:?}" + echo "https://github.com${path_suffix:?}" } Unzip() { @@ -79,22 +74,23 @@ ExecProgram() { exec "${prog_version_exe:?}" "$@" <&0 } -# MIT License Copyright (c) 2021 ginokent https://github.com/rec-logger/rec.sh +# LISENCE: https://github.com/kunitsucom/rec.sh/blob/HEAD/LICENSE # Common +if [ -t 2 ]; then REC_COLOR=true; else REC_COLOR=''; fi _recRFC3339() { date "+%Y-%m-%dT%H:%M:%S%z" | sed "s/\(..\)$/:\1/"; } _recCmd() { for a in "$@"; do if echo "${a:-}" | grep -Eq "[[:blank:]]"; then printf "'%s' " "${a:-}"; else printf "%s " "${a:-}"; fi; done | sed "s/ $//"; } # Color -RecDefault() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;35m DEFAULT\\033[0m] \"\$0\"\"}" 1>&2; } -RecDebug() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;34m DEBUG\\033[0m] \"\$0\"\"}" 1>&2; } -RecInfo() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;32m INFO\\033[0m] \"\$0\"\"}" 1>&2; } -RecNotice() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;36m NOTICE\\033[0m] \"\$0\"\"}" 1>&2; } -RecWarning() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;33m WARNING\\033[0m] \"\$0\"\"}" 1>&2; } -RecError() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;31m ERROR\\033[0m] \"\$0\"\"}" 1>&2; } -RecCritical() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;31m CRITICAL\\033[0m] \"\$0\"\"}" 1>&2; } -RecAlert() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;41m ALERT\\033[0m] \"\$0\"\"}" 1>&2; } -RecEmergency() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;41mEMERGENCY\\033[0m] \"\$0\"\"}" 1>&2; } +RecDefault() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;35m} DEFAULT${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecDebug() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;34m} DEBUG${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecInfo() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;32m} INFO${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecNotice() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;36m} NOTICE${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecWarning() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;33m} WARNING${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecError() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;31m} ERROR${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecCritical() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;1;31m} CRITICAL${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecAlert() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;41m} ALERT${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } +RecEmergency() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [${REC_COLOR:+\\033[0;1;41m}EMERGENCY${REC_COLOR:+\\033[0m}] \"\$0\"\"}" 1>&2; } RecExec() { RecInfo "$ $(_recCmd "$@")" && "$@"; } -RecRun() { _dlm="####R#E#C#D#E#L#I#M#I#T#E#R####" _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return ${_rtn:-0}; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfo "$ ${_cmd:-}" && { [ -z "${_stdout:-}" ] || RecInfo "${_stdout:?}"; } && { [ -z "${_stderr:-}" ] || RecWarning "${_stderr:?}"; } && return ${_rtn:-0}; } +RecRun() { _dlm="####R#E#C#D#E#L#I#M#I#T#E#R####" && _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return "${_rtn:-0}"; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfo "$ ${_cmd:-}" && { [ -z "${_stdout:-}" ] || RecInfo "${_stdout:?}"; } && { [ -z "${_stderr:-}" ] || RecWarning "${_stderr:?}"; } && return "${_rtn:-0}"; } # versenv common DownloadURL() { @@ -110,16 +106,18 @@ DownloadURL() { fi } -GetURL() { - local url="${1:?}" - if command -v wget >/dev/null; then - wget --secure-protocol=TLSv1_2 --dns-timeout=2 --connect-timeout=2 -q -O- "${url:?}" - elif command -v curl >/dev/null; then - curl --tlsv1.2 --connect-timeout 2 -fLRSs "${url:?}" - else - RecCritical "command not found: curl or wget" - exit 127 - fi +GetURLs() { + for arg in "$@"; do + local url="${arg:?}" + if command -v wget >/dev/null; then + wget --secure-protocol=TLSv1_2 --dns-timeout=2 --connect-timeout=2 -q -O- "${url:?}" + elif command -v curl >/dev/null; then + curl --tlsv1.2 --connect-timeout 2 -fLRSs "${url:?}" + else + RecCritical "command not found: curl or wget" + exit 127 + fi + done } HeadURL() { diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 4873f6f..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: CI -# ~~ -# https://github.com/kunitsucom/ccc/workflows/CI/badge.svg -# ~~ - -on: - push: - branches: - - main - pull_request: - branches: - - main - workflow_dispatch: - inputs: {} - -defaults: - run: - shell: bash - -jobs: - ci: - name: CI - # cf. https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - # cf. https://github.com/actions/setup-go#usage - - uses: actions/setup-go@v3 - with: - go-version-file: go.mod - - # cf. https://github.com/actions/cache/blob/main/examples.md#go---modules - - uses: actions/cache@v3 - id: cache - with: - path: | - ~/.cache/go-build - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - # cf. https://github.com/actions/cache#skipping-steps-based-on-cache-hit - - name: Run go mod tidy - if: steps.cache.outputs.cache-hit != 'true' - run: | - go mod tidy - git diff --exit-code go.mod go.sum - - # cf. https://github.com/golangci/golangci-lint-action#how-to-use - - uses: golangci/golangci-lint-action@v3.3.0 - with: - version: latest - - - name: Run go test - env: - COLOR: true - run: | - go test -v -race -p=4 -parallel=8 -timeout=300s -cover -coverprofile=./coverage.txt ./... - go tool cover -func=./coverage.txt - - # cf. https://github.com/codecov/codecov-action#example-workflowyml-with-codecov-action - - uses: codecov/codecov-action@v3 - with: - token: ${{ secrets.CODECOV_TOKEN }} - files: ./coverage.txt diff --git a/.github/workflows/go-lint.yml b/.github/workflows/go-lint.yml new file mode 100644 index 0000000..086adf0 --- /dev/null +++ b/.github/workflows/go-lint.yml @@ -0,0 +1,115 @@ +name: go-lint +# ~~~~~~~ +# https://github.com/kunitsucom/ilog.go/workflows/go-lint/badge.svg +# ~~~~~~~ + +on: + push: + branches: + - main + paths-ignore: + - '.github/dependabot.yml' + - '.github/pull_request_template.md' + - '.github/release.yml' + - 'README.md' + pull_request: + # branches: + # - main + paths-ignore: + - '.github/dependabot.yml' + - '.github/pull_request_template.md' + - '.github/release.yml' + - 'README.md' + workflow_dispatch: + inputs: {} + +# NOTE: 連続で commit & push した時に最新の commit 以外のワークフローをキャンセルする +concurrency: + group: ${{ github.workflow }}-${{ github.base_ref }}-${{ github.head_ref }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + id-token: write + contents: read + +env: + WORKDIR: . + +defaults: + run: + shell: bash + +jobs: + go-lint: # NOTE: for Branch protection rule `Status checks that are required.` + runs-on: ubuntu-latest # ref. https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on + steps: + - uses: actions/checkout@v3 + - name: DEBUG + run: | + cat <<'DEBUG_DOC' + == DEBUG ======================================================= + github.ref: ${{ github.ref }} + github.event_name: ${{ github.event_name }} + -- toJSON(github.event.inputs) --------------------------------- + ${{ toJSON(github.event.inputs) }} + -- toJSON(github) ---------------------------------------------- + ${{ toJSON(github) }} + ================================================================ + DEBUG_DOC + shell: bash + - name: actions/cache for versenv + uses: actions/cache@v3 + with: + path: | + ~/.cache/versenv + key: versenv-${{ runner.os }}-${{ hashFiles('**/.versenv.env') }} + restore-keys: | + versenv-${{ runner.os }}- + - name: Add GITHUB_PATH, GITHUB_ENV + run: | + # Update GITHUB_PATH + cat <> $GITHUB_PATH + ${PWD}/${{ env.WORKDIR }}/.local/bin + ${PWD}/.bin + GITHUB_PATH + # Update GITHUB_ENV + grep -Ev '^\s*$|^\s*#' .versenv.env >> $GITHUB_ENV + - name: Setup versenv + run: | + # Setup versenv + direnv allow ${{ env.WORKDIR }} + make versenv + - uses: actions/setup-go@v4 # ref. https://github.com/actions/setup-go#usage + id: setup-go + with: + cache: false + go-version-file: ${{ env.WORKDIR }}/go.mod + - name: Get Golang info + id: golang-info + run: | + echo "GOVERSION=$(go version | cut -d' ' -f3)" >> "$GITHUB_OUTPUT" + echo "GOCACHE=$(go env GOCACHE)" >> "$GITHUB_OUTPUT" + - name: actions/cache for go + uses: actions/cache@v3 + with: + path: | + ~/go/pkg/mod + ${{ steps.golang-info.outputs.GOCACHE }} + key: ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}-${{ hashFiles('**/go.sum') }}-${{ hashFiles('**/*.go') }} + restore-keys: | + ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}-${{ hashFiles('**/go.sum') }}- + ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}- + ${{ runner.os }}-go- + - name: Run go mod tidy + if: ${{ steps.setup-go.outputs.cache-hit != 'true' }} + env: + GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }} + working-directory: ${{ env.WORKDIR }} + run: | + direnv exec . go mod tidy + git diff --exit-code go.mod go.sum + - uses: golangci/golangci-lint-action@v3.7.0 # ref. https://github.com/golangci/golangci-lint-action#how-to-use + with: + working-directory: ${{ env.WORKDIR }} + args: --timeout=600s + version: ${{ env.GOLANGCI_LINT_VERSION }} diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..5581b9d --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,112 @@ +name: go-test +# ~~~~~~~ +# https://github.com/kunitsucom/ilog.go/workflows/go-test/badge.svg +# ~~~~~~~ + +on: + push: + branches: + - main + paths-ignore: + - '.github/dependabot.yml' + - '.github/pull_request_template.md' + - '.github/release.yml' + - 'README.md' + pull_request: + # branches: + # - main + paths-ignore: + - '.github/dependabot.yml' + - '.github/pull_request_template.md' + - '.github/release.yml' + - 'README.md' + workflow_dispatch: + inputs: {} + +# NOTE: 連続で commit & push した時に最新の commit 以外のワークフローをキャンセルする +concurrency: + group: ${{ github.workflow }}-${{ github.base_ref }}-${{ github.head_ref }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + id-token: write + contents: read + +env: + WORKDIR: . + +defaults: + run: + shell: bash + +jobs: + go-test: # NOTE: for Branch protection rule `Status checks that are required.` + runs-on: ubuntu-latest # ref. https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on + steps: + - uses: actions/checkout@v3 + - name: DEBUG + run: | + cat <<'DEBUG_DOC' + == DEBUG ======================================================= + github.ref: ${{ github.ref }} + github.event_name: ${{ github.event_name }} + -- toJSON(github.event.inputs) --------------------------------- + ${{ toJSON(github.event.inputs) }} + -- toJSON(github) ---------------------------------------------- + ${{ toJSON(github) }} + ================================================================ + DEBUG_DOC + shell: bash + - name: actions/cache for versenv + uses: actions/cache@v3 + with: + path: | + ~/.cache/versenv + key: versenv-${{ runner.os }}-${{ hashFiles('**/.versenv.env') }} + restore-keys: | + versenv-${{ runner.os }}- + - name: Add GITHUB_PATH, GITHUB_ENV + run: | + # Update GITHUB_PATH + cat <> $GITHUB_PATH + ${PWD}/${{ env.WORKDIR }}/.local/bin + ${PWD}/.bin + GITHUB_PATH + # Update GITHUB_ENV + grep -Ev '^\s*$|^\s*#' .versenv.env >> $GITHUB_ENV + - name: Setup versenv + run: | + # Setup versenv + direnv allow ${{ env.WORKDIR }} + make versenv + - uses: actions/setup-go@v4 # ref. https://github.com/actions/setup-go#usage + id: setup-go + with: + cache: false + go-version-file: ${{ env.WORKDIR }}/go.mod + - name: Get Golang info + id: golang-info + run: | + echo "GOVERSION=$(go version | cut -d' ' -f3)" >> "$GITHUB_OUTPUT" + echo "GOCACHE=$(go env GOCACHE)" >> "$GITHUB_OUTPUT" + - name: actions/cache for go + uses: actions/cache@v3 + with: + path: | + ~/go/pkg/mod + ${{ steps.golang-info.outputs.GOCACHE }} + key: ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}-${{ hashFiles('**/go.sum') }}-${{ hashFiles('**/*.go') }} + restore-keys: | + ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}-${{ hashFiles('**/go.sum') }}- + ${{ runner.os }}-go-${{ steps.golang-info.outputs.GOVERSION }}- + ${{ runner.os }}-go- + - name: Run go test + env: + GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }} + working-directory: ${{ env.WORKDIR }} + run: | + direnv exec . make test + - uses: codecov/codecov-action@v3 # ref. https://github.com/codecov/codecov-action#example-workflowyml-with-codecov-action + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ${{ env.WORKDIR }}/coverage.txt diff --git a/.versenv.env b/.versenv.env index 278ae75..05f2813 100644 --- a/.versenv.env +++ b/.versenv.env @@ -4,4 +4,5 @@ DIRENV_VERSION=2.32.3 # https://github.com/golangci/golangci-lint/releases +# GOLANGCI_LINT_VERSION=latest GOLANGCI_LINT_VERSION=latest diff --git a/Makefile b/Makefile index 8fe1565..dc3a0d7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ SHELL := /usr/bin/env bash -Eeu -o pipefail REPO_ROOT := $(shell git rev-parse --show-toplevel || pwd || echo '.') +REPO_LOCAL_DIR := ${REPO_ROOT}/.local PRE_PUSH := ${REPO_ROOT}/.git/hooks/pre-push GO_MODULE_NAME := github.com/kunitsucom/ccc BUILD_VERSION := $(shell git describe --tags --exact-match HEAD 2>/dev/null || git rev-parse --short HEAD) @@ -7,6 +8,8 @@ BUILD_REVISION := $(shell git rev-parse HEAD) BUILD_BRANCH := $(shell git rev-parse --abbrev-ref HEAD | tr / -) BUILD_TIMESTAMP := $(shell git log -n 1 --format='%cI') +export PATH := ${REPO_LOCAL_DIR}/bin:${REPO_ROOT}/.bin:${PATH} + .DEFAULT_GOAL := help .PHONY: help help: githooks ## display this help documents @@ -14,10 +17,17 @@ help: githooks ## display this help documents .PHONY: setup setup: githooks ## Setup tools for development + # == SETUP ===================================================== + # versenv + make versenv + # -------------------------------------------------------------- + +.PHONY: versenv +versenv: # direnv - ./.bin/direnv allow + direnv allow . # golangci-lint - ./.bin/golangci-lint --version + golangci-lint --version .PHONY: githooks githooks: @@ -54,6 +64,21 @@ test: githooks ## Run go test and display coverage .PHONY: ci ci: lint credits test ## CI command set +.PHONY: act-check +act-check: + @if ! command -v act >/dev/null 2>&1; then \ + printf "\033[31;1m%s\033[0m\n" "act is not installed: brew install act" 1>&2; \ + exit 1; \ + fi + +.PHONY: act-go-lint +act-go-lint: act-check + act pull_request --container-architecture linux/amd64 -P ubuntu-latest=catthehacker/ubuntu:act-latest -W .github/workflows/go-lint.yml + +.PHONY: act-go-test +act-go-test: act-check + act pull_request --container-architecture linux/amd64 -P ubuntu-latest=catthehacker/ubuntu:act-latest -W .github/workflows/go-test.yml + .PHONY: goxz goxz: ci ## Run goxz for release command -v goxz || go install github.com/Songmu/goxz/cmd/goxz@latest