|
| 1 | +name: "Deploy to EKS" |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - "main" |
| 6 | + release: |
| 7 | + types: |
| 8 | + - "created" |
| 9 | + |
| 10 | +jobs: |
| 11 | + deploy: |
| 12 | + name: "Setup, Build, Publish, and Deploy" |
| 13 | + runs-on: ubuntu-latest |
| 14 | + environment: "preview" |
| 15 | + env: |
| 16 | + # Infrastructure configuration |
| 17 | + EKS_CLUSTER: "war-eks-cluster" |
| 18 | + EKS_NAMESPACE: "prod" |
| 19 | + AWS_REGION: "eu-west-2" |
| 20 | + RELEASE_NAME: "watchflow-dev-landing" |
| 21 | + PUBLIC_DOMAIN: "watchflow.dev" |
| 22 | + ECR_REPOSITORY: "watchflow_dev_landing" |
| 23 | + |
| 24 | + # Certificate configuration |
| 25 | + CERT_ISSUER_NAME: "war-cert-issuer" |
| 26 | + |
| 27 | + # Application configuration |
| 28 | + PORT: "5500" |
| 29 | + HELM_PATH: "helm-chart" |
| 30 | + JOB_STATUS: "succeeded" |
| 31 | + |
| 32 | + # AWS credentials |
| 33 | + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" |
| 34 | + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" |
| 35 | + |
| 36 | + |
| 37 | + permissions: |
| 38 | + contents: "read" |
| 39 | + id-token: "write" |
| 40 | + packages: "write" |
| 41 | + |
| 42 | + steps: |
| 43 | + # Checkout the repository code |
| 44 | + - name: "Checkout" |
| 45 | + uses: actions/checkout@v4 |
| 46 | + |
| 47 | + # Configure AWS credentials for EKS and ECR access |
| 48 | + - name: "Configure AWS credentials" |
| 49 | + uses: aws-actions/configure-aws-credentials@v4 |
| 50 | + with: |
| 51 | + aws-access-key-id: "${{ env.AWS_ACCESS_KEY_ID }}" |
| 52 | + aws-secret-access-key: "${{ env.AWS_SECRET_ACCESS_KEY }}" |
| 53 | + aws-region: "${{ env.AWS_REGION }}" |
| 54 | + |
| 55 | + # Login to Amazon ECR to push Docker images |
| 56 | + - name: "Login to Amazon ECR" |
| 57 | + id: login-ecr |
| 58 | + uses: aws-actions/amazon-ecr-login@v2 |
| 59 | + |
| 60 | + # Build and push Docker image to ECR |
| 61 | + - name: "Build, tag, and push image to Amazon ECR" |
| 62 | + id: ecr-push |
| 63 | + env: |
| 64 | + ECR_REGISTRY: "${{ steps.login-ecr.outputs.registry }}" |
| 65 | + ECR_REPOSITORY: "${{ env.ECR_REPOSITORY }}" |
| 66 | + IMAGE_TAG: "${{ github.sha }}" |
| 67 | + run: | |
| 68 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 69 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 70 | + echo "image_repository=$ECR_REGISTRY/$ECR_REPOSITORY" >> $GITHUB_OUTPUT |
| 71 | +
|
| 72 | + # Update kubeconfig to connect to EKS cluster |
| 73 | + - name: "Update kubeconfig" |
| 74 | + run: aws eks update-kubeconfig --region "${{ env.AWS_REGION }}" --name "${{ env.EKS_CLUSTER }}" |
| 75 | + |
| 76 | + # Deploy application using Helm chart |
| 77 | + - name: "Install or upgrade helm chart" |
| 78 | + env: |
| 79 | + IMAGE_TAG: "${{ github.sha }}" |
| 80 | + IMAGE_REPOSITORY: "${{ steps.ecr-push.outputs.image_repository }}" |
| 81 | + run: |- |
| 82 | + helm upgrade "${{ env.RELEASE_NAME }}" "${{ env.HELM_PATH }}" --namespace "${{ env.EKS_NAMESPACE }}" \ |
| 83 | + --values "${{ env.HELM_PATH }}/values.yaml" \ |
| 84 | + --set image.repository=${{ env.IMAGE_REPOSITORY }} \ |
| 85 | + --set image.tag=${{ env.IMAGE_TAG }} \ |
| 86 | + --set ingress.hosts[0].host=${{ env.PUBLIC_DOMAIN }} \ |
| 87 | + --set ingress.tls[0].secretName=${{ env.RELEASE_NAME }}-tls \ |
| 88 | + --set ingress.tls[0].hosts[0]=${{ env.PUBLIC_DOMAIN }} \ |
| 89 | + --set cert.enabled=true \ |
| 90 | + --set cert.tls.secretName=${{ env.RELEASE_NAME }}-tls \ |
| 91 | + --set cert.issuerRef.name=${{ env.CERT_ISSUER_NAME }} \ |
| 92 | + --set cert.issuerRef.kind=ClusterIssuer \ |
| 93 | + --set cert.commonName=${{ env.PUBLIC_DOMAIN }} \ |
| 94 | + --set cert.dnsNames.hosts[0]=${{ env.PUBLIC_DOMAIN }} \ |
| 95 | + --set service.port=${{ env.PORT }} \ |
| 96 | + --install |
| 97 | + shell: bash |
| 98 | + |
| 99 | + # Set status to failed on any's step failure |
| 100 | + - name: "Set status to failed on any's step failure" |
| 101 | + if: ${{ failure() }} |
| 102 | + run: echo "JOB_STATUS=failed" >> $GITHUB_ENV |
| 103 | + |
| 104 | + # Exit with error on failure |
| 105 | + - name: "Exit with error on failure" |
| 106 | + if: ${{ failure() }} |
| 107 | + run: exit 1 |
0 commit comments