Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankbrgowda committed Apr 9, 2024
1 parent 47a61db commit 881c1fc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 61 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Deploy

on:
workflow_call:
inputs:
environment:
required: true
type: string
secrets:
aws_access_key_id:
required: true
aws_secret_access_key:
required: true
ssh_private_key:
required: true
jwt_secret:
required: true
session_secret:
required: true
google_client_id:
required: true
google_client_secret:
required: true
github_token:
required: true

jobs:
deploy:
name: Deploy to demo staging server
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
needs: build-and-push-docker
steps:
- name: Check out
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.aws_access_key_id }}
aws-secret-access-key: ${{ secrets.aws_secret_access_key }}
aws-region: us-east-1
- name: Get instance address
id: ec2-describe-instances
run: |
INSTANCE_ADDRESS=$(aws ec2 describe-instances \
--instance-ids ${{ vars.instance_id }} \
--query "Reservations[*].Instances[*].[PublicDnsName]" \
--output text)
echo "INSTANCE_ADDRESS=$INSTANCE_ADDRESS" >> "$GITHUB_OUTPUT"
- name: Set up SSH
run: |
mkdir --parents ~/.ssh
echo "${{ secrets.ssh_private_key }}" > ~/.ssh/staging
chmod 600 ~/.ssh/staging
cat >>~/.ssh/config <<END
Host staging
HostName ${{ steps.ec2-describe-instances.outputs.INSTANCE_ADDRESS }}
User ec2-user
IdentityFile ~/.ssh/staging
END
ssh-keyscan -H ${{ steps.ec2-describe-instances.outputs.INSTANCE_ADDRESS }} >> ~/.ssh/known_hosts
- name: Create Docker context
run: |
docker context create staging \
--docker host=ssh://staging \
--description "Staging server"
- name: Log in to the GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.github_token }}
- name: Deploy
env:
JWT_SECRET: ${{ secrets.jwt_secret }}
SESSION_SECRET: ${{ secrets.session_secret }}
GOOGLE_CLIENT_ID: ${{ secrets.google_client_id }}
GOOGLE_CLIENT_SECRET: ${{ secrets.google_client_secret }}
URL: ${{ vars.url }}
working-directory: .github/workflows/deploy
run: |
docker --context staging compose down
docker --context staging compose pull
docker --context staging compose up --build --detach
85 changes: 24 additions & 61 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
name: Push

on:
push: null
push:
branches:
- main
release:
types:
- created
workflow_dispatch:
inputs:
publish:
Expand Down Expand Up @@ -85,63 +90,21 @@ jobs:
collaboration server
cache-from: type=registry,ref=user/app:latest
cache-to: type=inline
deploy:
name: Deploy to demo staging server
runs-on: ubuntu-latest
environment: staging
needs: build-and-push-docker
if:
${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') ||
( inputs.deploy && always()) }}
steps:
- name: Check out
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Get instance address
id: ec2-describe-instances
run: |
INSTANCE_ADDRESS=$(aws ec2 describe-instances \
--instance-ids ${{ vars.INSTANCE_ID }} \
--query "Reservations[*].Instances[*].[PublicDnsName]" \
--output text)
echo "INSTANCE_ADDRESS=$INSTANCE_ADDRESS" >> "$GITHUB_OUTPUT"
- name: Set up SSH
run: |
mkdir --parents ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/staging
chmod 600 ~/.ssh/staging
cat >>~/.ssh/config <<END
Host staging
HostName ${{ steps.ec2-describe-instances.outputs.INSTANCE_ADDRESS }}
User ec2-user
IdentityFile ~/.ssh/staging
END
ssh-keyscan -H ${{ steps.ec2-describe-instances.outputs.INSTANCE_ADDRESS }} >> ~/.ssh/known_hosts
- name: Create Docker context
run: |
docker context create staging \
--docker host=ssh://staging \
--description "Staging server"
- name: Log in to the GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
URL: ${{ vars.URL }}
working-directory: .github/workflows/deploy
run: |
docker --context staging compose down
docker --context staging compose pull
docker --context staging compose up --build --detach
deploy-staging:
name: Deploy to staging server
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
uses: ./.github/workflows/deploy.yml
with:
environment: 'staging'
url: ${{ vars.URL }}
instance_id: ${{ vars.INSTANCE_ID }}
secrets: inherit
deploy-demo:
name: Deploy to demo server
if: ${{ github.event_name == 'release' }}
uses: ./.github/workflows/deploy.yml
with:
environment: 'demo'
url: ${{ vars.URL }}
instance_id: ${{ vars.INSTANCE_ID }}
secrets: inherit

0 comments on commit 881c1fc

Please sign in to comment.