working on ci/cd #1
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: Django CI | |
| on: [push, pull_request] | |
| jobs: | |
| lint: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.10" | |
| - name: Install flake8 | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install flake8 | |
| - name: Run flake8 | |
| run: flake8 . | |
| test: | |
| needs: lint | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.10" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install poetry | |
| poetry config virtualenvs.create false | |
| poetry install --no-root | |
| - name: Run Redis | |
| run: | | |
| sudo apt-get install redis-server | |
| sudo systemctl start redis-server | |
| env: | |
| REDIS_HOST: ${{ secrets.REDIS_HOST }} | |
| - name: Create .env file | |
| run: | | |
| echo '"${{ secrets.ENV_FILE }}"' | sed "s/\"//g" > .env | |
| echo "TAG=${{ github.sha }}" >> .env | |
| - name: Run tests | |
| run: python manage.py test | |
| build: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Log in to Docker Hub | |
| run: echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin | |
| - name: Build Docker image | |
| run: docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/habits_api:${{ github.sha }} . | |
| - name: Push Docker image to Docker Hub | |
| run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/habits_api:${{ github.sha }} | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Set up SSH | |
| uses: webfactory/ssh-agent@v0.9.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_KEY }} | |
| - name: Deploy to Server | |
| run: | | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF' | |
| docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/habits_api:${{ github.sha }} | |
| docker stop habits_api || true | |
| docker rm habits_api || true | |
| docker run -d --name habits_api --env-file .env -p 80:8000 ${{ secrets.DOCKER_HUB_USERNAME }}/habits_api:${{ github.sha }} | |
| EOF |