Skip to content

Catch errors thrown during tests (#141) #235

Catch errors thrown during tests (#141)

Catch errors thrown during tests (#141) #235

Workflow file for this run

name: test
on:
pull_request: { types: [opened, reopened, synchronize, ready_for_review] }
push: { branches: [main] }
workflow_call:
outputs:
cache_key:
value: ${{ jobs.cache-toolbox.outputs.cache_key }}
env:
SWIFT_IMAGE: 'swift:6.0-jammy'
jobs:
# Check if a build of most recent release of the toolbox for the runner's OS and arch is cached, build and cache it if not
# Run for both PRs and pushes to main so every individual PR doesn't have to rebuild the toolbox at least once
# Use a concurrency group with no cancellation to avoid redundant builds; queued jobs will see the cache when they get their turn
cache-toolbox:
if: ${{ !(github.event.pull_request.draft || false) }}
concurrency:
group: cache_toolbox_for_template
cancel-in-progress: false
outputs:
cache_key: ${{ env.SWIFT_IMAGE }}-vapor-toolbox-${{ steps.latest.outputs.tag }}
runs-on: ubuntu-latest
steps:
- name: Get latest toolbox release
id: latest
run: 'echo "tag=$(curl -fsSL "https://api.github.com/repos/vapor/toolbox/releases/latest" | jq -r .tag_name)" >>"${GITHUB_OUTPUT}"'
- name: Check cache
id: check
uses: actions/cache@v4
with:
key: ${{ env.SWIFT_IMAGE }}-vapor-toolbox-${{ steps.latest.outputs.tag }}
path: toolbox
lookup-only: true
- name: Check out latest toolbox
if: ${{ !steps.check.outputs.cache-hit }}
uses: actions/checkout@v4
with:
repository: vapor/toolbox
ref: ${{ steps.latest.outputs.tag }}
- name: Build and cache toolbox
if: ${{ !steps.check.outputs.cache-hit }}
run: |
docker run --rm -v "$(pwd):/toolbox-build" -w /toolbox-build "${SWIFT_IMAGE}" bash -c \
'swift build -c release --static-swift-stdlib --product vapor && mkdir toolbox && cp .build/release/vapor toolbox/vapor'
# Run vapor new and test the template for all combinations of options
# Only run for PRs, running on push to main would be redundant.
# Use normal "only one of this workflow per branch at a time, cancel stale jobs" concurrency
test-new-and-build:
if: ${{ github.event_name == 'pull_request' && !github.event.pull_request.draft }}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ toJSON(matrix) }}
cancel-in-progress: true
strategy:
fail-fast: false
matrix:
swift-image:
- 'swift:6.0-jammy'
fluentflags:
- '--no-fluent'
#- '--fluent.db mysql' # The MySQL image can't be configured usably via GH Actions at this time
- '--fluent.db postgres'
- '--fluent.db sqlite'
- '--fluent.db mongo'
leafflags:
- '--leaf'
- '--no-leaf'
include:
#- fluentflags: '--fluent.db mysql'
# dbhostname: mysql
- fluentflags: '--fluent.db postgres'
dbhostname: psql
- fluentflags: '--fluent.db mongo'
dbhosturl: 'mongodb://mongo:27017/vapor_database'
runs-on: ubuntu-latest
container: ${{ matrix.swift-image }}
needs: cache-toolbox
services:
mongo: { image: 'mongo:latest' }
#mysql:
# image: mysql:latest
# env: { MYSQL_ALLOW_EMPTY_PASSWORD: 'true', MYSQL_USER: vapor_username, MYSQL_PASSWORD: vapor_password, MYSQL_DATABASE: vapor_database }
psql:
image: postgres:latest
env: { POSTGRES_USER: vapor_username, POSTGRES_DB: vapor_database, POSTGRES_PASSWORD: vapor_password,
POSTGRES_HOST_AUTH_METHOD: 'scram-sha-256', POSTGRES_INITDB_ARGS: '--auth-host=scram-sha-256' }
steps:
- name: Get cached toolbox
id: get-cache
uses: actions/cache/restore@v4
with:
key: ${{ needs.cache-toolbox.outputs.cache_key }}
path: toolbox
fail-on-cache-miss: false
- name: Build toolbox inside container if cache fails
if: ${{ steps.get-cache.cache_hit != 'true' }}
run: |
apt-get update && apt-get install -y curl jq
tag="$(curl -fsSL "https://api.github.com/repos/vapor/toolbox/releases/latest" | jq -r .tag_name)"
git clone https://github.com/vapor/toolbox.git -b "${tag}" toolbox
swift build --package-path toolbox -c debug --product vapor
cp toolbox/.build/debug/vapor toolbox/vapor
- name: Generate a project from the template
env:
HEAD_REF: ${{ github.head_ref }}
CLONE_URL: ${{ github.event.pull_request.head.repo.clone_url }}
run: |
toolbox/vapor new template-test \
--template "${CLONE_URL}" --branch "${HEAD_REF}" \
--no-commit -o template-test \
${{ matrix.fluentflags }} ${{ matrix.leafflags }}
- name: Build and test template
run: swift test --package-path template-test
env:
DATABASE_HOST: ${{ matrix.dbhostname }}
DATABASE_URL: ${{ matrix.dbhosturl }}
# Run vapor new and build the container with Docker Compose for all combinations of options
# Only run for PRs, running on push to main would be redundant.
# Use normal "only one of this workflow per branch at a time, cancel stale jobs" concurrency
test-new-and-container:
if: ${{ github.event_name == 'pull_request' && !github.event.pull_request.draft }}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ toJSON(matrix) }}
cancel-in-progress: true
strategy:
fail-fast: false
matrix:
fluentflags:
- '--no-fluent'
- '--fluent.db mysql'
- '--fluent.db postgres'
- '--fluent.db sqlite'
- '--fluent.db mongo'
leafflags:
- '--leaf'
- '--no-leaf'
needs: cache-toolbox
runs-on: ubuntu-24.04
steps:
- name: Get cached toolbox
uses: actions/cache/restore@v4
with:
key: ${{ needs.cache-toolbox.outputs.cache_key }}
path: toolbox
fail-on-cache-miss: true
- name: Generate a project from the template
env:
HEAD_REF: ${{ github.head_ref }}
CLONE_URL: ${{ github.event.pull_request.head.repo.clone_url }}
run: |
toolbox/vapor new template-test \
--template "${CLONE_URL}" --branch "${HEAD_REF}" \
--no-commit -o template-test \
${{ matrix.fluentflags }} ${{ matrix.leafflags }}
- name: Build Docker container
run: docker compose --project-directory template-test build