Skip to content

Latest commit

 

History

History
171 lines (159 loc) · 5.65 KB

deployment-amazon-codecatalyst.md

File metadata and controls

171 lines (159 loc) · 5.65 KB

Golang Deployment - Deployment with Amazon CodeCatalyst

Kubernetes Deployment for Simple Golang API

goreport all contributors tags docker pulls download all view clone issues pull requests forks stars license


Workflow Action

Name: Workflow_GitHubActions_Deployment
SchemaVersion: "1.0"

# Optional - Set automatic triggers.
Triggers:
  - Type: PUSH
    Branches:
      - master
      - "release/*"
  - Type: PULLREQUEST
    Branches:
      - "features/*"
      - "bugfix/*"
      - "hotfix/*"
    Events:
      - OPEN
      - REVISION

# Required - Define action configurations.
Actions:
  GitHubActions_Deployment:
    Identifier: aws/github-actions-runner@v1
    Inputs:
      Sources:
        - WorkflowSource
    Configuration:
      Steps:
        - name: Build Container GO Apps
          run: |
            GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
            COMMIT_HASH=$(git log -1 --format=format:"%H")

            latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
            if [[ -z "$latestTag" ]]; then
              latestTag=1.0.0
            fi

            if [[ "$GIT_BRANCH" == "features/"* ]]; then
              semver="$latestTag-features.${COMMIT_HASH}"
            elif [[ "$GIT_BRANCH" == "bugfix/"* ]]; then
              semver="$latestTag-bugfix.${COMMIT_HASH}"
            elif [[ "$GIT_BRANCH" == "hotfix/"* ]]; then
              semver="$latestTag-beta.${COMMIT_HASH}"
            else
              semver="$latestTag.${COMMIT_HASH}"
            fi

            if [[ -z "$semver" ]]; then
              ## DockerHub
              ## ./dockerhub-build.sh Dockerfile devopscorner/bookstore alpine $COMMIT_HASH
              ./ecr-build.sh "${Secrets.AWS_ACCOUNT_ID}" Dockerfile devopscorner/bookstore alpine $COMMIT_HASH
            else
              ## DockerHub
              ## ./dockerhub-build.sh Dockerfile devopscorner/bookstore alpine $semver
              ## ECR
              ./ecr-build.sh "${Secrets.AWS_ACCOUNT_ID}" Dockerfile devopscorner/bookstore alpine $semver
            fi
        - name: List Container Images
          run: |
            docker images | grep "devopscorner/bookstore"
        - name: Push to ECR (Container Registry)
          run: |
            ./ecr-push.sh "${Secrets.AWS_ACCOUNT_ID}" devopscorner/bookstore alpine
        - name: Deploy to ECS
          run: |
            echo "Deploy to ECS cluster..."
    Outputs:
      Artifacts:
      - Name: "MyArtifact"
        Files:
          - "_infra/**"
          - ".aws/**"
          - ".codecatalyst/workflows/**"
          - "docs/**"
          - "docker-compose.yml"
          - "Dockerfile"
          - "Dockerfile.alpine-3.15"
          - "Dockerfile.alpine-3.16"
          - "Dockerfile.alpine-3.17"
          - "dockerhub-build.sh"
          - "dockerhub-push.sh"
          - "dockerhub-tag.sh"
          - "ecr-build.sh"
          - "ecr-push.sh"
          - "ecr-pull.sh"
          - "ecr-tag.sh"
          - "entrypoint.sh"
          - "git-clone.sh"
          - "Makefile"
          - "run-docker.sh"
          - "start-build.sh"
          - "src/**"
    Compute:
      Type: EC2
      Fleet: Linux.x86-64.Large
    Environment:
      Connections:
        - Role: CodeCatalystPreviewDevelopmentAdministrator-5nxysr
          Name: "YOUR_AWS_ACCOUNT"
      Name: staging-dev
  DeployToECS:
    DependsOn:
      - GitHubActions_Deployment
    Identifier: aws/ecs-deploy@v1
    Environment:
      Connections:
        - Role: CodeCatalystPreviewDevelopmentAdministrator-5nxysr
          Name: "YOUR_AWS_ACCOUNT"
      Name: staging-dev
    Inputs:
      Sources:
        - WorkflowSource
      Artifacts:
        - MyArtifact
    Configuration:
      region: us-west-2
      cluster: codecatalyst-ecs-cluster
      service: bookstore-ecs-service
      task-definition: .codecatalyst/workflows/taskdef.json

Task Definition

{
    "executionRoleArn": "arn:aws:iam::YOUR_AWS_ACCOUNT:role/codecatalyst-ecs-task-execution-role",
    "containerDefinitions": [
        {
            "name": "codecatalyst-ecs-container",
            "image": "YOUR_AWS_ACCOUNT.dkr.ecr.us-west-2.amazonaws.com/devopscorner/bookstore-codecatalyst:alpine",
            "essential": true,
            "portMappings": [
                {
                    "hostPort": 8080,
                    "protocol": "tcp",
                    "containerPort": 8080
                }
            ]
        }
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "cpu": "256",
    "family": "bookstore-ecs-task-def",
    "memory": "512",
    "networkMode": "awsvpc"
}