name: Deploy on: push: branches: - main - beta workflow_dispatch: inputs: environment: description: "Deployment environment" required: true type: choice options: - stg - 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 elif [ ${GITHUB_REF} == "refs/heads/beta" ]; then echo "environment=stg" >> $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@v3 id: semantic with: extra_plugins: | conventional-changelog-eslint@4.0.0 branches: | [ 'main', { name: 'alpha', prerelease: true }, { name: 'beta', prerelease: true } ] env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} build_ecr_image: if: ${{ github.event_name == 'workflow_dispatch' || needs.semantic_release.outputs.new_release_published == 'true' }} needs: [extract_environment, semantic_release] runs-on: [self-hosted, "prd"] 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@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: ${{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 helmfile-deploy: needs: [extract_environment, semantic_release, build_ecr_image] runs-on: [self-hosted, "prd-azure"] steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Helm uses: azure/setup-helm@v1 with: version: 'v3.9.0' - name: Install Azure ClI run: | curl -sL https://aka.ms/InstallAzureCLIDeb | bash - uses: azure/login@v2 with: creds: '{"clientId":"${{ secrets.ARM_CLIENT_ID }}","clientSecret":"${{ secrets.ARM_CLIENT_SECRET }}","subscriptionId":"${{ secrets.ARM_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.ARM_TENANT_ID }}"}' - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.8' - name: Install Helmfile run: | wget https://github.com/helmfile/helmfile/releases/download/v0.148.0/helmfile_0.148.0_linux_amd64.tar.gz tar -xzf helmfile_0.148.0_linux_amd64.tar.gz mv helmfile /usr/local/bin/ helmfile --version - name: Install Helm Diff Plugin run: helm plugin install https://github.com/databus23/helm-diff || true - name: Setup kubectl uses: azure/setup-kubectl@v1 with: version: 'v1.30.1' - name: Authenticate with cluster env: CLUSTER_NAME: platform-${{ needs.extract_environment.outputs.environment }} run: az aks get-credentials --resource-group dadosfera-prd --name ${CLUSTER_NAME} --overwrite-existing - name: Run Helmfile Apply env: ENV: ${{ needs.extract_environment.outputs.environment }} IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }} run: helmfile -f helmfiles/${ENV}.yaml sync --set image.tag=$IMAGE_TAG