Upload docker code using CI/CD pipeline of github action

1. Install Docker on your server Click Here to get detailed description on how to upload your code on server using docker 2. Run " docker ps " command to get the image and container name 3. Store Secrets in GitHub Go to GitHub Repo → Settings → Secrets and variables → Actions → New Repository Secret, and add: - DOCKER_USERNAME → Your Docker Hub username - DOCKER_PASSWORD → Your Docker Hub password/token - SERVER_HOST → AWS Server IP - SERVER_USER → ubuntu - SSH_PRIVATE_KEY → Paste your AWS SSH Private Key 4. Create GitHub Actions Workflow Create .github/workflows/deploy.yml in your repo: name: Deploy to QA on: push: branches: - master # Runs on direct push to 'qa' jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Log in to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Build and Push Docker Image run: | docker build -t dockerusername/myapp:${{ github.sha }} . docker push dockerusername/myapp:${{ github.sha }} - name: Deploy to Server uses: appleboy/ssh-action@v0.1.10 with: host: ${{ secrets.SERVER_HOST }} username: ubuntu key: ${{ secrets.SSH_PRIVATE_KEY }} port: 22 script: | docker pull dockerusername/myapp:${{ github.sha }} docker ps -a # Debugging step to check existing containers docker stop myapp || true docker rm -f myapp || true docker run -d -p 3000:3000 --restart unless-stopped --name myapp dockerusername/myapp:${{ github.sha }} docker image prune -f myapp - docker image name create account on dockerhub this process will login to docker hub , build your image and pull that image to your server comment if you are facing any problem or need any help

Mar 7, 2025 - 12:48
 0
Upload docker code using CI/CD pipeline of github action

1. Install Docker on your server

Click Here to get detailed description on how to upload your code on server using docker

2. Run " docker ps " command to get the image and container name

3. Store Secrets in GitHub

Go to GitHub Repo → Settings → Secrets and variables → Actions → New Repository Secret, and add:

- DOCKER_USERNAME → Your Docker Hub username
- DOCKER_PASSWORD → Your Docker Hub password/token
- SERVER_HOST → AWS Server IP
- SERVER_USER → ubuntu
- SSH_PRIVATE_KEY → Paste your AWS SSH Private Key

4. Create GitHub Actions Workflow

Create .github/workflows/deploy.yml in your repo:

name: Deploy to QA

on:
  push:
    branches:
      - master  # Runs on direct push to 'qa'

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Push Docker Image
        run: |
          docker build -t dockerusername/myapp:${{ github.sha }} .
          docker push dockerusername/myapp:${{ github.sha }}

      - name: Deploy to Server
        uses: appleboy/ssh-action@v0.1.10
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ubuntu
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          script: |
            docker pull dockerusername/myapp:${{ github.sha }}
            docker ps -a  # Debugging step to check existing containers
            docker stop myapp || true
            docker rm -f myapp || true
            docker run -d -p 3000:3000 --restart unless-stopped --name myapp dockerusername/myapp:${{ github.sha }}
            docker image prune -f

  1. myapp - docker image name
  2. create account on dockerhub
  • this process will login to docker hub , build your image and pull that image to your server

  • comment if you are facing any problem or need any help