Ahebwe  Oscar

Ahebwe Oscar

1633552260

How to Deploy Django to DigitalOcean with Docker and GitHub Actions

LearnHow to Deploy Django to DigitalOcean with Docker and GitHub Actions

By the end of this tutorial, you will be able to:

  1. Deploy Django to DigitalOcean with Docker
  2. Configure GitHub actions to continuously deploy Django to DigitalOcean
  3. Use GitHub Packages to store Docker Images
  4. Set up Passwordless SSH Login
  5. Configure DigitalOcean's Managed Databases for data persistence

#django #digitalocean #docker #GitHub 

What is GEEK

Buddha Community

How to Deploy Django to DigitalOcean with Docker and GitHub Actions
Ahebwe  Oscar

Ahebwe Oscar

1633552260

How to Deploy Django to DigitalOcean with Docker and GitHub Actions

LearnHow to Deploy Django to DigitalOcean with Docker and GitHub Actions

By the end of this tutorial, you will be able to:

  1. Deploy Django to DigitalOcean with Docker
  2. Configure GitHub actions to continuously deploy Django to DigitalOcean
  3. Use GitHub Packages to store Docker Images
  4. Set up Passwordless SSH Login
  5. Configure DigitalOcean's Managed Databases for data persistence

#django #digitalocean #docker #GitHub 

Audra  Haag

Audra Haag

1661060280

Configure GitHub Actions to Deploy A Django and Docker To DigitalOcean

In this tutorial, we'll look at how to configure GitHub Actions to continuously deploy a Django and Docker application to DigitalOcean.

Source: https://testdriven.io

#django #github #digitalocean #docker 

Ahebwe  Oscar

Ahebwe Oscar

1620177818

Django admin full Customization step by step

Welcome to my blog , hey everyone in this article you learn how to customize the Django app and view in the article you will know how to register  and unregister  models from the admin view how to add filtering how to add a custom input field, and a button that triggers an action on all objects and even how to change the look of your app and page using the Django suit package let’s get started.

Database

Custom Titles of Django Admin

Exclude in Django Admin

Fields in Django Admin

#django #create super user django #customize django admin dashboard #django admin #django admin custom field display #django admin customization #django admin full customization #django admin interface #django admin register all models #django customization

工藤  晃

工藤 晃

1661038380

配置 GitHub Actions 以將 Django 和 Docker 部署到 DigitalOcean

在本教程中,我們將了解如何配置 GitHub Actions 以將 Django 和 Docker 應用程序持續部署到 DigitalOcean。

依賴項

  1. Django v3.2.4
  2. 碼頭工人 v20.04
  3. Python v3.9.5

目標

在本教程結束時,您將能夠:

  1. 使用 Docker 將 Django 部署到 DigitalOcean
  2. 配置 GitHub 操作以將 Django 持續部署到 DigitalOcean
  3. 使用 GitHub Packages 存儲 Docker 圖像
  4. 設置無密碼 SSH 登錄
  5. 配置 DigitalOcean 的託管數據庫以實現數據持久性

項目設置

除了 Django 和 Docker,我們將使用的演示項目包括 Postgres、Nginx 和 Gunicorn。

首先克隆基礎項目:

$ git clone https://github.com/testdrivenio/django-github-digitalocean.git --branch base --single-branch
$ cd django-github-digitalocean

要在本地進行測試,請構建映像並啟動容器:

$ docker-compose up -d --build

導航到http://localhost:8000/。你應該看到:

{
  "hello": "world"
}

GitHub 包

GitHub Packages是一個包管理服務,與 GitHub 完全集成。它允許您在 GitHub 上的項目中公開或私下託管您的軟件包。我們將使用它來存儲 Docker 映像。

假設您在 GitHub 上有一個帳戶,請為此項目創建一個新存儲庫,並將本地項目的源遠程更新為指向您剛剛創建的存儲庫。

要在本地進行測試,您需要創建個人訪問令牌。在您的Developer Settings中,單擊“個人訪問令牌”。然後,單擊“生成新令牌”。提供描述性說明並選擇以下範圍:

  1. write:packages
  2. read:packages
  3. delete:packages

github個人訪問令牌

記下令牌。

構建並標記圖像:

