buildspec.yaml 3.51 KB
Newer Older
Spencer Sutton's avatar
Spencer Sutton committed
1
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.​
Spencer Sutton's avatar
Spencer Sutton committed
2
3
4
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Spencer Sutton's avatar
Spencer Sutton committed
5
# 
Spencer Sutton's avatar
Spencer Sutton committed
6
#      http://www.apache.org/licenses/LICENSE-2.0
Spencer Sutton's avatar
Spencer Sutton committed
7
# 
Spencer Sutton's avatar
Spencer Sutton committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

# https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
version: 0.2

env:
  secrets-manager:
    DOCKER_USERNAME: /osdu/devops/docker_credentials:username
    DOCKER_PASSWORD: /osdu/devops/docker_credentials:password
Spencer Sutton's avatar
Spencer Sutton committed
23
24
25
  
  variables:
    CI_COMMIT_TAG: v0.0.8
Spencer Sutton's avatar
Spencer Sutton committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

phases:
  install:
    commands:
      - if [ $(echo $CODEBUILD_SOURCE_VERSION | grep -c  ^refs/heads.*) -eq 1 ]; then echo "Branch name found"; else echo "This build only supports branch builds" && exit 1; fi
      - apt-get update -y
  pre_build:
    commands:
      - echo "Logging in to Amazon ECR..."
      - $(aws ecr get-login --no-include-email --region $AWS_REGION) # authenticate with ECR via the AWS CLI      
  build:
    commands:
      - export REPO_NAME=${PWD##*/} 
      - export OUTPUT_DIR="dist"
      - export BRANCH_NAME=`echo ${CODEBUILD_SOURCE_VERSION} | awk '{gsub("refs/heads/","");gsub("\\.","-");gsub("[[:space:]]","-")}1' | sed 's/\//-/g' | awk '{print tolower($0)}'`
      - export ECR_TAG=`echo build.${BRANCH_NAME}.${CODEBUILD_BUILD_NUMBER}.${CODEBUILD_RESOLVED_SOURCE_VERSION} | cut -c 1-120`
      - export ECR_IMAGE=${ECR_REGISTRY}:${ECR_TAG}
      - export ECR_IMAGE_BRANCH_LATEST=${ECR_REGISTRY}:${BRANCH_NAME}
      - export AWS_ACCOUNT_ID=`aws sts get-caller-identity | grep Account | cut -d':' -f 2 | cut -d'"' -f 2`

      - aws codeartifact login --tool pip --domain osdu-dev --domain-owner $AWS_ACCOUNT_ID --repository osdu-python --region us-east-1
      - |
        aws codeartifact get-package-version-asset \
        --domain osdu-dev \
        --domain-owner $AWS_ACCOUNT_ID \
        --repository osdu-python \
        --region us-east-1 \
        --format pypi \
        --package osdu-api \
Spencer Sutton's avatar
Spencer Sutton committed
55
56
57
        --package-version 0.0.8 \
        --asset osdu_api-0.0.8.tar.gz \
        ./build/osdu_api-0.0.8.tar.gz
Spencer Sutton's avatar
Spencer Sutton committed
58
59
60
61
62
63

      - echo "Logging into Docker Hub as ${DOCKER_USERNAME}..."
      - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}

      - echo "Building docker image..."
      - docker build -f build/Dockerfile . -t witsmlparser
Spencer Sutton's avatar
Spencer Sutton committed
64
      - docker build -f build/providers/aws/Dockerfile -t ${ECR_IMAGE} --build-arg docker_builder_image=witsmlparser .
Spencer Sutton's avatar
Spencer Sutton committed
65
66
67
68
69
70
71
72
73
74
      - docker tag  ${ECR_IMAGE} ${ECR_IMAGE_BRANCH_LATEST}
      - echo "Pushing docker image..."
      - docker push ${ECR_IMAGE}
      - docker push ${ECR_IMAGE_BRANCH_LATEST}


      - mkdir -p ${OUTPUT_DIR}

      - echo "Generate build-info.json"
      - |
Spencer Sutton's avatar
Spencer Sutton committed
75
        python build/providers/aws/build-info.py --branch ${CODEBUILD_SOURCE_VERSION} --commit ${CODEBUILD_RESOLVED_SOURCE_VERSION} \
Spencer Sutton's avatar
Spencer Sutton committed
76
77
78
79
80
81
82
83
          --buildid ${CODEBUILD_BUILD_ID} --buildnumber ${CODEBUILD_BUILD_NUMBER} --reponame ${REPO_NAME} --outdir ${OUTPUT_DIR} \
          --artifact ${ECR_IMAGE}

artifacts:
  files:
    - "**/*"
  base-directory: ${OUTPUT_DIR}
  name: ${REPO_NAME}_${BRANCH_NAME}_$(date +%F)_${CODEBUILD_BUILD_NUMBER}.zip