Update docker-image.yml #24
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: Build and Deploy Docker Image to Server | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| build: | |
| name: Push Docker Image to Docker Hub and Deploy to Server | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v2 | |
| - name: Login to Docker Hub | |
| id: docker-hub | |
| run: | | |
| docker login -u ${{secrets.DOCKER_USERNAME}} -p ${{secrets.DOCKER_PASSWORD}} | |
| - name: Build the Docker Image | |
| id: build-docker-image | |
| run: | | |
| ls -la | |
| docker build . -f Dockerfile -t ${{secrets.DOCKER_USERNAME}}/webportfolio:latest | |
| - name: Push the Docker Image | |
| id: push-docker-image | |
| run: docker push ${{secrets.DOCKER_USERNAME}}/webportfolio:latest | |
| - name: Set Up SSH Key | |
| env: | |
| PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} # Reference the private key stored in GitHub Secrets | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| - name: Connect to server via SSH | |
| run: | | |
| ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no opc@${{secrets.VPS_HOST}} "echo 'Connected successfully!'" | |