Update workflow file by giving sudo permission for docker #3
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 and share Docker Image to Docker Hub | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-share: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_TOKEN }} | |
- name: Build and push | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/backend-web:latest | |
deploy: | |
needs: build-and-share | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: eu-north-1 | |
- name: Deploy to EC2 | |
env: | |
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
HOST: ${{ secrets.EC2_HOST }} | |
USER: ${{ secrets.EC2_USER }} | |
run: | | |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key | |
ssh -o StrictHostKeyChecking=no -i private_key ${USER}@${HOST} ' | |
sudo docker compose pull && | |
sudo docker compose up -d && | |
sudo docker image prune -f && | |
sudo systemctl restart nginx | |
' |