final commit #28
This file contains 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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v2 | |
with: | |
context: ./mail | |
file: ./mail/DOCKERFILE | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/my_email_app:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
env: | |
EC2_SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} | |
EC2_URL: ${{ secrets.EC2_URL }} | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
steps: | |
- name: Setup SSH for EC2 | |
uses: omarhosny206/[email protected] | |
with: | |
EC2_SSH_PRIVATE_KEY: $EC2_SSH_PRIVATE_KEY | |
EC2_URL: $EC2_URL | |
# then you can run commands/scripts directly on the EC2 instance e.g.: | |
- name: Create a new file on the EC2 instance with "hello-world" | |
run: | | |
ssh -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_URL << 'EOF' | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/my_email_app:latest | |
sudo docker stop my_email_app || true | |
sudo docker rm my_email_app || true | |
sudo docker run -d --name my_email_app -p 80:8000 ${{ secrets.DOCKER_USERNAME }}/my_email_app:latest | |
EOF |