From 3955b0a1480b2d97bda679ca6228b07eb185e534 Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Fri, 25 Aug 2023 12:38:41 +0200 Subject: [PATCH] added git actions --- .../.github/actions/image_builder/action.yaml | 35 +++++++ level_2/.github/workflows/cd.yaml | 76 ++++++++++++++ level_2/.github/workflows/cd_prd.yaml | 99 +++++++++++++++++++ level_2/.github/workflows/ci.yaml | 25 +++++ level_2/bin/dockerize | 36 +++++++ 5 files changed, 271 insertions(+) create mode 100644 level_2/.github/actions/image_builder/action.yaml create mode 100644 level_2/.github/workflows/cd.yaml create mode 100644 level_2/.github/workflows/cd_prd.yaml create mode 100644 level_2/.github/workflows/ci.yaml create mode 100755 level_2/bin/dockerize diff --git a/level_2/.github/actions/image_builder/action.yaml b/level_2/.github/actions/image_builder/action.yaml new file mode 100644 index 000000000..ad516ca1d --- /dev/null +++ b/level_2/.github/actions/image_builder/action.yaml @@ -0,0 +1,35 @@ +name: 'Build Docker images for PromethAI' +description: 'Build PromethAI-related Docker images and push to the Docker registry (AWS ECR)' +inputs: + stage: + description: 'The stage of the pipeline, such as "dev" or "prd", for the PromethAI app' + required: true + aws_account_id: + description: 'The AWS account ID for the PromethAI app' + required: true + should_publish: + description: 'Whether to publish the PromethAI Docker image to AWS ECR; should be either "true" or "false"' + required: true + ecr_image_repo_name: + description: 'The Docker image ECR repository name for the PromethAI app, such as "workflows"' + required: true + dockerfile_location: + description: 'The directory location of the Dockerfile for the PromethAI app' + required: true + +runs: + using: "composite" + steps: + - name: Build PromethAI App Docker image + shell: bash + env: + STAGE: ${{ inputs.stage }} + run: | + export SHA_SHORT="$(git rev-parse --short HEAD)" + export CUR_DATE="$(date +%Y%m%d%H%M%S)" + export VERSION="${{ inputs.stage }}-$CUR_DATE-$SHA_SHORT" + export STAGE="${{ inputs.stage }}" + export APP_DIR="$PWD/${{ inputs.dockerfile_location }}" + image_name="${{ inputs.ecr_image_repo_name }}" docker_login="true" version="$VERSION" account="${{ inputs.aws_account_id }}" app_dir="$APP_DIR" publish="${{ inputs.should_publish }}" ./bin/dockerize + echo "Docker tag is: $VERSION" + echo $VERSION > /tmp/.DOCKER_IMAGE_VERSION diff --git a/level_2/.github/workflows/cd.yaml b/level_2/.github/workflows/cd.yaml new file mode 100644 index 000000000..4126999e3 --- /dev/null +++ b/level_2/.github/workflows/cd.yaml @@ -0,0 +1,76 @@ +name: Publishing promethai-backend Docker image + +on: + push: + branches: + - dev + - feature/* + paths-ignore: + - '**.md' + +env: + AWS_ROLE_DEV_CICD: "arn:aws:iam::463722570299:role/promethai-dev-base-role-github-ci-cd" + AWS_ACCOUNT_ID_DEV: "463722570299" + +jobs: + + publish_docker_to_ecr: + name: Publish Docker PromethAI image + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: Take code from repo + uses: actions/checkout@v3 + - name: Set environment variable for stage + id: set-env + run: | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then + echo "STAGE=prd" >> $GITHUB_ENV + echo "::set-output name=stage::prd" + else + echo "STAGE=dev" >> $GITHUB_ENV + echo "::set-output name=stage::dev" + fi + - name: Use output + run: echo "The stage is ${{ steps.set-env.outputs.stage }}" + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: ${{ env.AWS_ROLE_DEV_CICD }} + aws-region: eu-west-1 + - name: Create Docker image and push to ECR + uses: ./.github/actions/image_builder + id: generate-promethai-docker + with: + stage: dev + aws_account_id: ${{ env.AWS_ACCOUNT_ID_DEV }} + should_publish: true + ecr_image_repo_name: promethai-dev-backend-promethai-backend-memory + dockerfile_location: ./ + - name: Export Docker image tag + id: export-promethai-docker-tag + run: | + export DOCKER_TAG=$(cat /tmp/.DOCKER_IMAGE_VERSION) + echo "Docker tag is: $DOCKER_TAG" + echo "promethai_docker_tag_backend=$DOCKER_TAG" >> $GITHUB_OUTPUT + outputs: + promethai_docker_tag_backend: ${{ steps.export-promethai-docker-tag.outputs.promethai_docker_tag_backend }} + + apply_tf: + name: Trigger terraform apply workflow + runs-on: ubuntu-latest + needs: publish_docker_to_ecr + steps: + - name: TF apply workflow triggers step + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.PAT_FOR_CROSS_REPOS_CICD_TRIGGERING }} + script: | + await github.rest.actions.createWorkflowDispatch({ + owner: 'topoteretes', + repo: 'PromethAI-Infra', + workflow_id: 'terraform.apply.yml', + ref: 'main' + }) diff --git a/level_2/.github/workflows/cd_prd.yaml b/level_2/.github/workflows/cd_prd.yaml new file mode 100644 index 000000000..00f23e731 --- /dev/null +++ b/level_2/.github/workflows/cd_prd.yaml @@ -0,0 +1,99 @@ +on: + push: + branches: + - main + paths-ignore: + - '**.md' + - 'examples/**' +name: Publishing promethai-backend Docker image to prd ECR + +env: + AWS_ROLE_DEV_CICD: "arn:aws:iam::463722570299:role/promethai-dev-base-role-github-ci-cd" + AWS_ACCOUNT_ID_DEV: "463722570299" + ENVIRONMENT: prd + +jobs: + + publish_docker_to_ecr: + name: Publish Docker PromethAI image + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: Take code from repo + uses: actions/checkout@v3 + - name: Set environment variable for stage + id: set-env + run: | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then + echo "STAGE=prd" >> $GITHUB_ENV + echo "::set-output name=stage::prd" + else + echo "STAGE=dev" >> $GITHUB_ENV + echo "::set-output name=stage::dev" + fi + - name: Use output + run: echo "The stage is ${{ steps.set-env.outputs.stage }}" + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: ${{ env.AWS_ROLE_DEV_CICD }} + aws-region: eu-west-1 + - name: Create Docker image and push to ECR + uses: ./.github/actions/image_builder + id: generate-promethai-docker + with: + stage: prd + aws_account_id: ${{ env.AWS_ACCOUNT_ID_DEV }} + should_publish: true + ecr_image_repo_name: promethai-prd-backend-promethai-backend-memory + dockerfile_location: ./ + - name: Export Docker image tag + id: export-promethai-docker-tag + run: | + export DOCKER_TAG=$(cat /tmp/.DOCKER_IMAGE_VERSION) + echo "Docker tag is: $DOCKER_TAG" + echo "promethai_docker_tag_backend=$DOCKER_TAG" >> $GITHUB_OUTPUT + +# - name: Create Tag and Release +# runs-on: ubuntu-latest +# uses: actions/checkout@v3 +# needs: publish_docker_to_ecr # ensure this job runs after Docker image is pushed +# steps: +# - name: Check out code +# uses: actions/checkout@v3 +# - name: Bump version and push tag +# id: bump_version_and_push_tag +# uses: anothrNick/github-tag-action@1.34.0 +# env: +# GITHUB_TOKEN: ${{ secrets.PAT_FOR_CROSS_REPOS_CICD_TRIGGERING }} +# WITH_V: true +# DEFAULT_BUMP: 'minor' # or 'minor' or 'major' +# - name: Create Release +# id: create_release +# uses: actions/create-release@v1 +# env: +# GITHUB_TOKEN: ${{ secrets.PAT_FOR_CROSS_REPOS_CICD_TRIGGERING }} +# with: +# tag_name: ${{ steps.bump_version_and_push_tag.outputs.tag }} +# release_name: Release ${{ steps.bump_version_and_push_tag.outputs.tag }} + outputs: + promethai_docker_tag_backend: ${{ steps.export-promethai-docker-tag.outputs.promethai_docker_tag_backend }} + + apply_tf: + name: Trigger terraform apply workflow + runs-on: ubuntu-latest + needs: publish_docker_to_ecr + steps: + - name: TF apply workflow triggers step + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.PAT_FOR_CROSS_REPOS_CICD_TRIGGERING }} + script: | + await github.rest.actions.createWorkflowDispatch({ + owner: 'topoteretes', + repo: 'PromethAI-Infra', + workflow_id: 'terraform.apply.yml', + ref: 'main' + }) diff --git a/level_2/.github/workflows/ci.yaml b/level_2/.github/workflows/ci.yaml new file mode 100644 index 000000000..dba210ac0 --- /dev/null +++ b/level_2/.github/workflows/ci.yaml @@ -0,0 +1,25 @@ +name: Test build docker image for PromethAI backend app + +on: pull_request + +env: + AWS_ACCOUNT_ID_DEV: "463722570299" + +jobs: + + build_docker: + name: Build PromethAI Backend Docker App Image + runs-on: ubuntu-latest + steps: + - name: Check out PromethAI code + uses: actions/checkout@v3 + + - name: Build PromethAI backend Docker image tag + id: backend-docker-tag + run: | + export SHA_SHORT="$(git rev-parse --short HEAD)" + export CUR_DATE="$(date +%Y%m%d%H%M%S)" + export VERSION="dev-$CUR_DATE-$SHA_SHORT" + image_name="backend" docker_login="false" version="$VERSION" account="${{ env.AWS_ACCOUNT_ID_DEV }}" app_dir="backend" publish="false" ./bin/dockerize + export DOCKER_TAG=$(cat /tmp/.DOCKER_IMAGE_VERSION) + echo "Successfully built PromethAI backend Docker tag is: $DOCKER_TAG" diff --git a/level_2/bin/dockerize b/level_2/bin/dockerize new file mode 100755 index 000000000..30cfede3c --- /dev/null +++ b/level_2/bin/dockerize @@ -0,0 +1,36 @@ +set -euo pipefail + +AWS_REGION=${region:-eu-west-1} +AWS_DEPLOYMENT_ACCOUNT=${account:-463722570299} +AWS_REPOSITORY=${repo:-"${AWS_DEPLOYMENT_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com"} + +STAGE=${stage:-"dev"} +SHA_SHORT="$(git rev-parse --short HEAD)" +CUR_DATE="$(date +%Y%m%d%H%M%S)" +VERSION="$STAGE-$CUR_DATE-$SHA_SHORT" +IMAGE_NAME=${image_name:-promethai-${STAGE}-promethai-backend} + +REPO_NAME="${AWS_REPOSITORY}/${IMAGE_NAME}" +FULL_IMAGE_NAME="${REPO_NAME}:${VERSION}" +APP_DIR=${app_dir:-"."} + +PUBLISH=${publish:-false} + +echo "Building docker image ${FULL_IMAGE_NAME} located in dir ${app_dir}" + +pushd "${APP_DIR}" && + docker buildx build --platform linux/amd64 \ + --build-arg STAGE=${STAGE} \ + -t "${FULL_IMAGE_NAME}" . && + echo "${VERSION}" >/tmp/.DOCKER_IMAGE_VERSION && + echo "Successfully built docker image ${FULL_IMAGE_NAME}" + +if [ "${PUBLISH}" = true ]; then + echo "Pushing docker image ${FULL_IMAGE_NAME} to ECR repository to AWS account ${AWS_DEPLOYMENT_ACCOUNT}" + if [ "${PUBLISH}" = true ]; then + echo "logging in" + aws ecr get-login-password --region "${AWS_REGION}" | docker login --username AWS --password-stdin "${AWS_REPOSITORY}" + fi + docker push "${FULL_IMAGE_NAME}" && + echo "Successfully pushed docker image ${FULL_IMAGE_NAME} to ECR repository" +fi \ No newline at end of file