Fixed ssh file configured in the wrong location #33
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 | |
| on: | |
| push: | |
| branches: [ main, feature/ansible ] | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.tags }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Extract Docker metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ secrets.DOCKERHUB_USERNAME }}/task-manager | |
| tags: | | |
| type=sha,prefix=,format=short | |
| - name: Build and push Docker image (client) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./client | |
| push: true | |
| tags: ${{ secrets.DOCKERHUB_USERNAME }}/task-manager-client:${{ github.sha }} | |
| - name: Build and push Docker image (server) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./server | |
| push: true | |
| tags: ${{ secrets.DOCKERHUB_USERNAME }}/task-manager-server:${{ github.sha }} | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Write SSH private key | |
| uses: webfactory/ssh-agent@v0.9.0 | |
| with: | |
| ssh-private-key: ${{ secrets.ANSIBLE_DROPLET_SSH_KEY }} | |
| - name: Write EC2 SSH key (used by Ansible on the droplet) | |
| run: | | |
| echo "${{ secrets.EC2_SSH_KEY }}" > /tmp/ssh-key.pem | |
| chmod 600 /tmp/ssh-key.pem | |
| - name: Copy Ansible files to droplet | |
| run: | | |
| ssh -o StrictHostKeyChecking=no \ | |
| ${{ secrets.ANSIBLE_DROPLET_USER }}@${{ secrets.ANSIBLE_DROPLET_HOST }} \ | |
| "mkdir -p ~/ansible" | |
| scp -o StrictHostKeyChecking=no \ | |
| .ansible/ansible.cfg \ | |
| .ansible/inventory_aws_ec2.yaml \ | |
| .ansible/deploy-docker-new-user.yaml \ | |
| ${{ secrets.ANSIBLE_DROPLET_USER }}@${{ secrets.ANSIBLE_DROPLET_HOST }}:~/ansible/ | |
| - name: Copy EC2 SSH key to droplet | |
| run: | | |
| scp ~/.ssh/ansible_droplet.pem \ | |
| -o StrictHostKeyChecking=no \ | |
| /tmp/ssh-key.pem \ | |
| ${{ secrets.ANSIBLE_DROPLET_USER }}@${{ secrets.ANSIBLE_DROPLET_HOST }}:~/.ssh-key.pem | |
| - name: Run Ansible playbook on droplet | |
| run: | | |
| ssh ~/.ssh/ansible_droplet.pem \ | |
| -o StrictHostKeyChecking=no \ | |
| ${{ secrets.ANSIBLE_DROPLET_USER }}@${{ secrets.ANSIBLE_DROPLET_HOST }} \ | |
| "cd ~/ansible && \ | |
| export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} && \ | |
| export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} && \ | |
| export AWS_REGION=ap-southeast-1 && \ | |
| ansible-playbook deploy-docker-new-user.yaml \ | |
| -e image_tag=${{ github.sha }} \ | |
| -e dockerhub_username=${{ secrets.DOCKERHUB_USERNAME }}" |