0

Buildspec.yaml

version: 0.2
files:
  - source: /
    destination: /folder-test

phases:
  install:
    commands:
      - apt-get update
      - apt install jq
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --region eu-west-1 --no-include-email | sed 's|https://||')
      - IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
  build:
    commands:
      - echo Pulling docker image
      - docker pull 309005414223.dkr.ecr.eu-west-1.amazonaws.com/my-task-webserver-repository:latest
      - echo Running the Docker image...
      - docker run -d=true 309005414223.dkr.ecr.eu-west-1.amazonaws.com/my-task-webserver-repository:latest
  post_build:
    commands:
      - aws ecs describe-task-definition --task-definition my-task-task-definition | jq '.taskDefinition' > taskdef.json
artifacts:
  files:
    - appspec.yaml
    - taskdef.json

Appspec.yml

version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "arn:XXX/YYY"
        LoadBalancerInfo:
          ContainerName: "My-name"
          ContainerPort: "8080"
        NetworkConfiguration:
          AwsvpcConfiguration:
            Subnets: ["subnet-1","subnet-2","subnet-3"]
            SecurityGroups: ["sg-1","sg-2","sg-3"]
            AssignPublicIp: "DISABLED"

Terraform resource (codepipeline)

resource "aws_codepipeline" "codepipeline" {
  name     = "${var.namespace}-stage"
  role_arn = aws_iam_role.role.arn

  artifact_store {
    location = aws_s3_bucket.bucket.bucket
    type     = "S3"
  }

  stage {
    name = "Source"

    action {
      name             = "Source"
      category         = "Source"
      owner            = "ThirdParty"
      provider         = "GitHub"
      version          = "1"
      output_artifacts = ["my-source"]

      configuration = {
        OAuthToken = "UUUU"
        Owner  = var.owner
        Repo   = var.repo
        Branch = var.branch
      }
    }
  }

  stage {
    name = "Build"

    action {
      name             = "Build"
      category         = "Build"
      owner            = "AWS"
      provider         = "CodeBuild"
      version          = "1"
      input_artifacts  = ["my-source"]
      output_artifacts = ["my-build"]

      configuration = {
        ProjectName = my-project
      }
    }
  }


  stage {
    name = "Deploy"

    action {
      name            = "Deploy"
      category        = "Deploy"
      owner           = "AWS"
      provider        = "CodeDeployToECS"
      input_artifacts = ["my-build"]
      version         = "1"

      configuration = {
        ApplicationName     = app_name
        DeploymentGroupName = group_name
        TaskDefinitionTemplateArtifact = "my-build"
        AppSpecTemplateArtifact        = "my-build"
      }
    }
  }
}

Codebuild

resource "aws_codebuild_project" "codebuild" {
  name          = my-project
  description   = "Builds for my-project"
  build_timeout = "15"
  service_role  = aws_iam_role.role.arn

  artifacts {
    type = "CODEPIPELINE"
  }

  environment {
    compute_type    = "BUILD_GENERAL1_SMALL"
    image           = "aws/codebuild/standard:2.0"
    type            = "LINUX_CONTAINER"
    privileged_mode = true

  }

  cache {
    type  = "LOCAL"
    modes = ["LOCAL_DOCKER_LAYER_CACHE", "LOCAL_SOURCE_CACHE"]
  }

  source {
    type = "CODEPIPELINE"
  }

  vpc_config {
          security_group_ids = var.sg_ids
          subnets = ["subnet-1","subnet-2","subnet-3"]
          vpc_id = "vpc-1"
  }
}

Everything works well in codepipeline. Task is created, and trafic redirect. No log showing any issue. Just when connect through ssh to the server. The folder folder-test exists but no content there except child folders. Files are not there.

I tried removing the folder in console, and redeploying a new push, and the same result.

mrc
  • 2,845
  • 8
  • 39
  • 73

1 Answers1

0

According to the AWS specification for buildspec.yml your file does not conform to its specification.

Namely, there is no such section in the buildspec.yml like yours:

files:
  - source: /
    destination: /folder-test

This could explain why the file/folder is not what you expect it to be.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I tried first without it, and it didn't work at all :S The output is thhe same – mrc Jun 23 '20 at 10:26
  • @Maik Can I ask where did you get this `files` section from? Its definitly not in the `buildspec.yml` reference documentation. Maybe its some undocumented feature? – Marcin Jun 23 '20 at 10:28
  • I find it searching for answers when I had another trouble with EC2 deployment. I know it doesn't apply to ECS B/G but I had to try it – mrc Jun 23 '20 at 10:29
  • @Maik Do you want to create such folder in your container? For that you would define it in Dockerfile. – Marcin Jun 23 '20 at 10:41
  • No, thats not what I want. I just want to deploy all my code from guthub to my server. When it was an EC2 deployment it worked. Now in ECS B/G it's not working despite using all requirements in buildspec.yml. I added later on that 3 lines of code because I was trying things to fix it. But the orifginal code is without it. – mrc Jun 23 '20 at 10:50