|
1 | 1 | name: Frontend Build and Deploy |
2 | 2 | on: |
3 | | - workflow_dispatch: |
| 3 | + workflow_dispatch: # Manual trigger from GitHub Actions UI |
4 | 4 | inputs: |
5 | 5 | env: |
6 | 6 | type: choice |
7 | | - description: 'AWS Incubator Env' |
8 | | - options: |
9 | | - - dev |
10 | | - - prod |
| 7 | + description: "AWS Incubator Env" |
| 8 | + options: # Selectable environment options |
| 9 | + - dev |
| 10 | + - prod |
11 | 11 | ref: |
12 | | - description: 'Branch, Tag, or SHA' |
| 12 | + description: "Branch, Tag, or SHA" # Code reference to deploy |
13 | 13 | required: true |
14 | 14 | env: |
| 15 | + # Target ECS cluster name |
15 | 16 | AWS_SHARED_CLUSTER: incubator-prod |
16 | | - AWS_APP_NAME: vrms-client |
| 17 | + # Application name for tagging and service |
| 18 | + AWS_APP_NAME: vrms-frontend |
| 19 | + # AWS region for deployment |
17 | 20 | AWS_REGION: us-west-2 |
18 | | - DOCKERFILE: client/Dockerfile.prod |
| 21 | + # Dockerfile used for build (located in client/) |
| 22 | + DOCKERFILE: Dockerfile.prod |
| 23 | + # Path to frontend source and Dockerfile |
19 | 24 | DOCKER_PATH: client |
20 | 25 | jobs: |
21 | 26 | setup_env: |
22 | | - name: Set-up environment |
| 27 | + name: Set-up environment |
23 | 28 | runs-on: ubuntu-latest |
24 | 29 | steps: |
25 | | - - name: Debug Action |
26 | | - uses: hmarr/debug-action@v2 |
27 | | - - name: Checkout |
28 | | - uses: actions/checkout@v3 |
29 | | - with: |
30 | | - ref: ${{ github.event.inputs.ref }} |
31 | | - - name: Set AWS Env & Image Tag per workflow |
32 | | - run: | |
33 | | - SHORT_SHA=$(git rev-parse --short HEAD) |
34 | | - if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then |
35 | | - INPUT_ENV=${{ github.event.inputs.env }}; INPUT_REF=${{ github.event.inputs.ref }} |
36 | | - echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV |
37 | | - echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV |
38 | | - fi |
| 30 | + - name: Debug Action |
| 31 | + uses: hmarr/debug-action@v2 # Prints debug info to logs |
| 32 | + - name: Checkout |
| 33 | + uses: actions/checkout@v3 # Checks out code at specified ref |
| 34 | + with: |
| 35 | + ref: ${{ github.event.inputs.ref }} # Uses user-specified ref |
| 36 | + # Get short SHA of current commit |
| 37 | + # Only run if triggered manually |
| 38 | + # Get environment input from workflow dispatch |
| 39 | + # Get ref input from workflow dispatch |
| 40 | + # Set AWS_APPENV for later steps |
| 41 | + # Set IMAGE_TAG for later steps |
| 42 | + - name: Set AWS Env & Image Tag per workflow |
| 43 | + run: | |
| 44 | + SHORT_SHA=$(git rev-parse --short HEAD) |
| 45 | + if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then |
| 46 | + INPUT_ENV=${{ github.event.inputs.env }}; INPUT_REF=${{ github.event.inputs.ref }} |
| 47 | + echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV |
| 48 | + echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV |
| 49 | + fi |
39 | 50 | outputs: |
40 | 51 | AWS_APPENV: ${{ env.AWS_APPENV }} |
41 | 52 | IMAGE_TAG: ${{ env.IMAGE_TAG }} |
42 | 53 | build: |
43 | 54 | name: Build & Push Docker Image |
44 | 55 | runs-on: ubuntu-latest |
45 | | - needs: [setup_env] |
| 56 | + permissions: |
| 57 | + id-token: write # Needed for OIDC authentication to AWS |
| 58 | + needs: [setup_env] # Waits for environment setup |
46 | 59 | steps: |
47 | | - - name: Checkout |
48 | | - uses: actions/checkout@v3 |
49 | | - with: |
50 | | - ref: ${{ github.event.inputs.ref }} |
51 | | - - name: Checkout |
52 | | - uses: actions/setup-node@v3 |
53 | | - with: |
54 | | - node-version: 18 |
55 | | - cache: 'npm' |
56 | | - - name: Configure AWS credentials |
57 | | - uses: aws-actions/configure-aws-credentials@v2 |
58 | | - with: |
59 | | - aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }} |
60 | | - aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }} |
61 | | - aws-region: ${{ env.AWS_REGION }} |
62 | | - - name: Login to Amazon ECR |
63 | | - id: login-ecr |
64 | | - uses: aws-actions/amazon-ecr-login@v1 |
65 | | - - name: Init Docker Cache |
66 | | - |
67 | | - with: |
68 | | - key: ${{ github.workflow }}-2-{hash} |
69 | | - restore-keys: | |
70 | | - ${{ github.workflow }}-2- |
71 | | - - name: Build & Push Image to ECR |
72 | | - uses: kciter/aws-ecr-action@v3 |
73 | | - with: |
74 | | - access_key_id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }} |
75 | | - secret_access_key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }} |
76 | | - account_id: ${{ secrets.INCUBATOR_AWS_ACCOUNT_ID }} |
77 | | - repo: ${{ needs.setup_env.outputs.AWS_APPENV }} |
78 | | - region: ${{ env.AWS_REGION }} |
79 | | - tags: latest,${{ needs.setup_env.outputs.IMAGE_TAG }} |
80 | | - dockerfile: ${{ env.DOCKERFILE }} |
81 | | - path: ${{ env.DOCKER_PATH }} |
| 60 | + - name: Checkout |
| 61 | + uses: actions/checkout@v3 # Checks out code at specified ref |
| 62 | + with: |
| 63 | + ref: ${{ github.event.inputs.ref }} |
| 64 | + - name: Setup Node.js |
| 65 | + uses: actions/setup-node@v3 # Sets up Node.js for build |
| 66 | + with: |
| 67 | + node-version: 18 # Uses Node.js v18 |
| 68 | + cache: "npm" # Enables npm caching |
| 69 | + - name: Configure AWS credentials |
| 70 | + uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI |
| 71 | + with: |
| 72 | + role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy |
| 73 | + role-session-name: incubator-cicd-vrms-gha # Session name for audit |
| 74 | + aws-region: us-west-2 # AWS region |
| 75 | + - name: Login to Amazon ECR |
| 76 | + id: login-ecr |
| 77 | + uses: aws-actions/amazon-ecr-login@v1 # Authenticates Docker to ECR |
| 78 | + - name: Build, tag, and push the image to Amazon ECR |
| 79 | + id: build-push-image |
| 80 | + env: |
| 81 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # ECR registry URL |
| 82 | + ECR_REPOSITORY: ${{ env.AWS_APP_NAME }} # ECR repo name |
| 83 | + # List files for debug |
| 84 | + # Enter frontend directory for Docker build context |
| 85 | + # Build Docker image using production Dockerfile |
| 86 | + # Tag image with short SHA |
| 87 | + # Tag image with environment (dev/prod) |
| 88 | + # Use current directory as build context |
| 89 | + # Push all tags for this image to ECR |
| 90 | + run: | |
| 91 | + ls |
| 92 | + cd ./${{ env.DOCKER_PATH }} |
| 93 | + docker build \ |
| 94 | + -f ${{ env.DOCKERFILE }} \ |
| 95 | + -t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ needs.setup_env.outputs.IMAGE_TAG }} \ |
| 96 | + -t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.event.inputs.env }} \ |
| 97 | + . |
| 98 | + docker image push --all-tags ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }} |
82 | 99 | deploy: |
83 | 100 | name: Deploy to AWS ECS |
84 | 101 | runs-on: ubuntu-latest |
85 | | - needs: [setup_env, build] |
| 102 | + needs: [setup_env, build] # Waits for setup and build jobs |
| 103 | + permissions: |
| 104 | + id-token: write # Needed for OIDC authentication to AWS |
86 | 105 | steps: |
87 | | - - name: Configure AWS credentials |
88 | | - uses: aws-actions/configure-aws-credentials@v2 |
89 | | - with: |
90 | | - aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }} |
91 | | - aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }} |
92 | | - aws-region: ${{ env.AWS_REGION }} |
93 | | - - name: Login to Amazon ECR |
94 | | - id: login-ecr |
95 | | - uses: aws-actions/amazon-ecr-login@v1 |
96 | | - - name: Pull Task Definition & write to file |
97 | | - id: aws-task-definition |
98 | | - run: | |
99 | | - aws ecs describe-task-definition \ |
100 | | - --task-definition ${{ needs.setup_env.outputs.AWS_APPENV }} \ |
101 | | - --query taskDefinition | \ |
102 | | - jq 'del(.taskDefinitionArn,.revision,.status,.registeredBy,.registeredAt,.compatibilities,.requiresAttributes)' > task-def.json |
103 | | - - name: Interpolate new Docker Image into Task Definition |
104 | | - id: task-definition |
105 | | - uses: aws-actions/amazon-ecs-render-task-definition@v1 |
106 | | - with: |
107 | | - task-definition: task-def.json |
108 | | - container-name: ${{ needs.setup_env.outputs.AWS_APPENV }} |
109 | | - image: ${{ steps.login-ecr.outputs.registry }}/${{ needs.setup_env.outputs.AWS_APPENV }}:${{ needs.setup_env.outputs.IMAGE_TAG }} |
110 | | - - name: Deploy Amazon ECS |
111 | | - uses: aws-actions/amazon-ecs-deploy-task-definition@v1 |
112 | | - with: |
113 | | - task-definition: ${{ steps.task-definition.outputs.task-definition }} |
114 | | - service: ${{ needs.setup_env.outputs.AWS_APPENV }} |
115 | | - cluster: ${{ env.AWS_SHARED_CLUSTER }} |
116 | | - wait-for-service-stability: true |
117 | | - wait-for-minutes: 5 minutes |
118 | | - |
| 106 | + - name: Configure AWS credentials |
| 107 | + uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI |
| 108 | + with: |
| 109 | + role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy |
| 110 | + role-session-name: incubator-cicd-vrms-gha # Session name for audit |
| 111 | + aws-region: us-west-2 # AWS region |
| 112 | + - name: Restart ECS Service |
| 113 | + id: redeploy-service |
| 114 | + env: |
| 115 | + SERVICE_NAME: ${{env.AWS_APP_NAME}}-${{ github.event.inputs.env }} # ECS service name |
| 116 | + # Force a new deployment of the ECS service to use the latest Docker image |
| 117 | + run: | |
| 118 | + aws ecs update-service --force-new-deployment --service $SERVICE_NAME --cluster $AWS_SHARED_CLUSTER |
0 commit comments