diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml new file mode 100644 index 000000000..82fd768ff --- /dev/null +++ b/.github/workflows/build-docker.yml @@ -0,0 +1,69 @@ +name: Build Docker + +on: + pull_request: + branches: [ development ] + push: + branches: [ development ] + +jobs: + run-docker-checks-tests: + runs-on: ubuntu-latest + + steps: + - name: Check out repository code + uses: actions/checkout@v3 + + # Install Docker in GitHub Actions virtual machine + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + # ${{ secrets }} is saved in Repository secrets (Repo -> Settings -> Secrets and variables -> Actions) + # This .env file will be used by docker-compose.ci.yml + - name: Create .env file for Docker Compose + run: | + cat < .env + OED_SERVER_PORT=${{ secrets.OED_SERVER_PORT }} + OED_DB_USER=${{ secrets.OED_DB_USER }} + OED_DB_DATABASE=${{ secrets.OED_DB_DATABASE }} + OED_DB_TEST_DATABASE=${{ secrets.OED_DB_TEST_DATABASE }} + OED_DB_PASSWORD=${{ secrets.OED_DB_PASSWORD }} + OED_DB_PORT=${{ secrets.OED_DB_PORT }} + OED_TOKEN_SECRET=${{ secrets.OED_TOKEN_SECRET }} + EOF + + + # Build Docker images and run the containers -> integration smoke test + - name: Build & start containers + run: | + docker compose -f docker-compose.ci.yml build + docker compose -f docker-compose.ci.yml up -d + + # We need the database container for the further tests + - name: Wait for database to be healthy + run: docker compose -f docker-compose.ci.yml ps + + # We need git config --global -add safe.directory /usr/src/app because + # /usr/src/app is owned by the runner in GitHub Actions virtual machine whereas + # it is the root to executes git ls-files in checkHeader.sh. + # Git will deny the execution if the mounted volume (/usr/src/app) is owned by a different user. + - name: Run checks + run: | + docker compose -f docker-compose.ci.yml run --rm web \ + sh -c "\ + git config --global --add safe.directory /usr/src/app && \ + npm ci && \ + npm run check:header && \ + npm run check:typescript && \ + npm run check:types && \ + npm run check:lint \ + " + + - name: Run unit and integration tests + run: | + docker compose -f docker-compose.ci.yml run --rm web \ + sh -c "npm ci && npm test" + + - name: Tear down + if: always() + run: docker compose -f docker-compose.ci.yml down -v diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e2453f085..2b0945f27 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,16 +9,17 @@ on: jobs: run-checks-tests: env: - OED_DB_USER: test - OED_DB_PASSWORD: travisTest - OED_DB_DATABASE: travis_ci_dummy - OED_DB_TEST_DATABASE: travis_ci_test - OED_DB_HOST: postgres - OED_DB_PORT: 5432 - OED_TOKEN_SECRET: travis - OED_SERVER_PORT: 3000 - OED_TEST_SITE_READING_RATE: 00:15:00 - POSTGRES_PASSWORD: travisTest + # ${{ secrets }} is saved in Repository secrets (Repo -> Settings -> Secrets and variables -> Actions) + OED_DB_USER: ${{ secrets.OED_DB_USER }} + OED_DB_PASSWORD: ${{ secrets.OED_DB_PASSWORD }} + OED_DB_DATABASE: ${{ secrets.OED_DB_DATABASE }} + OED_DB_TEST_DATABASE: ${{ secrets.OED_DB_TEST_DATABASE }} + OED_DB_HOST: ${{ secrets.OED_DB_HOST }} + OED_DB_PORT: ${{ secrets.OED_DB_PORT }} + OED_TOKEN_SECRET: ${{ secrets.OED_TOKEN_SECRET }} + OED_SERVER_PORT: ${{ secrets.OED_SERVER_PORT }} + OED_TEST_SITE_READING_RATE: ${{ secrets.OED_TEST_SITE_READING_RATE }} + POSTGRES_PASSWORD: ${{ secrets.OED_DB_PASSWORD }} runs-on: ubuntu-latest # Make sure the node version here matches containers/web/Dockerfile for the standard OED build. @@ -28,8 +29,9 @@ jobs: postgres: image: postgres env: - POSTGRES_PASSWORD: travisTest - POSTGRES_DB: travis_ci_test + # ${{ secrets }} is saved in Repository secrets (Repo -> Settings -> Secrets and variables -> Actions) + POSTGRES_PASSWORD: ${{ secrets.OED_DB_PASSWORD }} + POSTGRES_DB: ${{ secrets.OED_DB_TEST_DATABASE }} options: >- --health-cmd pg_isready --health-interval 10s @@ -72,8 +74,9 @@ jobs: - name: Connect to PostgreSQL run: node client.js env: - POSTGRES_HOST: postgres - POSTGRES_PORT: 5432 + # ${{ secrets }} is saved in Repository secrets (Repo -> Settings -> Secrets and variables -> Actions) + POSTGRES_HOST: ${{ secrets.OED_DB_HOST }} + POSTGRES_PORT: ${{ secrets.OED_DB_PORT }} - name: node tests run: | diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml new file mode 100644 index 000000000..f64719f9b --- /dev/null +++ b/docker-compose.ci.yml @@ -0,0 +1,113 @@ +# * +# * This Source Code Form is subject to the terms of the Mozilla Public +# * License, v. 2.0. If a copy of the MPL was not distributed with this +# * file, You can obtain one at http://mozilla.org/MPL/2.0/. +# * + +version: "3.8" +services: + # Database service. It's PostgreSQL, see the + # Dockerfile in ./database + database: + build: ./containers/database/ + environment: + # Custom PGDATA per recommendations from official Docker page + PGDATA: /var/lib/postgresql/data/pgdata + POSTGRES_PASSWORD: ${OED_DB_PASSWORD} + volumes: + - ./postgres-data:/var/lib/postgresql/data/pgdata + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 10s + timeout: 10s + retries: 3 + # ports: + # - "5432:5432" + # Uncomment the above lines to enable access to the PostgreSQL server + # from the host machine. + + web: + # Configuration variables for the app. + environment: + OED_PRODUCTION: "no" + OED_SERVER_PORT: ${OED_SERVER_PORT} + OED_DB_USER: ${OED_DB_USER} + OED_DB_DATABASE: ${OED_DB_DATABASE} + OED_DB_TEST_DATABASE: ${OED_DB_TEST_DATABASE} + OED_DB_PASSWORD: ${OED_DB_PASSWORD} + OED_DB_HOST: database # Docker will set this hostname + OED_DB_PORT: ${OED_DB_PORT} + OED_TOKEN_SECRET: ${OED_TOKEN_SECRET} + OED_LOG_FILE: log.txt + OED_MAIL_METHOD: none # Method of sending mail. Supports "secure-smtp", "none". Case insensitive. + OED_MAIL_SMTP: smtp.example.com # Edit this + OED_MAIL_SMTP_PORT: 465 # Edit this + OED_MAIL_IDENT: someone@example.com # The user email that is used for sending emails (SMTP) + OED_MAIL_CREDENTIAL: credential # Set the email password for sending email here + OED_MAIL_FROM: mydomain@example.com # The email address that the email will come from + OED_MAIL_TO: someone@example.com # Set the destination address here for where to send emails + OED_MAIL_ORG: My Organization Name # Org name for mail that is included in the subject + # Changing this value does not impact what OED displays. + # What it will change is the date/time stamp on logs, notes and change dates that place the current date/time. + # It can also impact the interpretation of readings sent to OED such as Unix timestamps. + TZ: Etc/UTC # Set the timezone of the Docker container where OED runs the web services. + # If in a subdirectory, set it here + # - OED_SUBDIR=/subdir/ + # Set the correct build environment. + build: + context: ./ + dockerfile: ./containers/web/Dockerfile + # Link to the database so the app can persist data + links: + - database + # Load the source code into the container. + # Using a volume allows autorebuild to work. + volumes: + - ./:/usr/src/app + # Map the default port. + ports: + - "3000:3000" # Should be commented out if you uncomment 80:3000 below. + - "9229:9229" # Debug port, should be commented out for production + # For production you might want something like: + # - "80:3000" + # and comment out the debug port and 3000:3000 line above + # Don't bring this up without the DB + depends_on: + # - database + database: + # We need the database and it has to be ready for work (see healthcheck above). + condition: service_healthy + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:3000"] + interval: 10s + timeout: 5s + retries: 5 + # Lets docker compose up work right + # If environment variable install_args is not set then it becomes blank without warning user. + command: + [ + "bash", + "./src/scripts/installOED.sh", + "--nostart" + ] + # Use this if you are using a docker-compose that is earlier than version 2 and comment out the one above. + # command: ["bash", "./src/scripts/installOED.sh"] + + # Cypress testing service + cypress: + image: cypress/included + profiles: + - ui-testing + environment: + - CYPRESS_BASE_URL=http://web:3000 + - DISPLAY=:99 + working_dir: /usr/src/app + depends_on: + web: + condition: service_healthy + volumes: + - ./:/usr/src/app + entrypoint: > + /bin/sh -c " + rm -f /tmp/.X99-lock && + Xvfb :99 -screen 0 1024x768x16"