$ docker build -f app/Dockerfile -t ghcr.io/<USERNAME>/<REPOSITORY_NAME>/web:latest ./app

# example:
# docker build -f app/Dockerfile -t ghcr.io/testdrivenio/django-github-digitalocean/web:latest ./app

接下來,使用您的個人訪問令牌,通過 Docker 向 GitHub Packages 進行身份驗證:

$ docker login ghcr.io -u <USERNAME> -p <TOKEN>

# example:
# docker login ghcr.io -u testdrivenio -p ce70f1d4a3a906ce8ac24caa6870fd29f2273d30

將圖像推送到GitHub Packages 上的容器註冊表:

$ docker push ghcr.io/<USERNAME>/<REPOSITORY_NAME>/web:latest

# example:
# docker push ghcr.io/testdrivenio/django-github-digitalocean/web:latest

您現在應該能夠在以下 URL 之一看到包(取決於您使用的是個人帳戶還是組織):

https://github.com/orgs/<USERNAME>/packages

https://github.com/<USERNAME>?tab=packages

github包

數字海洋

讓我們設置 DigitalOcean 以使用我們的應用程序。

首先,您需要註冊一個DigitalOcean帳戶(如果您還沒有),然後生成一個訪問令牌,以便您可以訪問DigitalOcean API

將令牌添加到您的環境中:

$ export DIGITAL_OCEAN_ACCESS_TOKEN=[your_digital_ocean_token]

水滴

接下來,創建一個預先安裝了 Docker的新 Droplet :

$ curl -X POST \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    -d '{"name":"django-docker","region":"sfo3","size":"s-2vcpu-4gb","image":"docker-20-04"}' \
    "https://api.digitalocean.com/v2/droplets"

檢查狀態:

$ curl \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    "https://api.digitalocean.com/v2/droplets?name=django-docker"

如果您安裝了jq,那麼您可以像這樣解析 JSON 響應:

$ curl \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    "https://api.digitalocean.com/v2/droplets?name=django-docker" \
  | jq '.droplets[0].status'

root 密碼應通過電子郵件發送給您。取回它。然後,一旦 droplet 的狀態為active,以 root 身份通過 SSH 進入實例並在提示時更新密碼。

接下來,生成一個新的 SSH 密鑰:

$ ssh-key gen -t rsa

將密鑰保存到/root/.ssh/id_rsa並且不要設置密碼。這將生成一個公鑰和私鑰——分別為id_rsaid_rsa.pub。要設置無密碼 SSH 登錄,請將公鑰複製到authorized_keys文件並設置適當的權限:

$ cat ~/.ssh/id_rsa.pub
$ vi ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/id_rsa

複製私鑰的內容:

$ cat ~/.ssh/id_rsa

退出 SSH 會話,然後將密鑰設置為本地計算機上的環境變量:

export PRIVATE_KEY='-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA04up8hoqzS1+APIB0RhjXyObwHQnOzhAk5Bd7mhkSbPkyhP1
...
iWlX9HNavcydATJc1f0DpzF0u4zY8PY24RVoW8vk+bJANPp1o2IAkeajCaF3w9nf
q/SyqAWVmvwYuIhDiHDaV2A==
-----END RSA PRIVATE KEY-----'

將密鑰添加到ssh-agent

$ ssh-add - <<< "${PRIVATE_KEY}"

要測試,請運行:

$ ssh -o StrictHostKeyChecking=no root@<YOUR_INSTANCE_IP> whoami

root

然後,為應用程序創建一個新目錄:

$ ssh -o StrictHostKeyChecking=no root@<YOUR_INSTANCE_IP> mkdir /app

數據庫

繼續前進,讓我們通過 DigitalOcean 的Managed Databases啟動生產 Postgres 數據庫:

$ curl -X POST \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    -d '{"name":"django-docker-db","region":"sfo3","engine":"pg","version":"13","size":"db-s-2vcpu-4gb","num_nodes":1}' \
    "https://api.digitalocean.com/v2/databases"

檢查狀態:

$ curl \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    "https://api.digitalocean.com/v2/databases?name=django-docker-db" \
  | jq '.databases[0].status'

旋轉起來需要幾分鐘。一旦狀態為online,獲取連接信息:

