terraform with github action #63
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: Build on DockerHub & Deploy | |
to Ec2 | |
on: | |
push: | |
branches: | |
- main | |
env: | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | |
AWS_PRIVATE_KEY: ${{ secrets.AWS_PRIVATE_KEY }} | |
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: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v2 | |
with: | |
context: ./ | |
push: true | |
dockerfile: ./Dockerfile | |
tags: junitalama/my-volunteer-app:latest | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_TOKEN }} | |
- name: Set permissions for private key | |
run: | | |
echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem | |
chmod 600 key.pem | |
- name: Pull Docker image | |
run: | | |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker pull junitalama/my-volunteer-app:latest' | |
- name: Stop running container | |
run: | | |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker stop stupefied_raman || true' | |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker rm stupefied_raman || true' | |
- name: Run new container | |
run: | | |
ssh -o StrictHostKeyChecking=no -i key.pem [email protected] 'sudo docker run -d --name stupefied_raman -p 5000:5000 -e DB_URL=${{ secrets.DB_URL }} junitalama/my-volunteer-app:latest' | |
# name: EC2 | |
# on: | |
# push: | |
# branches: | |
# - main | |
# jobs: | |
# deploy: | |
# name: Deploy to EC2 on main branch push | |
# runs-on: ubuntu-latest | |
# steps: | |
# - name: Checkout the files | |
# uses: actions/checkout@v4 | |
# - name: Deploy to server | |
# uses: easingthemes/ssh-deploy@main | |
# env: | |
# SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} | |
# REMOTE_HOST: ${{ secrets.HOST_DNS }} | |
# REMOTE_USER: ${{ secrets.USERNAME }} | |
# TARGET: ${{ secrets.TARGET_DIR }} |