|
| 1 | +env: |
| 2 | + REGISTRY: "registry.digitalocean.com/your_registry" |
| 3 | + IMAGE_NAME: "image_name" |
| 4 | + |
| 5 | + COMMAND: "python main.py" |
| 6 | + |
| 7 | +jobs: |
| 8 | + test: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + - name: Checkout master |
| 13 | + uses: actions/checkout@v2 |
| 14 | + |
| 15 | + - name: Set up Python 3.8 |
| 16 | + uses: actions/setup-python@v2 |
| 17 | + with: |
| 18 | + python-version: 3.8 |
| 19 | + |
| 20 | + - name: Install dependencies with pipenv |
| 21 | + run: | |
| 22 | + python -m pip install --upgrade pip |
| 23 | + pip install pipenv |
| 24 | + if [ -f Pipfile ]; then pipenv install --deploy --dev; fi |
| 25 | + |
| 26 | + - name: Lint with flake8 |
| 27 | + run: | |
| 28 | + # stop the build if there are Python syntax errors or undefined names |
| 29 | + pipenv run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics |
| 30 | + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide |
| 31 | + pipenv run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics |
| 32 | + |
| 33 | + - name: Test with pytest |
| 34 | + run: | |
| 35 | + pipenv run pytest . |
| 36 | + build_and_push: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + if: github.event_name == 'push' |
| 39 | + needs: test |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout master |
| 43 | + uses: actions/checkout@v2 |
| 44 | + |
| 45 | + - name: Build container image |
| 46 | + run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) . |
| 47 | + |
| 48 | + - name: Install doctl |
| 49 | + uses: digitalocean/action-doctl@v2 |
| 50 | + with: |
| 51 | + token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} |
| 52 | + |
| 53 | + - name: Log in to DigitalOcean Container Registry with short-lived credentials |
| 54 | + run: doctl registry login --expiry-seconds 600 |
| 55 | + |
| 56 | + - name: Push image to DigitalOcean Container Registry |
| 57 | + run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) |
| 58 | + |
| 59 | + deploy: |
| 60 | + runs-on: ubuntu-latest |
| 61 | + if: github.event_name == 'push' |
| 62 | + needs: build_and_push |
| 63 | + |
| 64 | + steps: |
| 65 | + - name: Deploy to Digital Ocean droplet via SSH action |
| 66 | + |
| 67 | + with: |
| 68 | + HOST: ${{ secrets.HOST }} |
| 69 | + USERNAME: ${{ secrets.USERNAME }} |
| 70 | + KEY: ${{ secrets.SSHKEY }} |
| 71 | + envs: IMAGE_NAME,REGISTRY,GITHUB_SHA,COMMAND |
| 72 | + script: | |
| 73 | + # Stop running container |
| 74 | + docker stop $(echo $IMAGE_NAME) |
| 75 | + |
| 76 | + # Remove old container |
| 77 | + docker rm $(echo $IMAGE_NAME) |
| 78 | + |
| 79 | + # Run a new container from a new image |
| 80 | + docker run -d \ |
| 81 | + --restart always \ |
| 82 | + --env-file .env \ |
| 83 | + -p 8000:8000 \ |
| 84 | + --name $(echo $IMAGE_NAME) \ |
| 85 | + $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) $(echo $COMMAND) |
0 commit comments