mirror of
https://github.com/dadosfera/maestro.git
synced 2026-08-31 19:58:21 +00:00
208 lines
7.1 KiB
YAML
208 lines
7.1 KiB
YAML
name: Deploy
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
workflow_dispatch:
|
|
inputs:
|
|
environment:
|
|
description: 'Deployment environment'
|
|
required: true
|
|
type: choice
|
|
options:
|
|
- dev
|
|
- stg
|
|
- prd
|
|
|
|
jobs:
|
|
extract_environment:
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
environment: ${{ steps.extract_environment.outputs.environment }}
|
|
env:
|
|
EVENT: ${{ github.event_name }}
|
|
DEPLOY_ENV: ${{ github.event.inputs.environment }}
|
|
steps:
|
|
- name: Extract Environment
|
|
run: |
|
|
if [ ${EVENT} == "workflow_dispatch" ]; then
|
|
echo "##[set-output name=environment;]$(echo ${DEPLOY_ENV})"
|
|
elif [ ${GITHUB_REF} == "refs/heads/main" ]; then
|
|
echo "##[set-output name=environment;]$(echo "prd")"
|
|
fi
|
|
id: extract_environment
|
|
|
|
semantic_release:
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
new_release_published: ${{ steps.semantic.outputs.new_release_published }}
|
|
new_release_version: ${{ (steps.semantic.outputs.new_release_published == 'true' && steps.semantic.outputs.new_release_version) || (github.event_name == 'workflow_dispatch' && '0.0.0') }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v2
|
|
|
|
- if: github.event_name != 'workflow_dispatch'
|
|
name: Semantic Release
|
|
uses: cycjimmy/semantic-release-action@v2
|
|
id: semantic
|
|
with:
|
|
semantic_version: 16
|
|
extra_plugins: |
|
|
conventional-changelog-eslint
|
|
branches: |
|
|
[
|
|
'main'
|
|
]
|
|
env:
|
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
deploy:
|
|
if: ${{ github.event_name == 'workflow_dispatch' || needs.semantic_release.outputs.new_release_published == 'true' }}
|
|
needs: [extract_environment, semantic_release]
|
|
runs-on:
|
|
[self-hosted, '${{ needs.extract_environment.outputs.environment }}']
|
|
|
|
steps:
|
|
- name: Printing stats
|
|
env:
|
|
EVENT: ${{ github.event_name }}
|
|
IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
run: echo ${GITHUB_REF#refs/heads/}
|
|
|
|
- name: Checkout
|
|
uses: actions/checkout@v2
|
|
|
|
- name: Update Pip
|
|
run: |
|
|
python3 -m pip install --upgrade pip
|
|
|
|
- name: Install Docker Compose
|
|
run: |
|
|
python3 -m pip install docker-compose --upgrade
|
|
|
|
- name: Install AWS CLI
|
|
run: |
|
|
python3 -m pip install awscli --upgrade
|
|
|
|
- name: Install AWS Elastic Beanstalk CLI
|
|
run: |
|
|
python3 -m pip install awsebcli --upgrade
|
|
|
|
- name: Configure AWS Region
|
|
uses: aws-actions/configure-aws-credentials@v1
|
|
id: aws
|
|
with:
|
|
aws-region: us-east-1
|
|
|
|
- name: Login to AWS ECR
|
|
id: login_ecr
|
|
uses: aws-actions/amazon-ecr-login@v1
|
|
|
|
- name: Build, Tag, and Push Image to AWS ECR
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
ACCOUNT_ID: ${{ steps.aws.outputs.aws-account-id }}
|
|
run: |
|
|
docker-compose -f build.docker-compose.yml build
|
|
docker-compose -f build.docker-compose.yml push
|
|
|
|
- name: Create ZIP file to Deploy AWS Beanstalk
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
ACCOUNT_ID: ${{ steps.aws.outputs.aws-account-id }}
|
|
run: |
|
|
sed -i -e "s/\${ENV}/$ENV/g" docker-compose.yml
|
|
sed -i -e "s/\${IMAGE_TAG}/$IMAGE_TAG/g" docker-compose.yml
|
|
sed -i -e "s/\${ACCOUNT_ID}/$ACCOUNT_ID/g" docker-compose.yml
|
|
zip deploy.zip docker-compose.yml -r .ebextensions
|
|
|
|
- name: Deploy AWS Beanstalk
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
AWS_REGION: us-east-1
|
|
APP_NAME: ${{ github.event.repository.name }}
|
|
run: |
|
|
eb use $APP_NAME-$ENV
|
|
echo -e "deploy:\n artifact: deploy.zip" >> .elasticbeanstalk/config.yml
|
|
eb deploy
|
|
|
|
- name: Remove Docker's Trash
|
|
if: always()
|
|
run: |
|
|
docker system prune --volumes -a -f
|
|
docker system df
|
|
api_docs:
|
|
needs: [extract_environment, semantic_release, deploy]
|
|
runs-on:
|
|
[self-hosted, '${{ needs.extract_environment.outputs.environment }}']
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v2
|
|
|
|
- name: Extract Docs PageId
|
|
if: needs.semantic_release.outputs.new_release_published == 'true'
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
DEV_DOCS_PAGE_ID: ${{ secrets.DEV_DOCS_PAGE_ID }}
|
|
STG_DOCS_PAGE_ID: ${{ secrets.STG_DOCS_PAGE_ID }}
|
|
PRD_DOCS_PAGE_ID: ${{ secrets.PRD_DOCS_PAGE_ID }}
|
|
shell: bash
|
|
run: |
|
|
if [ $ENV == "dev" ]; then
|
|
echo "##[set-output name=result;]$(echo $DEV_DOCS_PAGE_ID)"
|
|
elif [ $ENV == "stg" ]; then
|
|
echo "##[set-output name=result;]$(echo $STG_DOCS_PAGE_ID)"
|
|
elif [ $ENV == "prd" ]; then
|
|
echo "##[set-output name=result;]$(echo $PRD_DOCS_PAGE_ID)"
|
|
fi
|
|
id: extract_docs_page_id
|
|
|
|
- name: Extract Docs BlockId
|
|
if: needs.semantic_release.outputs.new_release_published == 'true'
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
DEV_DOCS_BLOCK_ID: ${{ secrets.DEV_DOCS_BLOCK_ID }}
|
|
STG_DOCS_BLOCK_ID: ${{ secrets.STG_DOCS_BLOCK_ID }}
|
|
PRD_DOCS_BLOCK_ID: ${{ secrets.PRD_DOCS_BLOCK_ID }}
|
|
shell: bash
|
|
run: |
|
|
if [ $ENV == "dev" ]; then
|
|
echo "##[set-output name=result;]$(echo $DEV_DOCS_BLOCK_ID)"
|
|
elif [ $ENV == "stg" ]; then
|
|
echo "##[set-output name=result;]$(echo $STG_DOCS_BLOCK_ID)"
|
|
elif [ $ENV == "prd" ]; then
|
|
echo "##[set-output name=result;]$(echo $PRD_DOCS_BLOCK_ID)"
|
|
fi
|
|
id: extract_docs_block_id
|
|
|
|
- name: Generate API docs
|
|
if: needs.semantic_release.outputs.new_release_published == 'true'
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
DOCS_URL: ${{ secrets.DOCS_URL }}
|
|
DOCS_API_TOKEN: ${{ secrets.DOCS_API_TOKEN }}
|
|
DOCS_PAGE_ID: ${{ steps.extract_docs_page_id.outputs.result }}
|
|
DOCS_BLOCK_ID: ${{ steps.extract_docs_block_id.outputs.result }}
|
|
continue-on-error: true
|
|
run: |
|
|
curl -XPOST \
|
|
--url $DOCS_URL \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"service": "$DOCS_API_TOKEN",
|
|
"pageId": "$DOCS_PAGE_ID",
|
|
"blockId": "$DOCS_BLOCK_ID",
|
|
"schema": "../swagger.json"
|
|
}'
|
|
|
|
|
|
- name: Cleanup Docker's Leftovers
|
|
if: always()
|
|
continue-on-error: true
|
|
run: |
|
|
docker system prune
|
|
docker rmi -f $(docker images -aq)
|