|
| 1 | +name: Python CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - 'app_python/**' |
| 7 | + - '.github/workflows/python-ci.yml' |
| 8 | + pull_request: |
| 9 | + paths: |
| 10 | + - 'app_python/**' |
| 11 | + - '.github/workflows/python-ci.yml' |
| 12 | + |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + working-directory: ./app_python # We set the default directory of python project |
| 16 | + |
| 17 | +jobs: |
| 18 | + |
| 19 | + lint-and-test: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + strategy: |
| 22 | + matrix: |
| 23 | + python-version: ["3.10"] |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code repository |
| 27 | + uses: actions/checkout@v2 |
| 28 | + |
| 29 | + - name: Set up Python |
| 30 | + uses: actions/setup-python@v2 |
| 31 | + with: |
| 32 | + python-version: ${{ matrix.python-version }} |
| 33 | + cache: 'pip' |
| 34 | + cache-dependency-path: | |
| 35 | + ./app_python/requirements.txt |
| 36 | +
|
| 37 | + - name: Install dependencies |
| 38 | + run: | |
| 39 | + python -m pip install --upgrade pip |
| 40 | + pip install -r requirements.txt |
| 41 | +
|
| 42 | + - name: Lint with flake8 |
| 43 | + run: | |
| 44 | + pip install flake8 |
| 45 | + flake8 . |
| 46 | +
|
| 47 | + - name: Run unit tests |
| 48 | + run: | |
| 49 | + python test_app.py |
| 50 | +
|
| 51 | + security-check: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + permissions: |
| 54 | + actions: read |
| 55 | + contents: read |
| 56 | + security-events: write |
| 57 | + |
| 58 | + steps: |
| 59 | + - name: Checkout code repository. |
| 60 | + uses: actions/checkout@v3 |
| 61 | + |
| 62 | + - name: Check for vulnerabilities using Snyk |
| 63 | + uses: snyk/actions/python-3.10@master |
| 64 | + continue-on-error: true # To make sure that SARIF upload gets called |
| 65 | + env: |
| 66 | + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} |
| 67 | + with: |
| 68 | + args: |
| 69 | + --package-manager=pip |
| 70 | + --skip-unresolved |
| 71 | + --file=app_python/requirements.txt |
| 72 | + |
| 73 | + build-and-push: |
| 74 | + needs: [lint-and-test, security-check] |
| 75 | + runs-on: ubuntu-latest |
| 76 | + |
| 77 | + steps: |
| 78 | + |
| 79 | + - name: Checkout code repository |
| 80 | + uses: actions/checkout@v2 |
| 81 | + |
| 82 | + - name: Set up Docker Buildx |
| 83 | + uses: docker/setup-buildx-action@v3 |
| 84 | + |
| 85 | + - name: Login to Docker Hub |
| 86 | + id: login |
| 87 | + uses: docker/login-action@v1 |
| 88 | + with: |
| 89 | + registry: docker.io |
| 90 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 91 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 92 | + |
| 93 | + - name: Build Docker image |
| 94 | + run: | |
| 95 | + docker buildx build -t m4k4rich/time-python-app:latest . |
| 96 | + docker buildx build --push -t m4k4rich/time-python-app:latest . |
| 97 | + working-directory: ./app_python # Change to your project directory |
| 98 | + |
| 99 | + - name: Check Docker Hub Login Status |
| 100 | + run: echo "Logged in to Docker Hub successfully." |
| 101 | + |
| 102 | + - name: Logout from Docker Hub |
| 103 | + run: docker logout |
| 104 | + |
| 105 | + |
0 commit comments