$ curl \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$DIGITAL_OCEAN_ACCESS_TOKEN'' \
    "https://api.digitalocean.com/v2/databases?name=django-docker-db" \
  | jq '.databases[0].connection'

示例響應:

{
  "protocol": "postgresql",
  "uri": "postgresql://doadmin:v60qcyaito1i0h66@django-docker-db-do-user-778274-0.a.db.ondigitalocean.com:25060/defaultdb?sslmode=require",
  "database": "defaultdb",
  "host": "django-docker-db-do-user-778274-0.a.db.ondigitalocean.com",
  "port": 25060,
  "user": "doadmin",
  "password": "v60qcyaito1i0h66",
  "ssl": true
}

GitHub 操作

要配置GitHub Actions,首先在項目的根目錄中添加一個名為“.github”的新目錄。在該目錄中添加另一個名為“workflows”的目錄。現在,要配置由一個或多個作業組成的工作流,請在“工作流”目錄中創建一個名為main.yml的新文件。

建立工作

name: Continuous Integration and Delivery

on: [push]

env:
  WEB_IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')/web
  NGINX_IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')/nginx

jobs:

  build:
    name: Build Docker Images
    runs-on: ubuntu-latest
    steps:
      - name: Checkout master
        uses: actions/checkout@v1
      - name: Add environment variables to .env
        run: |
          echo DEBUG=0 >> .env
          echo SQL_ENGINE=django.db.backends.postgresql >> .env
          echo DATABASE=postgres >> .env
          echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
          echo SQL_DATABASE=${{ secrets.SQL_DATABASE }} >> .env
          echo SQL_USER=${{ secrets.SQL_USER }} >> .env
          echo SQL_PASSWORD=${{ secrets.SQL_PASSWORD }} >> .env
          echo SQL_HOST=${{ secrets.SQL_HOST }} >> .env
          echo SQL_PORT=${{ secrets.SQL_PORT }} >> .env
      - name: Set environment variables
        run: |
          echo "WEB_IMAGE=$(echo ${{env.WEB_IMAGE}} )" >> $GITHUB_ENV
          echo "NGINX_IMAGE=$(echo ${{env.NGINX_IMAGE}} )" >> $GITHUB_ENV
      - name: Log in to GitHub Packages
        run: echo ${PERSONAL_ACCESS_TOKEN} | docker login ghcr.io -u ${{ secrets.NAMESPACE }} --password-stdin
        env:
          PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      - name: Pull images
        run: |
          docker pull ${{ env.WEB_IMAGE }} || true
          docker pull ${{ env.NGINX_IMAGE }} || true
      - name: Build images
        run: |
          docker-compose -f docker-compose.ci.yml build
      - name: Push images
        run: |
          docker push ${{ env.WEB_IMAGE }}
          docker push ${{ env.NGINX_IMAGE }}

在這裡,我們定義了一個將在所有推送時觸發的build 作業:

  1. 設置全局變量WEB_IMAGENGINX_IMAGE環境變量
  2. 檢查存儲庫,以便作業可以訪問它
  3. 將環境變量添加到.env文件
  4. 設置和環境變量WEB_IMAGENGINX_IMAGE以便可以在 Docker Compose 文件中訪問它們
  5. 登錄 GitHub 包
  6. 如果圖像存在,則拉取圖像
  7. 構建圖像
  8. 將圖像推送到 GitHub Packages

你注意到這些秘密了嗎?

  1. secrets.SECRET_KEY
  2. secrets.SQL_DATABASE
  3. secrets.SQL_USER
  4. secrets.SQL_PASSWORD
  5. secrets.SQL_HOST
  6. secrets.SQL_PORT
  7. secrets.NAMESPACE
  8. secrets.PERSONAL_ACCESS_TOKEN

這些需要在存儲庫的機密中設置(設置 > 機密)。根據上面的數據庫連接信息添加前六個變量。

例如:

  1. SECRET_KEY9zYGEFk2mn3mWB8Bmg9SAhPy6F4s7cCuT8qaYGVEnu7huGRKW9
  2. SQL_DATABASEdefaultdb
  3. SQL_HOSTdjango-docker-db-do-user-778274-0.a.db.ondigitalocean.com
  4. SQL_PORT25060
  5. SQL_USERdoadmin
  6. SQL_PASSWORDv60qcyaito1i0h66

