Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.4 kB
2
Indexable
Never
name: Deploy Needy New

on:
  workflow_dispatch:
    inputs:
      environment:
        type: choice
        description: Choose the environment to deploy to
        options:
          - stage
          - prod
          - both
        default: stage
  push:
    branches:
      - terraform


permissions:
  id-token: write
  contents: read

env:
  STAGE_CLUSTER_NAME: staging
  PROD_CLUSTER_NAME: production
  AWS_DEFAULT_REGION: us-east-1
  PROD_PROJECT_NAME: needy-prod
  PROD_VALUES_FILE: prod_values.yaml
  STAGE_PROJECT_NAME: needy-stage
  STAGE_VALUES_FILE: stage_values.yaml

jobs:
  build:
    name: Build Image
    runs-on: ubuntu-latest

    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v3
        with:
          role-to-assume: arn:aws:iam::767397862679:role/GithubActionRole
          role-session-name: GitHub_to_AWS_via_FederatedOIDC
          aws-region: us-east-1

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@master

      - name: Docker cache layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-single-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-single-buildx

      - name: Set release version
        run: echo "RELEASE_REVISION=$(date +'%s')" >> $GITHUB_ENV


      - name: Build & Push Image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          MICROSERVICE_RELEASE_IMAGE: ${{ steps.login-ecr.outputs.registry }}/microservice:${{ env.RELEASE_REVISION }}
          WEBAPP_RELEASE_IMAGE: ${{ steps.login-ecr.outputs.registry }}/webapp:${{ env.RELEASE_REVISION }}
        run: |

          echo "microservice image: ${{ env.MICROSERVICE_RELEASE_IMAGE }}"
          docker buildx create --use

          docker buildx build \
            --platform linux/amd64 \
            --tag ${{ env.MICROSERVICE_RELEASE_IMAGE }} \
            --file ./src/needy/services/Dockerfile \
            --cache-from=type=local,src=/tmp/.buildx-cache \
            --cache-to=type=local,dest=/tmp/.buildx-cache-new \
            . --push

          docker buildx build \
            --platform linux/amd64 \
            --tag ${{ env.WEBAPP_RELEASE_IMAGE }} \
            --file ./services/webapp/Dockerfile \
            --cache-from=type=local,src=/tmp/.buildx-cache \
            --cache-to=type=local,dest=/tmp/.buildx-cache-new \
            . --push

          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache

      - name: kubeconfig
        run: |
          aws eks update-kubeconfig --name ${{ env.STAGE_CLUSTER_NAME }} --region ${{ env.AWS_DEFAULT_REGION }} --kubeconfig ./kubeconfig
          echo 'KUBE_CONFIG_DATA<<EOF' >> $GITHUB_ENV
          echo $(cat ./kubeconfig | base64) >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV

      - name: helm deploy
        uses: koslib/helm-eks-action@master
        env:
          KUBE_CONFIG_DATA: ${{ env.KUBE_CONFIG_DATA }}
        with:
          command: cd deploy/ && helm upgrade ${{ env.PROD_PROJECT_NAME }} . --values ${{env.PROD_VALUES_FILE}} --set global.needyVersion=${{ env.RELEASE_REVISION }}
Leave a Comment