Merge pull request #125 from nizarfadlan/fix/search-speaker #251
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD Pipeline | |
| on: | |
| push: | |
| branches: [main, dev] | |
| pull_request: | |
| workflow_dispatch: {} | |
| jobs: | |
| format-lint-test: | |
| name: Format, Lint and Test | |
| runs-on: ubuntu-latest | |
| services: | |
| postgres: | |
| image: postgres | |
| env: | |
| POSTGRES_PASSWORD: postgres | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: | |
| - 5432:5432 | |
| strategy: | |
| matrix: | |
| python-version: ["3.12"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - name: Display Python version | |
| run: python -c "import sys; print(sys.version)" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Check code formatting with Ruff | |
| run: ruff format --check | |
| - name: Lint code with Ruff | |
| run: ruff check --exit-non-zero-on-fix | |
| - name: Test with pytest | |
| env: | |
| ENVIRONMENT: os | |
| CORS_ALLOWED_ORIGINS: http://127.0.0.1/,https://www.google.com/,https://fastapi.tiangolo.com/ | |
| SECRET_KEY: githubtest | |
| ALGORITHM: HS256 | |
| ACCESS_TOKEN_EXPIRE_MINUTES: 1440 | |
| REFRESH_TOKEN_EXPIRE_MINUTES: 2880 | |
| TZ: Asia/Jakarta | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5432 | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DATABASE: postgres | |
| POSTGRES_USER: postgres | |
| run: | | |
| alembic upgrade head | |
| pytest . -n auto | |
| docker-build: | |
| name: Build Docker Image | |
| needs: format-lint-test | |
| if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| env: | |
| REGISTRY: ghcr.io | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Read version | |
| id: version | |
| run: echo "version=$(cat version.txt)" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| run: echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u "${{ secrets.GHCR_USERNAME }}" --password-stdin | |
| - name: Generate .env file (dev) | |
| if: ${{ github.ref_name == 'dev' }} | |
| run: printf "%s" "${{ secrets.DEV_ENV_FILE }}" > .env | |
| - name: Generate .env file (prod) | |
| if: ${{ github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v') }} | |
| run: printf "%s" "${{ secrets.PROD_ENV_FILE }}" > .env | |
| - name: Load IMAGE_NAME from .env | |
| run: | | |
| IMAGE_NAME=$(grep '^IMAGE_NAME=' .env | cut -d'=' -f2) | |
| echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV | |
| - name: Load GHCR_USERNAME from .env | |
| run: | | |
| GHCR_USERNAME=$(grep '^GHCR_USERNAME=' .env | cut -d'=' -f2) | |
| echo "GHCR_USERNAME=$GHCR_USERNAME" >> $GITHUB_ENV | |
| - name: Build Docker image | |
| run: | | |
| docker build \ | |
| -t ghcr.io/${{ env.GHCR_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }} \ | |
| -t ghcr.io/${{ env.GHCR_USERNAME }}/${{ env.IMAGE_NAME }}:latest \ | |
| . | |
| - name: Push Docker image | |
| run: | | |
| docker push ghcr.io/${{ env.GHCR_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }} | |
| docker push ghcr.io/${{ env.GHCR_USERNAME }}/${{ env.IMAGE_NAME }}:latest | |
| - name: Logout Docker | |
| run: docker logout ghcr.io | |
| deploy-dev: | |
| name: Deploy to Development | |
| needs: [docker-build] | |
| if: ${{ github.ref_name == 'dev' }} | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: development | |
| url: https://dev-api.pycon.id | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Read version | |
| id: version | |
| run: echo "version=$(cat version.txt)" >> $GITHUB_OUTPUT | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.7.0 | |
| with: | |
| ssh-private-key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} | |
| - name: Setup known_hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEV_SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts | |
| - name: Copy docker-compose.yaml and version.txt to server | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.DEV_SSH_HOST }} | |
| username: ${{ secrets.DEV_SSH_USER }} | |
| port: ${{ secrets.DEV_SSH_PORT || '22' }} | |
| key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} | |
| source: "docker-compose.yaml,version.txt" | |
| target: /home/${{ secrets.DEV_SSH_USER }}/pyconid25-be-dev/ | |
| overwrite: true | |
| - name: SSH - Generate .env file dari secret | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.DEV_SSH_HOST }} | |
| username: ${{ secrets.DEV_SSH_USER }} | |
| port: ${{ secrets.DEV_SSH_PORT || '22' }} | |
| key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} | |
| script: | | |
| cd ~/pyconid25-be-dev | |
| chmod 644 .env | |
| # Update IMAGE_TAG di .env sesuai version.txt | |
| VERSION=$(cat version.txt) | |
| if grep -q '^IMAGE_TAG=' .env; then | |
| sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=$VERSION/" .env | |
| else | |
| echo "IMAGE_TAG=$VERSION" >> .env | |
| fi | |
| # Login ke GHCR pakai akun premium | |
| echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u "${{ secrets.GHCR_USERNAME }}" --password-stdin | |
| # Pull dan deploy | |
| docker compose --env-file .env pull | |
| docker compose --env-file .env up -d | |
| deploy-prod: | |
| name: Deploy to Production | |
| needs: [docker-build] | |
| if: ${{ github.ref_name == 'main' || startsWith(github.ref, 'refs/tags/v') }} | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: production | |
| url: https://api.pycon.id | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Read version | |
| id: version | |
| run: echo "version=$(cat version.txt)" >> $GITHUB_OUTPUT | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.7.0 | |
| with: | |
| ssh-private-key: ${{ secrets.PROD_SSH_PRIVATE_KEY }} | |
| - name: Setup known_hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.PROD_SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts | |
| - name: Copy docker-compose.yaml and version.txt to server | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.PROD_SSH_HOST }} | |
| username: ${{ secrets.PROD_SSH_USER }} | |
| port: ${{ secrets.PROD_SSH_PORT || '22' }} | |
| key: ${{ secrets.PROD_SSH_PRIVATE_KEY }} | |
| source: "docker-compose.yaml,version.txt" | |
| target: /home/${{ secrets.PROD_SSH_USER }}/pyconid25-be-prod/ | |
| overwrite: true | |
| - name: SSH - Generate .env file dari secret | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.PROD_SSH_HOST }} | |
| username: ${{ secrets.PROD_SSH_USER }} | |
| port: ${{ secrets.PROD_SSH_PORT || '22' }} | |
| key: ${{ secrets.PROD_SSH_PRIVATE_KEY }} | |
| script: | | |
| cd ~/pyconid25-be-prod | |
| chmod 644 .env | |
| # Update IMAGE_TAG di .env sesuai version.txt | |
| VERSION=$(cat version.txt) | |
| if grep -q '^IMAGE_TAG=' .env; then | |
| sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=$VERSION/" .env | |
| else | |
| echo "IMAGE_TAG=$VERSION" >> .env | |
| fi | |
| # Login ke GHCR pakai akun premium | |
| echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u "${{ secrets.GHCR_USERNAME }}" --password-stdin | |
| # Pull dan deploy | |
| docker compose --env-file .env pull | |
| docker compose --env-file .env up -d |