使用您的 Github 用戶名或您的組織名稱NAMESPACE以及您的個人訪問令牌作為PERSONAL_ACCESS_TOKEN變量。

github的秘密

完成後,提交代碼並將其推送到 GitHub 以觸發新的構建。確保它通過。您應該在 GitHub Packages 中看到圖像:

github包

部署作業

接下來,添加一個deploy作業:

deploy:
  name: Deploy to DigitalOcean
  runs-on: ubuntu-latest
  needs: build
  steps:
    - name: Checkout master
      uses: actions/checkout@v1
    - name: Add environment variables to .env
      run: |
        echo DEBUG=0 >> .env
        echo SQL_ENGINE=django.db.backends.postgresql >> .env
        echo DATABASE=postgres >> .env
        echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
        echo SQL_DATABASE=${{ secrets.SQL_DATABASE }} >> .env
        echo SQL_USER=${{ secrets.SQL_USER }} >> .env
        echo SQL_PASSWORD=${{ secrets.SQL_PASSWORD }} >> .env
        echo SQL_HOST=${{ secrets.SQL_HOST }} >> .env
        echo SQL_PORT=${{ secrets.SQL_PORT }} >> .env
        echo WEB_IMAGE=${{ env.WEB_IMAGE }} >> .env
        echo NGINX_IMAGE=${{ env.NGINX_IMAGE }} >> .env
        echo NAMESPACE=${{ secrets.NAMESPACE }} >> .env
        echo PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }} >> .env
    - name: Add the private SSH key to the ssh-agent
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        mkdir -p ~/.ssh
        ssh-agent -a $SSH_AUTH_SOCK > /dev/null
        ssh-keyscan github.com >> ~/.ssh/known_hosts
        ssh-add - <<< "${{ secrets.PRIVATE_KEY }}"
    - name: Build and deploy images on DigitalOcean
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        scp  -o StrictHostKeyChecking=no -r ./.env ./docker-compose.prod.yml root@${{ secrets.DIGITAL_OCEAN_IP_ADDRESS }}:/app
        ssh -o StrictHostKeyChecking=no root@${{ secrets.DIGITAL_OCEAN_IP_ADDRESS }} << 'ENDSSH'
          cd /app
          source .env
          docker login ghcr.io -u $NAMESPACE -p $PERSONAL_ACCESS_TOKEN
          docker pull $WEB_IMAGE
          docker pull $NGINX_IMAGE
          docker-compose -f docker-compose.prod.yml up -d
        ENDSSH

因此,在deploy作業中,僅在build作業成功完成時運行(通過needs: build),我們:

  1. 檢查存儲庫,以便作業可以訪問它
  2. 將環境變量添加到.env文件
  3. 將私有 SSH 密鑰添加到 ssh-agent 並運行代理
  4. .envdocker-compose.prod.yml文件複製到遠程服務器
  5. SSH 到 DigitalOcean 上的刪除服務器
  6. 導航到部署目錄並設置環境變量
  7. 登錄 GitHub 包
  8. 拉取圖片
  9. 旋轉容器
  10. 結束 SSH 會話

DIGITAL_OCEAN_IP_ADDRESSPRIVATE_KEY秘密添加到 GitHub。然後,將服務器的 IP 添加到ALLOWED_HOSTSDjango 設置中的列表中。

提交並推送您的代碼以觸發新的構建。構建通過後,導航到您的實例的 IP。你應該看到:

{
  "hello": "world"
}

測試

最後,通過在下面deploy添加來更新作業,以便它僅在對分支進行更改時運行:if: github.ref == 'refs/heads/master'needs: buildmaster

