From 8412914e10c712fa4be69e8318bba29e035e7106 Mon Sep 17 00:00:00 2001 From: juanfri Date: Fri, 21 Jun 2024 12:11:09 +0200 Subject: [PATCH] add CI workflow --- .github/workflows/main.yml | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..81e23b3 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,61 @@ +name: CI/CD Pipeline + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: pytest + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/web-scraping:latest + + - name: Deploy to EC2 + env: + EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} + EC2_HOST: ${{ secrets.EC2_HOST }} + EC2_USER: ${{ secrets.EC2_USER }} + EC2_PORT: ${{ secrets.EC2_PORT }} + run: | + echo "${EC2_SSH_KEY}" > key.pem + chmod 600 key.pem + ssh -o StrictHostKeyChecking=no -i key.pem ${EC2_USER}@${EC2_HOST} -p ${EC2_PORT} << 'EOF' + docker pull ${{ secrets.DOCKER_USERNAME }}/web-scraping:latest + docker stop web-scraping || true + docker rm web-scraping || true + docker run -d -p 3000:3000 --name web-scraping ${{ secrets.DOCKER_USERNAME }}/web-scraping:latest + EOF