Question

How to deploy an app to droplet via ssh action?

I was able to build and push image to the container registry.But somehow when it comes to deploy section, it cannot deploy successfully. Also something starnge is what I can see from Contrl plane UI and after ssh into the server is totally different. Even there’s command that delete old image it can be deleted from control plane, but from inside the droplet, it still has multi images with new tag names. Also I successfully installed docker and can see from github pipeline, but from the server it shows no docker. Could someone point out me to the right directrion?

jobs: build_and_push: runs-on: ubuntu-latest steps: - name: Checkout the repo 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: Remove all old images
    run: if [ ! -z "$(doctl registry repository list | grep "$(echo $IMAGE_NAME)")" ]; then doctl registry repository delete-manifest $(echo $IMAGE_NAME) $(doctl registry repository list-tags $(echo $IMAGE_NAME) | grep -o "sha.*") --force; else echo "No repository"; fi

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

  - name: Run registry garbage collection
    run: doctl registry garbage-collection start --include-untagged-manifests --force

deploy: runs-on: ubuntu-latest needs: build_and_push

steps:
  - name: Install Docker/
    uses: docker-practice/actions-setup-docker@master
    timeout-minutes: 12
  - 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,{{ secrets.DIGITALOCEAN_ACCESS_TOKEN }},GITHUB_SHA
      script: |
        # Login to registry
        docker login -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} -p ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} registry.digitalocean.com
        # 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 \
        --name $(echo $IMAGE_NAME) \
        $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
April 6, 2024

Hi there,

This is quite strange, if you’re seeing inconsistencies between what GitHub Actions reports and what you see on the Droplet, ensure that Docker is correctly installed and running on the Droplet.

You can verify this by logging into the Droplet via SSH and running docker --version. If Docker isn’t installed, you’ll need to install it on the server itself:

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04

If there are differences between the Control Plane UI and the actual state inside the Droplet, it could be a synchronization or caching issue:

https://www.digitalocean.com/community/questions/how-to-clean-up-container-registry

Regarding the GitHub workflow, what I could suggest here is to add more verbose logging to your SSH script commands to track each step’s execution and output. That way you could get more information on what might be happening and where things might be failing.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.