cd #21
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: cd | |
on: | |
workflow_dispatch: | |
pull_request: | |
branches: [main] | |
env: | |
AWS_REGION: ap-south-1 | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
PRIVATE_SSH_KEY: ${{ secrets.AWS_SSH_KEY }} | |
SERVER_PUBLIC_IP: ${{ secrets.AWS_PUBLIC_KEY }} | |
jobs: | |
deploy: | |
name: deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: "1.21" | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Build the app | |
run: | | |
chmod +x ./scripts/build.sh | |
./scripts/build.sh | |
- 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: ap-south-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push image to Amazon ECR | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: verses | |
IMAGE_TAG: latest | |
run: | | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
- name: install goose | |
run: go install github.com/pressly/goose/v3/cmd/goose@latest | |
- name: Run migrations | |
run: | | |
cd sql/schema | |
goose postgres ${{secrets.DB_CONN}} up | |
- name: Deploy docker image to EC2 | |
env: | |
REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
REPOSITORY: verses | |
IMAGE_TAG: latest | |
AWS_DEFAULT_REGION: ap-south-1 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.AWS_PUBLIC_KEY}} | |
username: ubuntu | |
key: ${{ secrets.AWS_SSH_KEY }} | |
envs: PRIVATE_SSH_KEY,REGISTRY,REPOSITORY,IMAGE_TAG,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_DEFAULT_REGION,AWS_REGION | |
script: |- | |
sudo apt update | |
sudo apt install docker.io -y | |
sudo apt install awscli -y | |
sudo $(aws ecr get-login --no-include-email --region ap-south-1); | |
sudo docker stop deploy || true | |
sudo docker rm deploy || true | |
sudo docker pull $REGISTRY/$REPOSITORY:$IMAGE_TAG | |
sudo docker run -d -e PORT=80 -e JWT_SECRET=${{secrets.JWT_TOKEN}} -e GOLD_KEY=nfjd878 -e DB_CONN=${{secrets.DB_CONN}} --name deploy -p 80:80 $REGISTRY/$REPOSITORY:$IMAGE_TAG | |