2222env :
2323 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
2424 ISSUE_NUMBER : ${{ github.event.issue.number }}
25+ AWS_ACCOUNT_ID : ${{ secrets.AWS_ACCOUNT_ID }}
26+ AWS_ACCESS_KEY_ID : ${{ secrets.AWS_ACCESS_KEY_ID }}
27+ AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
28+ AWS_REGION : ${{ secrets.AWS_REGION }}
2529
2630jobs :
2731 retrieve-project :
2832 runs-on : ubuntu-20.04
2933 if : contains(join(github.event.label.name, ', '), 'microflow-start')
3034 outputs :
31- PROJECT : ${{ steps.parsing.outputs.repo_name }}
35+ PROJECT : ${{ steps.parsing.outputs.repo }}
36+ PROJECT_NAME : ${{ steps.parsing.outputs.repo_name }}
3237 JDK : ${{ steps.parsing.outputs.jdk_version }}
38+ DEPLOY : ${{ steps.parsing.outputs.deploy }}
3339 steps :
3440 - name : Get information from body
3541 id : parsing
@@ -41,11 +47,15 @@ jobs:
4147 import re
4248 import sys
4349 rawform = str(os.environ['BODY'])
44- repo_name = re.search(r"(### GitHub Repository\s*)(.*)", rawform).group(2)
50+ repo = re.search(r"(### GitHub Repository\s*)(.*)", rawform).group(2)
51+ repo_name = repo.split('/')[1]
4552 jdk_version = re.search(r"(### Java version used in the project\s*)(.*)", rawform).group(2)
53+ deploy = re.search(r"(### Deploy the project\s*)(.*)", rawform).group(2)
4654
55+ print(f'::set-output name=repo::{repo}')
4756 print(f'::set-output name=repo_name::{repo_name}')
4857 print(f'::set-output name=jdk_version::{jdk_version}')
58+ print(f'::set-output name=deploy::{deploy}')
4959
5060 - name : Return error
5161 if : ${{ always() && steps.parsing.outcome == 'failure' }}
6878 uses : actions/checkout@v3
6979 id : checkout
7080 with :
71- repository : ${{ steps.parsing.outputs.repo_name }}
81+ repository : ${{ steps.parsing.outputs.repo }}
7282 token : ${{ secrets.GITHUB_TOKEN }}
7383
7484 - name : Return error
@@ -272,7 +282,7 @@ jobs:
272282
273283 web-update :
274284 runs-on : ubuntu-20.04
275- needs : [retrieve-project, sonarqube-scanner, checkstyle-spotbugs]
285+ needs : [sonarqube-scanner, checkstyle-spotbugs]
276286 steps :
277287 - name : Checkout to ghpages branch
278288 uses : actions/checkout@v3
@@ -355,3 +365,189 @@ jobs:
355365 run : |
356366 gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
357367 --body "#### :heavy_check_mark: The project has build successfully"
368+
369+ - uses : actions/upload-artifact@v3
370+ with :
371+ name : packaged_project
372+ path : .
373+
374+ deploy :
375+ runs-on : ubuntu-20.04
376+ needs : [retrieve-project, build-test]
377+ if : ${{ needs.retrieve-project.outputs.DEPLOY == '- [X] yes' }}
378+ steps :
379+ - name : Get project
380+ id : get-project
381+ uses : actions/download-artifact@v3
382+ with :
383+ name : packaged_project
384+
385+ - name : Configure AWS credentials
386+ id : aws-login
387+ uses : aws-actions/configure-aws-credentials@v1
388+ with :
389+ aws-access-key-id : ${{ env.AWS_ACCESS_KEY_ID }}
390+ aws-secret-access-key : ${{ env.AWS_SECRET_ACCESS_KEY }}
391+ aws-region : ${{ env.AWS_REGION }}
392+
393+ - name : Return error
394+ if : ${{ always() && steps.aws-login.outcome == 'failure' }}
395+ run : |
396+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
397+ --body "#### :x: Could not login in AWS
398+ Check the used aws credentials.
399+
400+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
401+
402+ - name : Return success
403+ if : ${{ always() && steps.aws-login.outcome == 'success' }}
404+ run : |
405+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
406+ --body "#### :heavy_check_mark: Logged in AWS successfully"
407+
408+ - name : Login to Amazon ECR
409+ id : login-ecr
410+ uses : aws-actions/amazon-ecr-login@v1
411+
412+ - name : Return error
413+ if : ${{ always() && steps.login-ecr.outcome == 'failure' }}
414+ run : |
415+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
416+ --body "#### :x: Could not login in AWS ECR
417+ Check the used aws credentials.
418+
419+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
420+
421+ - name : Return success
422+ if : ${{ always() && steps.login-ecr.outcome == 'success' }}
423+ run : |
424+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
425+ --body "#### :heavy_check_mark: Logged in AWS ECR successfully"
426+
427+ - name : Build Images
428+ id : image-build
429+ run : |
430+ cd project
431+ docker-compose build
432+
433+ - name : Return error
434+ if : ${{ always() && steps.image-build.outcome == 'failure' }}
435+ run : |
436+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
437+ --body "#### :x: The docker images that compose the project could not be build
438+ A docker-compose file is needed in the root of the project in order to build it.
439+
440+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
441+
442+ - name : Return success
443+ if : ${{ always() && steps.image-build.outcome == 'success' }}
444+ run : |
445+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
446+ --body "#### :heavy_check_mark: Docker images for every service were built successfully"
447+
448+ - name : Create ECR registries and push images
449+ id : create-ecr
450+ env :
451+ REGISTRY : ${{ steps.login-ecr.outputs.registry }}
452+ AWS_DEFAULT_REGION : $AWS_REGION
453+ run : |
454+ cd project
455+ docker image ls -f "reference=project_*" | awk '(NR>1) { print $1 }' > image_names
456+ cat image_names | xargs -I{} docker tag {} $REGISTRY/{}:latest
457+ cat image_names | xargs -I{} aws ecr create-repository \
458+ --repository-name {} \
459+ --image-scanning-configuration scanOnPush=true \
460+ --region $AWS_REGION
461+ cat image_names | xargs -I{} docker push $REGISTRY/{}:latest
462+
463+ - name : Return error
464+ if : ${{ always() && steps.create-ecr.outcome == 'failure' }}
465+ run : |
466+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
467+ --body "#### :x: The docker images that compose the project could not be pushed to the AWS ECR
468+ Check the following requirements:
469+ - The used aws credentials have rights to create AWS ECR repositories.
470+ - The aws region used is the one where you want to deploy the project and the one where the cluster is located.
471+
472+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
473+
474+ - name : Return success
475+ if : ${{ always() && steps.create-ecr.outcome == 'success' }}
476+ run : |
477+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
478+ --body "#### :heavy_check_mark: Docker images were pushed to the private ECR registry successfully"
479+
480+ - name : Tag images with ECR prefix
481+ id : tag-images
482+ env :
483+ REGISTRY : ${{ steps.login-ecr.outputs.registry }}
484+ run : |
485+ sed -i "s|${{ needs.retrieve-project.outputs.PROJECT_NAME }}|$REGISTRY/project|g" project/kubernetes-manifest.yaml
486+
487+ - name : Return error
488+ if : ${{ always() && steps.tag-images.outcome == 'failure' }}
489+ run : |
490+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
491+ --body "#### :x: The docker images that use the Kubernetes manifest could not be tagged with the ECR images
492+ Check the following requirements:
493+ - A Kubernetes manifest using the images composed by docker-compose exist in the root of the project (the image_name:tag should be the same).
494+
495+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
496+
497+ - name : Return success
498+ if : ${{ always() && steps.tag-images.outcome == 'success' }}
499+ run : |
500+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
501+ --body "#### :heavy_check_mark: Docker images were updated in the Kubernetes manifest successfully"
502+
503+ - name : Create VPC for EKS
504+ id : eks-vpc
505+ uses :
aws-actions/[email protected] 506+ with :
507+ name : ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-stack
508+ template : https://s3.us-west-2.amazonaws.com/amazon-eks/cloudformation/2020-10-29/amazon-eks-vpc-private-subnets.yaml
509+ no-fail-on-empty-changeset : " 1"
510+
511+ - name : Create Kubernetes cluster
512+ id : eks-cluster
513+ run : |
514+
515+ rawoutput=$(aws cloudformation describe-stacks --stack-name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-stack |jq --raw-output '.Stacks[].Outputs[]')
516+ securitygroup=$(echo $rawoutput | jq --raw-output 'select(.OutputKey == "SecurityGroups").OutputValue')
517+ subnetids=$(echo $rawoutput | jq --raw-output 'select(.OutputKey == "SubnetIds").OutputValue')
518+
519+ aws eks create-cluster --region ${{ env.AWS_REGION }} --name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-cluster --kubernetes-version 1.22 \
520+ --role-arn arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/eksClusterRoleMicroflow \
521+ --resources-vpc-config subnetIds=$subnetids,securityGroupIds=$securitygroup
522+
523+ aws eks wait cluster-active --name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-cluster
524+
525+ aws eks create-nodegroup --cluster-name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-cluster --nodegroup-name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-nodegroup \
526+ --subnets $(echo $subnetids | sed 's/,/ /g') --node-role arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/microflowClusterNodeGroupsRole \
527+ --scaling-config minSize=2,maxSize=3,desiredSize=3 --remote-access ec2SshKey=microflow-key-pair
528+
529+ - name : Deploy to Kubernetes cluster
530+ id : deploy-k8s
531+ run : |
532+
533+ aws eks wait nodegroup-active --nodegroup-name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-nodegroup --cluster-name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-cluster
534+
535+ aws eks update-kubeconfig --region ${{ env.AWS_REGION }} --name ${{ needs.retrieve-project.outputs.PROJECT_NAME }}-cluster
536+ kubectl apply -f project/kubernetes-manifest.yaml
537+
538+ - name : Return error
539+ if : ${{ always() && steps.deploy-k8s.outcome == 'failure' }}
540+ run : |
541+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
542+ --body "#### :x: The Kubernetes manifest could not be applied
543+ Check the following requirements:
544+ - Check the kubernetes manifest file, that should be named, **kubernetes-manifest.yml**
545+
546+ See the [workflow log](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details."
547+
548+ - name : Return success
549+ if : ${{ always() && steps.deploy-k8s.outcome == 'success' }}
550+ run : |
551+ gh issue comment -R "${{github.repository}}" "$ISSUE_NUMBER" \
552+ --body "#### :heavy_check_mark: The project was successfully deployed to the AWS EKS cluster"
553+
0 commit comments