Skip to content

Commit 8096f28

Browse files
🚀 Webapp docker service and testing using composite actions (#1)
* first commit * make shell file executable * remove js * make shell exec * add app health check * add codeowners and update readme * merge docker up and docker down * fix if condition * move scripts in one folder * fix typo * add logic for multiple server * change container name * add comments * fix name * fix typos * add comments * fix typo * fix branch name * add names for steps * add redoc * enable create dir * fix typo * remove redoc part * add work-dir * increase sleep time to avoid failing * turn off health check for now * add wor-dir for testing * print env * improve instructions * add envs * remove default for work dir * make work dir optional * add openapi * replace _ with - * fix mkdir * fix syntax * fix typo * print clean branch name * debug underscore * revert - * turn on checks for now * remove app part from ls * add / for mnt * remove app name * enable testing * add fast api parameter * add debug * update docs * fix spacing * change version * finish formatting * fix typo * add port as an input * add port * add validatecodeonce for multiple * remove port and add openapi in multiple * debug openapi link * remove debugs * add flake8 back * improve text * remove unwanted text * always run docker dowm * change description to reflect repo name * add container name * merge single and multiple code * fix syntax * add an echo to debug * remove extra parameters * add proper reports * Revert changes due to this [error](https://github.com/SatelCreative/sb-pim/actions/runs/4886470560/jobs/8739880820)
1 parent 74a1c30 commit 8096f28

File tree

7 files changed

+155
-2
lines changed

7 files changed

+155
-2
lines changed

‎.gitignore

Whitespace-only changes.

‎CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @rahulpatidar0191

‎README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1-
# satel-webapp-testing
2-
Centralized GitHub actions to test a webapp
1+
# Satel Python Docker Tests
2+
This centralized GitHub action runs tests on a python app using docker services
3+
4+
## Usage
5+
```yml
6+
name: Run tests
7+
on:
8+
<conditions-you-want-this-workflow-to-run-on>
9+
10+
jobs:
11+
code-validation:
12+
# HOST-NAME is self-hosted or the name of server where the action runner is hosted, cosmicray for example
13+
runs-on: <HOST-NAME>
14+
steps:
15+
- name: Validate code
16+
uses: SatelCreative/[email protected]
17+
with:
18+
# APP-NAME can be st-pim or sb-pim for example
19+
app-name: <APP-NAME>
20+
registry: <REGISTRY-NAME>
21+
clean-branch-name: <CLEAN-BRANCH-NAME>
22+
# WORK-DIR, where all the docker related files are located, optional field, default is root
23+
work-dir: <WORK-DIR>
24+
fastapi-parameter: </PARAMETER>
25+
container-name: <NAME_OF-WEBAPP-CONTAINER>
26+
27+
28+
```

‎action.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Validate code using docker
2+
description: Run docker services, test them and then remove them
3+
inputs:
4+
app-name:
5+
description: Name of the Webapp
6+
required: false
7+
registry:
8+
description: Provides current name of the registry
9+
required: true
10+
clean-branch-name:
11+
description: Branch name that was pushed to registry
12+
required: true
13+
work-dir:
14+
description: Location of all the docker files
15+
required: false
16+
fastapi-parameter:
17+
description: Extra URL parameter for openapi
18+
required: false
19+
container-name:
20+
description: Name of the docker container for webapp
21+
required: false
22+
default: webapp
23+
24+
runs:
25+
using: "composite"
26+
steps:
27+
- name: Docker Up
28+
env:
29+
APP_NAME: ${{ inputs.app-name }}
30+
REGISTRY: ${{ inputs.registry }}
31+
CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }}
32+
WORK_DIR: ${{ inputs.work-dir }}
33+
run: ${{ github.action_path }}/scripts/DockerUp.sh
34+
shell: bash
35+
36+
- name: Run docker tests
37+
env:
38+
APP_NAME: ${{ inputs.app-name }}
39+
CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }}
40+
WORK_DIR: ${{ inputs.work-dir }}
41+
FAST_PARA: ${{ inputs.fastapi-parameter }}
42+
CONTAINER_NAME: ${{ inputs.container-name }}
43+
run: ${{ github.action_path }}/scripts/ValidateCode.sh
44+
shell: bash
45+
46+
- name: Docker Down
47+
if: ${{ always() }}
48+
env:
49+
APP_NAME: ${{ inputs.app-name }}
50+
REGISTRY: ${{ inputs.registry }}
51+
CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }}
52+
WORK_DIR: ${{ inputs.work-dir }}
53+
run: ${{ github.action_path }}/scripts/DockerDown.sh
54+
shell: bash

