mirror of
https://github.com/dadosfera/maestro.git
synced 2026-08-31 19:58:21 +00:00
225 lines
8.7 KiB
YAML
225 lines
8.7 KiB
YAML
name: Deploy
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
workflow_dispatch:
|
|
inputs:
|
|
environment:
|
|
description: "Deployment environment"
|
|
required: true
|
|
type: choice
|
|
options:
|
|
- stg
|
|
- stg2
|
|
- prd
|
|
push_to_dockerhub:
|
|
description: "Push image to Dockerhub?"
|
|
required: true
|
|
type: boolean
|
|
default: false
|
|
|
|
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 "environment=${DEPLOY_ENV}" >> $GITHUB_OUTPUT
|
|
elif [ ${GITHUB_REF} == "refs/heads/main" ]; then
|
|
echo "environment=prd" >> $GITHUB_OUTPUT
|
|
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@v4
|
|
|
|
- if: github.event_name != 'workflow_dispatch'
|
|
name: Semantic Release
|
|
uses: cycjimmy/semantic-release-action@v4
|
|
id: semantic
|
|
with:
|
|
extra_plugins: |
|
|
conventional-changelog-eslint@4
|
|
branches: |
|
|
[
|
|
'main'
|
|
]
|
|
env:
|
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
deploy-info:
|
|
if: ${{ github.event_name == 'workflow_dispatch'}}
|
|
needs: [extract_environment, semantic_release]
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Create summary
|
|
env:
|
|
EVENT: ${{ github.event_name }}
|
|
IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
run: echo "### Deploy da branch \`$GITHUB_REF_NAME\` no ambiente **$ENV** :rocket:" >> $GITHUB_STEP_SUMMARY
|
|
|
|
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: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Update Pip
|
|
run: |
|
|
python3 -m pip install --upgrade pip
|
|
|
|
- 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@v4
|
|
id: aws
|
|
with:
|
|
aws-region: us-east-1
|
|
|
|
- name: Login to AWS ECR
|
|
id: login_ecr
|
|
uses: aws-actions/amazon-ecr-login@v2
|
|
|
|
- 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: Login to Docker Hub
|
|
if: ${{inputs.push_to_dockerhub}}
|
|
uses: docker/login-action@v2
|
|
with:
|
|
username: dadosfera
|
|
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
|
|
- name: Build, Tag, and Push Image to Dockerhub
|
|
if: ${{inputs.push_to_dockerhub}}
|
|
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.dockerhub.yml build
|
|
docker compose -f build.docker-compose.dockerhub.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 }}
|
|
NODE_EXPORTER_URL: ${{format('{0}.dkr.ecr.us-east-1.amazonaws.com\/monitoring\/node_exporter:latest', steps.aws.outputs.aws-account-id)}} # ${{needs.extract_environment.outputs.environment == 'prd' && '611330257153.dkr.ecr.us-east-1.amazonaws.com\/monitoring\/node_exporter:latest' || '468720548566.dkr.ecr.us-east-1.amazonaws.com\/monitoring\/node_exporter:latest'}}
|
|
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
|
|
sed -i -e "s/\${NODE_EXPORTER_URL}/$NODE_EXPORTER_URL/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: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Extract Docs BlockId and PageId
|
|
env:
|
|
ENV: ${{ needs.extract_environment.outputs.environment }}
|
|
STG2_DOCS_BLOCK_ID: ${{ secrets.DEV_DOCS_BLOCK_ID }}
|
|
STG2_DOCS_PAGE_ID: ${{ secrets.DEV_DOCS_PAGE_ID }}
|
|
STG_DOCS_BLOCK_ID: ${{ secrets.STG_DOCS_BLOCK_ID }}
|
|
STG_DOCS_PAGE_ID: ${{ secrets.STG_DOCS_PAGE_ID }}
|
|
PRD_DOCS_BLOCK_ID: ${{ secrets.PRD_DOCS_BLOCK_ID }}
|
|
PRD_DOCS_PAGE_ID: ${{ secrets.PRD_DOCS_PAGE_ID }}
|
|
shell: bash
|
|
run: |
|
|
if [ $ENV == "stg2" ]; then
|
|
echo "block_id=$STG2_DOCS_BLOCK_ID" >> $GITHUB_OUTPUT
|
|
echo "page_id=$STG2_DOCS_PAGE_ID" >> $GITHUB_OUTPUT
|
|
elif [ $ENV == "stg" ]; then
|
|
echo "block_id=$STG_DOCS_BLOCK_ID" >> $GITHUB_OUTPUT
|
|
echo "page_id=$STG_DOCS_PAGE_ID" >> $GITHUB_OUTPUT
|
|
elif [ $ENV == "prd" ]; then
|
|
echo "block_id=$PRD_DOCS_BLOCK_ID" >> $GITHUB_OUTPUT
|
|
echo "page_id=$PRD_DOCS_PAGE_ID" >> $GITHUB_OUTPUT
|
|
fi
|
|
id: extract_docs_info
|
|
|
|
- name: Generate API docs
|
|
env:
|
|
DOCS_URL: ${{ secrets.DOCS_URL }}
|
|
DOCS_API_TOKEN: ${{ secrets.DOCS_API_TOKEN }}
|
|
DOCS_PAGE_ID: ${{ steps.extract_docs_info.outputs.page_id }}
|
|
DOCS_BLOCK_ID: ${{ steps.extract_docs_info.outputs.block_id }}
|
|
EVENT: ${{ github.event_name }}
|
|
RELEASE_VERSION: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
run: |
|
|
if [ ${EVENT} != "workflow_dispatch" ]; then
|
|
sed -i -E "s/(\"title\": )\"Maestro.+\"/\1\"Maestro - $RELEASE_VERSION\"/g" docsfera.json
|
|
fi
|
|
sed -i -e "s/\${DOCS_API_TOKEN}/$DOCS_API_TOKEN/g" docsfera.json
|
|
sed -i -e "s/\${DOCS_PAGE_ID}/$DOCS_PAGE_ID/g" docsfera.json
|
|
sed -i -e "s/\${DOCS_BLOCK_ID}/$DOCS_BLOCK_ID/g" docsfera.json
|
|
curl -X POST -H 'Content-Type: application/json' -d @docsfera.json $DOCS_URL
|
|
|
|
- name: Generate External API docs
|
|
env:
|
|
DOCS_URL: ${{ secrets.DOCS_URL }}
|
|
DOCS_API_TOKEN: ${{ secrets.DOCS_API_TOKEN }}
|
|
DOCS_PAGE_ID: ${{secrets.EXTERNAL_DOCS_PAGE_ID}}
|
|
DOCS_BLOCK_ID: ${{secrets.EXTERNAL_DOCS_BLOCK_ID}}
|
|
EVENT: ${{ github.event_name }}
|
|
RELEASE_VERSION: ${{ needs.semantic_release.outputs.new_release_version }}
|
|
run: |
|
|
if [ ${EVENT} != "workflow_dispatch" ]; then
|
|
sed -i -E "s/(\"title\": )\"Maestro.+\"/\1\"Maestro - $RELEASE_VERSION\"/g" docsfera.external.json
|
|
fi
|
|
sed -i -e "s/\${DOCS_API_TOKEN}/$DOCS_API_TOKEN/g" docsfera.external.json
|
|
sed -i -e "s/\${DOCS_PAGE_ID}/$DOCS_PAGE_ID/g" docsfera.external.json
|
|
sed -i -e "s/\${DOCS_BLOCK_ID}/$DOCS_BLOCK_ID/g" docsfera.external.json
|
|
curl -X POST -H 'Content-Type: application/json' -d @docsfera.external.json $DOCS_URL
|