Merge pull request #3 from 3xjn/dev #161
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 Push Docker Image | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Log in to GitHub Container Registry | |
run: echo "${{ secrets.PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Set lowercase repository name | |
id: repo-name | |
run: echo "REPO_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
- name: Build Docker image | |
run: docker build -t ghcr.io/${{ env.REPO_NAME }}/my-container:latest . | |
- name: Push Docker image to GitHub Container Registry | |
run: docker push ghcr.io/${{ env.REPO_NAME }}/my-container:latest | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.DROPLET_IP }} >> ~/.ssh/known_hosts | |
- name: SSH into Droplet and Deploy using Docker | |
env: | |
MONGO_CONNECTION_STRING: ${{ secrets.MONGO_CONNECTION_STRING }} | |
MONGO_COLLECTION_NAME: ${{ secrets.MONGO_COLLECTION_NAME }} | |
MONGO_DATABASE_NAME: ${{ secrets.MONGO_DATABASE_NAME }} | |
JWT_PRIVATE_KEY: ${{ secrets.JWT_PRIVATE_KEY }} | |
JWT_PUBLIC_KEY: ${{ secrets.JWT_PUBLIC_KEY }} | |
AUTH_GOOGLE_SECRET: ${{ secrets.AUTH_GOOGLE_SECRET }} | |
AUTH_GOOGLE_ID: ${{ secrets.AUTH_GOOGLE_ID }} | |
run: | | |
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=10 -i ~/.ssh/id_rsa ${{ secrets.DROPLET_USERNAME }}@${{ secrets.DROPLET_IP }} << EOF | |
echo ${{ secrets.PAT }} | docker login ghcr.io -u 3xjn --password-stdin | |
docker stop my-todo-app || true && docker rm my-todo-app || true | |
docker pull ghcr.io/${{ env.REPO_NAME }}/my-container:latest | |
docker run -d --name my-todo-app \ | |
-p 8080:8080 \ | |
-e Mongo__ConnectionString="$MONGO_CONNECTION_STRING" \ | |
-e Mongo__CollectionName="$MONGO_COLLECTION_NAME" \ | |
-e Mongo__DatabaseName="$MONGO_DATABASE_NAME" \ | |
-e Jwt__PrivateKey="$JWT_PRIVATE_KEY" \ | |
-e Jwt__PublicKey="$JWT_PUBLIC_KEY" \ | |
-e Authentication__Google__ClientSecret="$AUTH_GOOGLE_SECRET" \ | |
-e Authentication__Google__ClientId="$AUTH_GOOGLE_ID" \ | |
ghcr.io/${{ env.REPO_NAME }}/my-container:latest | |
EOF |