From 0e87f882a77d8005ffdcfb6902206615dc58e3d0 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 14 Dec 2021 17:20:06 +0000 Subject: [PATCH] DEV: Use discourse image for postgres in GitHub Actions (#15291) The discourse base image already contains a postgres installation, so pulling a separate postgres image is a little wasteful. Using the copy of Postgres in the discourse image saves about 20 seconds on every GitHub actions run. This commit sets up Postgres with a few performance-improving flags, which we were already using for the `rake docker:test` task (used on our internal CI system). --- .github/workflows/tests.yml | 24 ++++++------------------ lib/tasks/docker.rake | 10 +--------- script/start_test_db.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 27 deletions(-) create mode 100755 script/start_test_db.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7538d6d6d75..2ded21021bf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,6 @@ jobs: DISCOURSE_HOSTNAME: www.example.com RUBY_GLOBAL_METHOD_CACHE_SIZE: 131072 RAILS_ENV: test - PGHOST: postgres PGUSER: discourse PGPASSWORD: discourse USES_PARALLEL_DATABASES: ${{ matrix.build_type == 'backend' && matrix.target == 'core' }} @@ -32,27 +31,10 @@ jobs: matrix: build_type: [backend, frontend, annotations] target: [core, plugins] - postgres: ["13"] exclude: - build_type: annotations target: plugins - services: - postgres: - image: postgres:${{ matrix.postgres }} - ports: - - 5432:5432 - env: - POSTGRES_USER: discourse - POSTGRES_PASSWORD: discourse - POSTGRES_DB: discourse_test - options: >- - --mount type=tmpfs,destination=/var/lib/postgresql/data - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - steps: - uses: actions/checkout@master with: @@ -67,6 +49,12 @@ jobs: run: | redis-server /etc/redis/redis.conf & + - name: Start Postgres + run: | + chown -R postgres /var/run/postgresql + sudo -E -u postgres script/start_test_db.rb + sudo -u postgres psql -c "CREATE ROLE $PGUSER LOGIN SUPERUSER PASSWORD '$PGPASSWORD';" + - name: Bundler cache uses: actions/cache@v2 with: diff --git a/lib/tasks/docker.rake b/lib/tasks/docker.rake index 30f8675e9c6..90bc68d040c 100644 --- a/lib/tasks/docker.rake +++ b/lib/tasks/docker.rake @@ -102,16 +102,8 @@ task 'docker:test' do puts "Starting background redis" @redis_pid = Process.spawn('redis-server --dir tmp/test_data/redis') - @postgres_bin = "/usr/lib/postgresql/#{ENV['PG_MAJOR']}/bin/" - `#{@postgres_bin}initdb -D tmp/test_data/pg` - - # speed up db, never do this in production mmmmk - `echo fsync = off >> tmp/test_data/pg/postgresql.conf` - `echo full_page_writes = off >> tmp/test_data/pg/postgresql.conf` - `echo shared_buffers = 500MB >> tmp/test_data/pg/postgresql.conf` - puts "Starting postgres" - @pg_pid = Process.spawn("#{@postgres_bin}postmaster -D tmp/test_data/pg") + @pg_pid = Process.spawn("script/start_test_db.rb --exec") ENV["RAILS_ENV"] = "test" # this shaves all the creation of the multisite db off diff --git a/script/start_test_db.rb b/script/start_test_db.rb new file mode 100755 index 00000000000..280a6347f28 --- /dev/null +++ b/script/start_test_db.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +BIN = "/usr/lib/postgresql/#{ENV["PG_MAJOR"]}/bin" +DATA = "/tmp/test_data/pg" + +def run(*args) + system(*args, exception: true) +end + +should_exec = false +while a = ARGV.pop + if a == "--exec" + should_exec = true + else + raise "Unknown argument #{a}" + end +end + +run "#{BIN}/initdb -D #{DATA}" + +run "echo fsync = off >> #{DATA}/postgresql.conf" +run "echo full_page_writes = off >> #{DATA}/postgresql.conf" +run "echo shared_buffers = 500MB >> #{DATA}/postgresql.conf" + +if should_exec + exec "#{BIN}/postmaster -D #{DATA}" +else + run "#{BIN}/pg_ctl -D #{DATA} start" +end