Deploy Nginx with API passing through it. #23
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 Deploy Docker Image to EC2 | |
on: | |
push: | |
branches: | |
- deploy-nginx | |
jobs: | |
build-nginx-image: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Login to Docker Hub | |
run: echo ${{ secrets.DOCKER_REGISTRY_TOKEN }} | docker login -u ${{ secrets.DOCKER_REGISTRY_USERNAME }} --password-stdin | |
- name: Build Docker image | |
run: docker build -t elarsaks/gorilla-labs-nginx:${{ github.sha }} ./apps/nginx | |
- name: Push Docker image to Docker Hub | |
run: docker push elarsaks/gorilla-labs-nginx:${{ github.sha }} | |
deploy-to-ec2: | |
needs: build-nginx-image | |
runs-on: ubuntu-latest | |
steps: | |
#TODO: Fix this - Running it places SSL keys on the machine, but then the step itself fails | |
# - name: Decode and Transfer SSL Certificate | |
# uses: appleboy/[email protected] | |
# with: | |
# host: ${{ secrets.EC2_HOST }} | |
# key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
# username: ubuntu | |
# script: | | |
# # Decode and transfer SSL certificate and key | |
# echo "${{ secrets.SSL_CERTIFICATE }}" | base64 -d > certificate.crt | |
# echo "${{ secrets.SSL_KEY }}" | base64 -d > private.key | |
# scp certificate.crt private.key ubuntu@${{ secrets.EC2_HOST }}:/home/ubuntu/ | |
- name: Docker Pull & Run From Hub | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
username: ubuntu | |
script: | | |
echo ${{ secrets.DOCKER_REGISTRY_TOKEN }} | docker login -u ${{ secrets.DOCKER_REGISTRY_USERNAME }} --password-stdin | |
docker pull elarsaks/gorilla-labs-nginx:${{ github.sha }} | |
# Check if the container is running and stop it | |
if docker ps -q -f name=gorilla-labs-nginx; then | |
docker stop gorilla-labs-nginx | |
fi | |
# Check if the container exists and remove it | |
if docker ps -a -q -f name=gorilla-labs-nginx; then | |
docker rm gorilla-labs-nginx | |
fi | |
# Run the new container with restart policy, connected to the network | |
docker run -d --restart=always \ | |
--name gorilla-labs-nginx \ | |
--network gorilla-labs-network \ | |
-p 80:80 \ | |
-p 8080:8080 \ | |
-p 443:443 \ | |
-v /home/ubuntu/ssl:/etc/nginx/ssl \ | |
elarsaks/gorilla-labs-nginx:${{ github.sha }} |