diff --git a/.github/workflows/_unit-test.yml b/.github/workflows/_unit-test.yml new file mode 100644 index 000000000..c4c4a62c1 --- /dev/null +++ b/.github/workflows/_unit-test.yml @@ -0,0 +1,76 @@ +name: Run unit tests + +on: + workflow_call: + inputs: + command: + required: true + type: string + image: + required: true + type: string + environment: + type: string + +jobs: + vars: + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + outputs: + token: ${{ steps.vars.outputs.token }} + environment: + name: ${{ inputs.environment }} + steps: + - name: GCP Auth + id: auth + uses: google-github-actions/auth@v2 + with: + token_format: 'access_token' + workload_identity_provider: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }} + service_account: ${{ vars.SERVICE_ACCOUNT }} + - id: vars + run: | + echo "token=$(gcloud auth print-access-token)" >> $GITHUB_OUTPUT + unit-tests: + runs-on: ubuntu-latest + needs: [vars] + container: + image: ${{ inputs.image }} + credentials: + username: oauth2accesstoken + password: ${{ needs.vars.outputs.token }} + services: + postgis: + image: postgis/postgis:16-3.4-alpine + env: + POSTGRES_USER: testuser + POSTGRES_PASSWORD: fancypassword + POSTGRES_DB: testdb + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: + - name: Setup + run: | + cd /var/www/app + python3 -m pip install -r requirements-dev.txt + + - name: Run Tests + run: | + cd /var/www/app + ${{ inputs.command }} + # export PYTHONPATH=/var/www/app:$PYTHONPATH + env: + DB_HOST: postgis + DB_NAME: testdb + DB_USER: testuser + DB_PASSWORD: fancypassword + DB_PORT: 5432 + FQDN: testing.pamdas.org + GCP_ENVIRONMENT_ENABLED: false + TRACING_ENABLED: false + CELERY_TASK_ALWAYS_EAGER: true \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 94a57689c..ad3a70473 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - gundi-3054 # remove this line before merge - 'release-**' jobs: @@ -26,6 +27,15 @@ jobs: repository: ${{ needs.vars.outputs.repository }} tag: ${{ needs.vars.outputs.tag }} + unit-test: + uses: ./.github/workflows/_unit-test.yml + needs: [vars, build] + with: + environment: stage + image: "${{ needs.vars.outputs.repository }}:${{ needs.vars.outputs.tag }}" + command: | + pytest + deploy_dev: uses: PADAS/gundi-workflows/.github/workflows/deploy_k8s.yml@v2 if: startsWith(github.ref, 'refs/heads/main') diff --git a/docker/Dockerfile b/docker/Dockerfile index ddd93c015..c5a2572ae 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,7 +1,16 @@ FROM ubuntu:20.04 MAINTAINER chrisdo@vulcan.com -# use apt-get update and install in the same RUN command: http://lenguyenthedat.com/docker-cache/ +EXPOSE 8000 +ENV LC_ALL C.UTF-8 +ENV LANG C.UTF-8 +ENV LANGUAGE C.UTF-8 +ENV DJANGO_SETTINGS_MODULE=cdip_admin.settings +ENV DEBUG=False +ENV SERVICE_NAME=cdip_portal +ENV DB_HOST=host.docker.internal +ENV DJANGO_SILK_ENABLED=True + RUN apt-get update && \ apt-get -y upgrade && \ DEBIAN_FRONTEND=noninteractive \ @@ -18,52 +27,16 @@ RUN apt-get update && \ software-properties-common \ postgresql-client-12 \ vim \ - && apt-get autoremove - - -# Use ubuntugis/ppa for bionic since it's not available for 20.04 (focal) yet. -RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 089EBE08314DF160 -RUN /bin/sh -c 'echo "deb http://ppa.launchpad.net/ubuntugis/ppa/ubuntu bionic main" >> /etc/apt/sources.list.d/ubuntugis.list' - -#RUN apt-get update -RUN apt-get update && \ - apt-get -y install gdal-bin libgdal-dev -RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal -RUN export C_INCLUDE_PATH=/usr/include/gdal -RUN pip3 install GDAL==$(gdal-config --version) - -WORKDIR /workspace - -ADD dependencies/requirements.txt /workspace/requirements.txt - -RUN pip3 install -U "pip" - -RUN pip3 install -r /workspace/requirements.txt --no-cache-dir - -ARG built_version="" - -ADD ./cdip_admin /var/www/app -ADD ./swagger-fix/index.html /usr/local/lib/python3.8/dist-packages/rest_framework_swagger/templates/rest_framework_swagger/ - -EXPOSE 8000 - -ENV LC_ALL C.UTF-8 -ENV LANG C.UTF-8 -ENV LANGUAGE C.UTF-8 - -ENV BUILT_VERSION=${built_version} - -ENV DJANGO_SETTINGS_MODULE=cdip_admin.settings -ENV DEBUG=False - -# Default service name -ENV SERVICE_NAME=cdip_portal - -# if connecting to db running on localhost -ENV DB_HOST=host.docker.internal + gdal-bin libgdal-dev && \ + apt-get autoremove && \ + pip3 install -U "pip" WORKDIR /var/www/app +COPY dependencies . +RUN pip3 install -r requirements.txt --no-cache-dir +COPY ./cdip_admin . +COPY ./swagger-fix/index.html /usr/local/lib/python3.8/dist-packages/rest_framework_swagger/templates/rest_framework_swagger/ RUN mkdir -p /var/www/static # Collect static files (using a stub database) @@ -71,4 +44,6 @@ ENV DJANGO_SILK_ENABLED=True RUN python3 manage.py collectstatic --noinput --settings=cdip_admin.local_settings_nodb ENV DJANGO_SILK_ENABLED=False +RUN mkdir -p /var/www/static && \ + python3 manage.py collectstatic --noinput --settings=cdip_admin.local_settings_nodb CMD ["/var/www/app/start_scripts/start.sh"]