diff --git a/README.md b/README.md index 80fefb1..a842986 100644 --- a/README.md +++ b/README.md @@ -409,7 +409,6 @@ ci: - ubuntu-latest - windows-latest coveralls: true - postgres: true ignorePaths: [] ``` @@ -419,13 +418,13 @@ successful on multiple operating systems. Default value for this is `ubuntu-late If `coveralls` is `true` then your test coverage report will be uploaded to [Coveralls]. Make sure that you have enabled Coveralls for your GitHub repo beforehand. -If `postgres` is `true` then a PostgreSQL service container will be added for the `test` job. -You can connect to this PostgreSQL service at `localhost:54321` with `postgres` as username and password ([More info][postgres-service-container]). - `ignorePaths` specifies a list of filename patterns. Workflows will not trigger if a path name matches a pattern in this list. [More info][ref-onpushpull] and [filter pattern cheat sheet][ref-pattern-cheat-sheet]. This option is not defined by default. +If your application depends on `github.com/lib/pq`, the latest PostgreSQL server binaries will be available in the container when tests are executed. +This is intended for use with `github.com/sapcc/go-bits/easypg`, which can launch a PostgreSQL server during `func TestMain`; see documentation in package easypg for details. + ### `githubWorkflow.pushContainerToGhcr` If `enabled` is set to true, the generated `Dockerfile` is built for the platforms `linux/amd64` and `linux/arm64` and pushed to the repository path under `ghcr.io`. @@ -496,7 +495,6 @@ license: [doublestar-pattern]: https://github.com/bmatcuk/doublestar#patterns [govulncheck]: https://github.com/golang/vuln [misspell]: https://github.com/client9/misspell -[postgres-service-container]: https://docs.github.com/en/actions/guides/creating-postgresql-service-containers#testing-the-postgresql-service-container [ref-onpushpull]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestpaths [ref-pattern-cheat-sheet]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet [ref-runs-on]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on diff --git a/internal/core/config.go b/internal/core/config.go index 2d13794..2e7e38c 100644 --- a/internal/core/config.go +++ b/internal/core/config.go @@ -129,7 +129,6 @@ type CIWorkflowConfig struct { IgnorePaths []string `yaml:"ignorePaths"` RunnerType []string `yaml:"runOn"` Coveralls bool `yaml:"coveralls"` - Postgres bool `yaml:"postgres"` } // LicenseWorkflowConfig appears in type Configuration. @@ -242,11 +241,7 @@ func (c *Configuration) Validate() { // Validate CI workflow configuration. if ghwCfg.CI.Enabled { if len(ghwCfg.CI.RunnerType) > 1 && !strings.HasPrefix(ghwCfg.CI.RunnerType[0], "ubuntu") { - logg.Fatal("githubWorkflow.ci.runOn must only define a single Ubuntu based runner when githubWorkflow.ci.postgres is enabled") - } - } else { - if ghwCfg.CI.Postgres { - logg.Fatal("githubWorkflow.ci.enabled must be set to 'true' when githubWorkflow.ci.postgres is enabled") + logg.Fatal("githubWorkflow.ci.runOn must only define a single Ubuntu based runner when githubWorkflow.ci.enabled is true") } } } diff --git a/internal/ghworkflow/workflow_ci.go b/internal/ghworkflow/workflow_ci.go index ecc4da3..163baee 100644 --- a/internal/ghworkflow/workflow_ci.go +++ b/internal/ghworkflow/workflow_ci.go @@ -5,7 +5,6 @@ package ghworkflow import ( "fmt" - "strings" "github.com/sapcc/go-makefile-maker/internal/core" "github.com/sapcc/go-makefile-maker/internal/golang" @@ -41,30 +40,18 @@ func ciWorkflow(cfg core.Configuration, sr golang.ScanResult) { testCmd := []string{ "make build/cover.out", } - if ghwCfg.CI.Postgres || sr.UsesPostgres { + if sr.UsesPostgres { testCmd = append([]string{ "sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y", "sudo apt-get install --no-install-recommends postgresql-" + core.DefaultPostgresVersion, fmt.Sprintf("export PATH=/usr/lib/postgresql/%s/bin:$PATH", core.DefaultPostgresVersion), }, testCmd...) - // TODO remove this service once all users migrated to use github.com/sapcc/go-bits/easypg - testJob.Services = map[string]jobService{"postgres": { - Image: "postgres:" + core.DefaultPostgresVersion, - Env: map[string]string{"POSTGRES_PASSWORD": "postgres"}, - Ports: []string{"54321:5432"}, - Options: strings.Join([]string{ - // Set health checks to wait until postgres has started - "--health-cmd pg_isready", - "--health-interval 10s", - "--health-timeout 5s", - "--health-retries 5", - }, " "), - }} } testJob.addStep(jobStep{ Name: "Run tests and generate coverage report", Run: makeMultilineYAMLString(testCmd), }) + if ghwCfg.CI.Coveralls && !ghwCfg.IsSelfHostedRunner { multipleOS := len(ghwCfg.CI.RunnerType) > 1 env := map[string]string{ diff --git a/internal/makefile/render.go b/internal/makefile/render.go index 6bb8bfd..4b21952 100644 --- a/internal/makefile/render.go +++ b/internal/makefile/render.go @@ -18,9 +18,6 @@ import ( "github.com/sapcc/go-makefile-maker/internal/golang" ) -//go:embed with-postgres-db.sh -var withPostgresDBScript []byte - // Render renders the Makefile. func Render(cfg core.Configuration, sr golang.ScanResult) { f := must.Return(os.Create("Makefile")) @@ -62,7 +59,12 @@ func Render(cfg core.Configuration, sr golang.ScanResult) { if sr.UsesPostgres { must.Succeed(os.MkdirAll("testing", os.ModePerm)) - must.Succeed(os.WriteFile("testing/with-postgres-db.sh", withPostgresDBScript, 0666)) + + // Cleanup obsolete helper script that was previously managed by this tool. + err := os.Remove("testing/with-postgres-db.sh") + if !os.IsNotExist(err) { + must.Succeed(err) + } } } diff --git a/internal/makefile/with-postgres-db.sh b/internal/makefile/with-postgres-db.sh deleted file mode 100755 index 3c370c8..0000000 --- a/internal/makefile/with-postgres-db.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/sh - -# Copyright 2022 SAP SE -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# SPDX-License-Identifier: Apache-2.0 - -# shellcheck shell=ash -set -euo pipefail - -# Darwin compatibility -if hash greadlink >/dev/null 2>/dev/null; then - readlink() { greadlink "$@"; } -fi - -# set working directory to repo root -cd "$(dirname "$(dirname "$(readlink -f "$0")")")" - -step() { - printf '\x1B[1;36m>>\x1B[0;36m %s...\x1B[0m\n' "$1" -} - -if [ ! -d testing/postgresql-data/ ]; then - step "First-time setup: Creating PostgreSQL database for testing" - initdb -A trust -U postgres testing/postgresql-data/ -fi -mkdir -p testing/postgresql-run/ - -step "Configuring PostgreSQL" -sed -ie '/^#\?\(external_pid_file\|unix_socket_directories\|port\)\b/d' testing/postgresql-data/postgresql.conf -( - echo "external_pid_file = '${PWD}/testing/postgresql-run/pid'" - echo "unix_socket_directories = '${PWD}/testing/postgresql-run'" - echo "port = 54321" -) >> testing/postgresql-data/postgresql.conf - -# usage in trap is not recognized -# shellcheck disable=SC2317 -stop_postgres() { - EXIT_CODE=$? - step "Stopping PostgreSQL" - pg_ctl stop -D testing/postgresql-data/ -w -s - exit "${EXIT_CODE}" -} - -step "Starting PostgreSQL" -rm -f -- testing/postgresql.log -trap stop_postgres EXIT INT TERM -pg_ctl start -D testing/postgresql-data/ -l testing/postgresql.log -w -s - -step "Running command: $*" -set +e -"$@" -EXIT_CODE=$? -set -e - -exit "${EXIT_CODE}"