‎scripts/DockerDown.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Set registry and clean branch name as global variables
4+
export REGISTRY=$REGISTRY
5+
export CLEAN_BRANCH_NAME=$CLEAN_BRANCH_NAME
6+
7+
if [[ -n $WORK_DIR ]]
8+
then
9+
echo "WORK_DIR ${WORK_DIR}"
10+
cd $WORK_DIR
11+
fi
12+
13+
echo "Docker down"
14+
docker-compose -f docker-compose.yml -f docker-compose.pipeline.yml down

‎scripts/DockerUp.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
# Set registry and clean branch name as global variables
4+
export REGISTRY=$REGISTRY
5+
export CLEAN_BRANCH_NAME=$CLEAN_BRANCH_NAME
6+
7+
if [[ -n $WORK_DIR ]]
8+
then
9+
echo "WORK_DIR ${WORK_DIR}"
10+
cd $WORK_DIR
11+
fi
12+
echo "Dev image check"
13+
IMG_STR=`cat docker-compose.override.yml | grep 'devenv'| cut -d ":" -f 2-3`
14+
IMG_LIST=( $IMG_STR ) #convert string into an array
15+
for IMG in "${IMG_LIST[@]}"
16+
do
17+
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect ${IMG} > /dev/null || exit 1 # Check to see if the dev image is on registry or not
18+
done
19+
20+
echo "Docker up"
21+
docker-compose -f docker-compose.yml -f docker-compose.pipeline.yml up -d

‎scripts/ValidateCode.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
if [[ -n $WORK_DIR ]]
4+
then
5+
echo "WORK_DIR ${WORK_DIR}"
6+
cd $WORK_DIR
7+
fi
8+
9+
echo "App health check" # Check to see if the app container is running or not
10+
sleep 5
11+
docker-compose exec -T ${CONTAINER_NAME} python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
12+
13+
echo "Openapi link" # copy openapi files to samba mount
14+
docker-compose exec -T ${CONTAINER_NAME} python -c "import requests; f=open('${CLEAN_BRANCH_NAME}_openapi.json','w',encoding = 'utf-8'); f.write(requests.get('http://localhost:8000${FAST_PARA}/openapi.json').text);f.close()"
15+
[ -d "/mnt/samba/${APP_NAME}" ] || mkdir -p "/mnt/samba/${APP_NAME}"
16+
docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":"/python/app/${CLEAN_BRANCH_NAME}_openapi.json" "/mnt/samba/${APP_NAME}/${CLEAN_BRANCH_NAME}_openapi.json"
17+
18+
echo "Clean up old reports"
19+
rm -f unittesting.xml coverage.xml typing.xml typing-server.xml typing-integrations.xml
20+
21+
echo "Code tests"
22+
## Catch the exit codes so we don't exit the whole script before we are done.
23+
## Typing, linting, formatting check & unit and integration testing
24+
if [[ $CONTAINER_NAME != "webapp" ]]; then
25+
echo "flake8 tests" #TODO: remove the if else block once the errors on sb-pim are fixed
26+
docker-compose exec -T ${CONTAINER_NAME} flake8; STATUS1=$? # For Sb-pim only
27+
28+
else
29+
docker-compose exec -T ${CONTAINER_NAME} validatecodeonce; STATUS1=$?
30+
docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/typing.xml typing.xml
31+
docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/unittesting.xml unittesting.xml
32+
docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/coverage.xml coverage.xml
33+
fi
34+
35+
## Return the status code
36+
TOTAL=$((STATUS1))
37+
exit $TOTAL

0 commit comments

Comments
 (0)