|
| 1 | +name: CI-CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + |
| 9 | +jobs: |
| 10 | + lint_and_test: |
| 11 | + name: Lint & Test |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Python 3.12 |
| 18 | + uses: actions/setup-python@v5 |
| 19 | + with: |
| 20 | + python-version: "3.12" |
| 21 | + |
| 22 | + - name: Cache pip |
| 23 | + uses: actions/cache@v4 |
| 24 | + with: |
| 25 | + path: ~/.cache/pip |
| 26 | + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} |
| 27 | + restore-keys: | |
| 28 | + ${{ runner.os }}-pip- |
| 29 | +
|
| 30 | + - name: Install dependencies |
| 31 | + run: | |
| 32 | + pip install --upgrade pip |
| 33 | + pip install -r requirements.txt |
| 34 | + pip install -r dev-requirements.txt || true |
| 35 | +
|
| 36 | + - name: Lint |
| 37 | + run: | |
| 38 | + flake8 bot tests |
| 39 | + black --check . |
| 40 | + isort --check-only . |
| 41 | +
|
| 42 | + - name: Test |
| 43 | + env: |
| 44 | + BOT_TOKEN: ${{ secrets.BOT_TOKEN }} |
| 45 | + TEST_MODE: 1 |
| 46 | + run: | |
| 47 | + pytest |
| 48 | +
|
| 49 | + build_and_test: |
| 50 | + needs: lint_and_test |
| 51 | + name: Build Docker Image |
| 52 | + runs-on: ubuntu-latest |
| 53 | + outputs: |
| 54 | + image-tag: ${{ steps.build.outputs.tag }} |
| 55 | + |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + |
| 59 | + - name: Build Docker image |
| 60 | + id: build |
| 61 | + run: | |
| 62 | + IMAGE=ghcr.io/${{ github.repository_owner }}/mybot |
| 63 | + TAG=${{ github.sha }} |
| 64 | + docker build -t $IMAGE:$TAG -f docker/Dockerfile . |
| 65 | + echo "::set-output name=tag::$TAG" |
| 66 | +
|
| 67 | + - name: Login to GHCR |
| 68 | + uses: docker/login-action@v3 |
| 69 | + with: |
| 70 | + registry: ghcr.io |
| 71 | + username: ${{ github.repository_owner }} |
| 72 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + |
| 74 | + - name: Push Docker image |
| 75 | + run: | |
| 76 | + IMAGE=ghcr.io/${{ github.repository_owner }}/mybot |
| 77 | + TAG=${{ steps.build.outputs.tag }} |
| 78 | + docker push $IMAGE:$TAG |
| 79 | +
|
| 80 | + deploy_k8s: |
| 81 | + needs: build_and_test |
| 82 | + name: Deploy to Kubernetes |
| 83 | + runs-on: ubuntu-latest |
| 84 | + environment: prod |
| 85 | + steps: |
| 86 | + - uses: actions/checkout@v4 |
| 87 | + |
| 88 | + - name: Set up KUBECONFIG |
| 89 | + env: |
| 90 | + KUBECONFIG: ${{ secrets.KUBECONFIG_BASE64 }} |
| 91 | + run: | |
| 92 | + echo "$KUBECONFIG" | base64 -d > ~/.kube/config |
| 93 | +
|
| 94 | + - name: Deploy to cluster |
| 95 | + run: | |
| 96 | + kubectl set image deploy/mybot-deploy mybot=ghcr.io/${{ github.repository_owner }}/mybot:${{ needs.build_and_test.outputs.image-tag }} |
| 97 | + kubectl rollout status deploy/mybot-deploy |
| 98 | +
|
| 99 | + deploy_ssh: |
| 100 | + needs: build_and_test |
| 101 | + name: Deploy via SSH |
| 102 | + runs-on: ubuntu-latest |
| 103 | + steps: |
| 104 | + - uses: actions/checkout@v4 |
| 105 | + |
| 106 | + - name: SSH deployment |
| 107 | + uses: appleboy/ssh-action@v0.1.10 |
| 108 | + with: |
| 109 | + host: ${{ secrets.SSH_HOST }} |
| 110 | + username: ${{ secrets.SSH_USER }} |
| 111 | + key: ${{ secrets.SSH_KEY }} |
| 112 | + script: | |
| 113 | + cd /opt/mybot |
| 114 | + git pull |
| 115 | + docker-compose up -d |
0 commit comments