Question

Automatic deployment using Github Actions, Digital Ocean Registry into a Droplet,

Posted July 27, 2020 1.2k views
Docker

Hi,

I am currently woking on a workflow for my automatic deployment of dockerized application to digital ocean droplet.

Currently I am creating a docker image and pushing it to digital ocean registry using github actions.

Now I want to pull the image into my droplet from digital ocean registry and run the image.

What should be the command I should execute for this?

name: Docker

on:
  push:
    # Publish `master` as Docker `latest` image.
    branches:
      - master

    # Publish `v1.2.3` tags as releases.
    tags:
      - v*

  # Run tests for any PRs.
  pull_request:

env:
  # TODO: Change variable to your image's name.
  IMAGE_NAME: myimagename

jobs:
  # Run tests.
  # See also https://docs.docker.com/docker-hub/builds/automated-testing/
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Run tests
        run: |
          if [ -f docker-compose.test.yml ]; then
            docker-compose --file docker-compose.test.yml build
            docker-compose --file docker-compose.test.yml run sut
          else
            docker build . --file Dockerfile
          fi
  #Push to Digital Ocean private Registry
  pushToDORegistry:
    # Ensure test job passes before pushing image.
    needs: test

    runs-on: ubuntu-latest
    if: github.event_name == 'push' 
    steps:
      - uses: actions/checkout@v2
      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Install doctl                 # install the doctl on the runner
        uses: digitalocean/action-doctl@v2
        with:
            token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

      - name: push image to digitalocean
        run: |
          doctl registry login
          docker tag $IMAGE_NAME registry.digitalocean.com/<My Registry>/$IMAGE_NAME
          docker push registry.digitalocean.com/<My Registry>/$IMAGE_NAME
  #Run the container in droplet
  deploy:
    needs: pushToDORegistry
    runs-on: ubuntu-latest
    if: github.event_name == 'push' 
    steps:
        - uses: actions/checkout@v2
        - name: Executing remote  command
          uses: appleboy/ssh-action@master
          with:
            host: ${{ secrets.HOST }}
            USERNAME: ${{ secrets.USERNAME }}
            PORT: ${{ secrets.PORT }}
            password: ${{ secrets.PASSWORD }}
            script: |
                <WHAT IS COMMAND NEEDED TO PULL IMAGE FROM DIGITAL OCEAN REGISTRY and RUN THE DOCKER IMAGE>

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello!

I’m using this configuration

env:
  REGISTRY: "registry.digitalocean.com/your_registry"
  IMAGE_NAME: "image_name"

  COMMAND: "python main.py"

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout master
        uses: actions/checkout@v2

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install dependencies with pipenv
        run: |
          python -m pip install --upgrade pip
          pip install pipenv
          if [ -f Pipfile ]; then pipenv install --deploy --dev; fi

      - name: Lint with flake8
        run: |
          # stop the build if there are Python syntax errors or undefined names
          pipenv run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          pipenv run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

      - name: Test with pytest
        run: |
          pipenv run pytest .
  build_and_push:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    needs: test

    steps:
      - name: Checkout master
        uses: actions/checkout@v2

      - name: Build container image
        run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) .

      - name: Install doctl
        uses: digitalocean/action-doctl@v2
        with:
          token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

      - name: Log in to DigitalOcean Container Registry with short-lived credentials
        run: doctl registry login --expiry-seconds 600

      - name: Push image to DigitalOcean Container Registry
        run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)

  deploy:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    needs: build_and_push

    steps:
      - name: Deploy to Digital Ocean droplet via SSH action
        uses: appleboy/ssh-action@v0.1.3
        with:
          HOST: ${{ secrets.HOST }}
          USERNAME: ${{ secrets.USERNAME }}
          KEY: ${{ secrets.SSHKEY }}
          envs: IMAGE_NAME,REGISTRY,GITHUB_SHA,COMMAND
          script: |
            # Stop running container
            docker stop $(echo $IMAGE_NAME)

            # Remove old container
            docker rm $(echo $IMAGE_NAME)

            # Run a new container from a new image
            docker run -d \
            --restart always \
            --env-file .env \
            -p 8000:8000 \
            --name $(echo $IMAGE_NAME) \
            $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) $(echo $COMMAND)

Feel free to ask any questions

Submit an Answer