diff --git a/.github/workflows/tests/pytest.yml b/.github/workflows/tests/pytest.yml new file mode 100644 index 0000000000..86ed03a9c3 --- /dev/null +++ b/.github/workflows/tests/pytest.yml @@ -0,0 +1,68 @@ +# A generic workflow to run tests within a docker compose stack + +name: Run Tests + +on: + pull_request: + branches: + - main + - staging + - development + +jobs: + run-tests: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Vars and Secrets to Env + env: + TAG_OVERRIDE: ${{ env.TAG_OVERRIDE || 'ci-development' }} + GIT_BRANCH: ${{ github.ref_name }} + VARS_CONTEXT: ${{ toJson(vars) }} + SECRETS_CONTEXT: ${{ toJson(secrets) }} + run: | + # Random delimeter string for security + delim=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) + + # Parse JSON with multiline strings, using delimeter (Github specific) + to_envs() { jq -r "to_entries[] | \"\(.key)<<$delim\n\(.value)\n$delim\n\""; } + + # Set all vars + echo "TAG_OVERRIDE=${TAG_OVERRIDE}" >> $GITHUB_ENV + echo "GIT_BRANCH=${GIT_BRANCH}" >> $GITHUB_ENV + + # Set VARS_CONTEXT if not null + if [ "${VARS_CONTEXT}" != "null" ]; then + echo "${VARS_CONTEXT}" | to_envs >> $GITHUB_ENV + fi + + # Set SECRETS_CONTEXT if not null + if [ "${SECRETS_CONTEXT}" != "null" ]; then + echo "${SECRETS_CONTEXT}" | to_envs >> $GITHUB_ENV + fi + + - name: Create .env file + run: | + # Get a8m/envsubst (required for default vals syntax ${VAR:-default}) + echo "Downloading envsubst" + curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst + chmod +x envsubst + + # Check if .env.example exists + if [ -f .env.example ]; then + echo "Substituting variables from .env.example --> .env" + ./envsubst < .env.example > .env + else + echo ".env.example not found, creating .env with GIT_BRANCH only" + echo "GIT_BRANCH=${GIT_BRANCH}" > .env + fi + + echo "GIT_BRANCH=${GIT_BRANCH}" >> .env + + - name: Run Tests + run: | + docker compose up -d proxy + docker compose run api pytest diff --git a/.github/workflows/tests/test_ci.sh b/.github/workflows/tests/test_ci.sh index 60a319231d..aaad26820b 100644 --- a/.github/workflows/tests/test_ci.sh +++ b/.github/workflows/tests/test_ci.sh @@ -11,11 +11,18 @@ set -e # GITHUB_TOKEN=input # Feed to act using -s flag: -s GITHUB_TOKEN=input_personal_access_token -# PR Test Backend -act pull_request -W .github/workflows/pr_test_backend.yml \ +# Run backend PyTest manually +docker compose build api +act pull_request -W .github/workflows/tests/pytest.yml \ -e .github/workflows/tests/pr_payload.json \ --var-file=.env --secret-file=.env +# # PR Test Backend +# Includes image build, which fails due to registry auth +# act pull_request -W .github/workflows/pr_test_backend.yml \ +# -e .github/workflows/tests/pr_payload.json \ +# --var-file=.env --secret-file=.env + # PR Test Frontend act pull_request -W .github/workflows/pr_test_frontend.yml \ -e .github/workflows/tests/pr_payload.json \