Deploy to Docker Hub #34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker Deploy CI/CD | |
| run-name: Deploy to Docker Hub | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v3 | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Generate Environment File | |
| run: | | |
| echo "NEXT_PUBLIC_CLIENT=${{ secrets.NEXT_PUBLIC_CLIENT }}" >> .env.production | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Cache Docker layers | |
| uses: actions/cache@v3 | |
| with: | |
| path: /tmp/.buildx-cache | |
| key: ${{ runner.os }}-docker-${{ github.sha }} | |
| restore-keys: | | |
| ${{ runner.os }}-docker- | |
| - name: Build and Push Docker Image | |
| run: | | |
| docker buildx build --cache-from=type=local,src=/tmp/.buildx-cache \ | |
| --cache-to=type=local,dest=/tmp/.buildx-cache \ | |
| --push \ | |
| -t ${{ secrets.DOCKERHUB_USERNAME}}/rhythm-up:latest . | |
| - name: Deploy to Server with Docker Compose | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| port: ${{ secrets.SERVER_PORT }} | |
| script: | | |
| cd /home/${{ secrets.SERVER_USER }} | |
| if [ ! -d "youtube-share/.git" ]; then | |
| echo "Repository does not exist. Cloning the repository..." | |
| git clone https://github.com/${{ github.repository }}.git youtube-share | |
| cd youtube-share | |
| git checkout main | |
| else | |
| echo "Repository exists. Pulling latest changes..." | |
| cd youtube-share | |
| git pull origin main | |
| fi | |
| if ! docker network inspect shared_network >/dev/null 2>&1; then | |
| echo "Network 'shared_network' does not exist. Creating it..." | |
| docker network create shared_network | |
| fi | |
| docker-compose pull | |
| docker-compose down | |
| docker image prune -af | |
| docker-compose up -d |