CI/CD with GitHub Actions: Build and Deploy Docker to a Server
DevOps

CI/CD with GitHub Actions: Build and Deploy Docker to a Server

  • AuthorLiam K.
  • Date
  • Time14 minutes

Why use Actions for Docker?

GitHub Actions provides easy integration with repositories, secrets and runners. We demonstrate a minimal pipeline to build, push and deploy images securely.

Example Workflow

yaml
name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build image
        run: docker build -t myapp:\${{ github.sha }} .
      - name: Push to registry
        run: |
          echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin registry.example.com
          docker tag myapp:\${{ github.sha }} registry.example.com/myorg/myapp:\${{ github.sha }}
          docker push registry.example.com/myorg/myapp:\${{ github.sha }}
      - name: Deploy via SSH
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: \${{ secrets.DEPLOY_HOST }}
          username: deploy
          key: \${{ secrets.DEPLOY_KEY }}
          script: |
            docker pull registry.example.com/myorg/myapp:\${{ github.sha }}
            docker-compose down && docker-compose up -d

Secure Secrets and Deploy Keys

Use repository secrets for registry credentials and a deploy key with limited privileges for SSH access. Rotate keys regularly.

Technical Author

Liam K.

Liam K.

Founder of Trivox. Writes about how the platform, the network, and the Amsterdam sites fit together.