deploy:
  name: Deploy to DigitalOcean
  runs-on: ubuntu-latest
  needs: build
  if: github.ref == 'refs/heads/master'
  steps:
    - name: Checkout master
      uses: actions/checkout@v1
    - name: Add environment variables to .env
      run: |
        echo DEBUG=0 >> .env
        echo SQL_ENGINE=django.db.backends.postgresql >> .env
        echo DATABASE=postgres >> .env
        echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
        echo SQL_DATABASE=${{ secrets.SQL_DATABASE }} >> .env
        echo SQL_USER=${{ secrets.SQL_USER }} >> .env
        echo SQL_PASSWORD=${{ secrets.SQL_PASSWORD }} >> .env
        echo SQL_HOST=${{ secrets.SQL_HOST }} >> .env
        echo SQL_PORT=${{ secrets.SQL_PORT }} >> .env
        echo WEB_IMAGE=${{ env.WEB_IMAGE }} >> .env
        echo NGINX_IMAGE=${{ env.NGINX_IMAGE }} >> .env
        echo NAMESPACE=${{ secrets.NAMESPACE }} >> .env
        echo PERSONAL_ACCESS_TOKEN=${{ secrets.PERSONAL_ACCESS_TOKEN }} >> .env
    - name: Add the private SSH key to the ssh-agent
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        mkdir -p ~/.ssh
        ssh-agent -a $SSH_AUTH_SOCK > /dev/null
        ssh-keyscan github.com >> ~/.ssh/known_hosts
        ssh-add - <<< "${{ secrets.PRIVATE_KEY }}"
    - name: Build and deploy images on DigitalOcean
      env:
        SSH_AUTH_SOCK: /tmp/ssh_agent.sock
      run: |
        scp  -o StrictHostKeyChecking=no -r ./.env ./docker-compose.prod.yml root@${{ secrets.DIGITAL_OCEAN_IP_ADDRESS }}:/app
        ssh -o StrictHostKeyChecking=no root@${{ secrets.DIGITAL_OCEAN_IP_ADDRESS }} << 'ENDSSH'
          cd /app
          source .env
          docker login ghcr.io -u $NAMESPACE -p $PERSONAL_ACCESS_TOKEN
          docker pull $WEB_IMAGE
          docker pull $NGINX_IMAGE
          docker-compose -f docker-compose.prod.yml up -d
        ENDSSH

要進行測試,請創建一個新develop分支。worldurls.py之後添加一個感嘆號:

def home(request):
    return JsonResponse({"hello": "world!"})

提交您的更改並將其推送到 GitHub。確保僅build作業運行。一旦構建通過,打開一個針對master分支的 PR 並合併更改。這將觸發具有兩個階段的新構建 -builddeploy. 確保部署按預期工作:

{
  "hello": "world!"
}

--

而已!您可以在django-github-digitalocean 存儲庫中找到最終代碼。

來源:  https ://testdriven.io

#django #github #digitalocean #docker 

Loma  Baumbach

Loma Baumbach

1595774031

ScaleGrid DigitalOcean Support for MySQL, PostgreSQL and Redis™

PALO ALTO, Calif., June 9, 2020 – ScaleGrid, a leading Database-as-a-Service (DBaaS) provider, has just announced support for their MySQLPostgreSQL and Redis™ solutions on DigitalOcean. This launch is in addition to their current DigitalOcean offering for MongoDB® database, the only DBaaS to support this database on DigitalOcean.

MySQL and PostgreSQL are the top two open source relational databases in the world, and Redis is the top key-value database. These databases are a natural fit for the developer market that has gravitated towards DigitalOcean since its launch just nine years ago in 2011. The open source model is not only popular with the developer market, but also enterprise companies looking to modernize their infrastructure and reduce spend.  DigitalOcean instance costs are also over 28% less expensive than AWS, and over 26% less than Azure, providing significant savings for companies who are struggling in this global climate.

ScaleGrid’s MySQL, PostgreSQL and Redis™ solutions on DigitalOcean are competitively priced starting at just $15/GB, the same as DigitalOcean’s Managed Database solution, but offer on average 30% more storage for the same price. Additionally, ScaleGrid offers several competitive advantages such as full superuser access, custom master-slave configurations, and advanced slow query analysis and monitoring capabilities through their sophisticated platform. To compare more features, check out their ScaleGrid vs. DigitalOcean MySQLScaleGrid vs. DigitalOcean PostgreSQL and ScaleGrid vs. DigitalOcean Redis™ pages.

#cloud #database #developer #digital ocean #mysql #postgresql #redis #scalegrid #advanced performance #database infrastructure #dbaas on digitalocean #digitalocean customers #digitalocean instance costs #digitalocean managed databases #high performance ssd #mysql digitalocean #postgresql digitalocean #redis digitalocean #scalegrid digitalocean #scalegrid vs. digitalocean