|
| 1 | +# Buildspec runs in the build stage of your pipeline. |
| 2 | +version: 0.2 |
| 3 | +phases: |
| 4 | + install: |
| 5 | + runtime-versions: |
| 6 | + docker: 19 |
| 7 | + ruby: 2.6 |
| 8 | + commands: |
| 9 | + - echo "cd into $CODEBUILD_SRC_DIR" |
| 10 | + - cd $CODEBUILD_SRC_DIR |
| 11 | + # Download the copilot linux binary. |
| 12 | + - wget -q https://ecs-cli-v2-release.s3.amazonaws.com/copilot-linux-v1.21.0-8-gf3c4e78f |
| 13 | + - mv ./copilot-linux-v1.21.0-8-gf3c4e78f ./copilot-linux |
| 14 | + - chmod +x ./copilot-linux |
| 15 | + build: |
| 16 | + commands: |
| 17 | + - echo "Run your tests" |
| 18 | + # - make test |
| 19 | + post_build: |
| 20 | + commands: |
| 21 | + - ls -l |
| 22 | + - export COLOR="false" |
| 23 | + - pipeline=$(cat $CODEBUILD_SRC_DIR/copilot/pipelines/copilot-pipeline-test-main/manifest.yml | ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))') |
| 24 | + - pl_envs=$(echo $pipeline | jq -r '.stages[].name') |
| 25 | + # Find all the local services in the workspace. |
| 26 | + - svc_ls_result=$(./copilot-linux svc ls --local --json) |
| 27 | + - svc_list=$(echo $svc_ls_result | jq '.services') |
| 28 | + - > |
| 29 | + if [ ! "$svc_list" = null ]; then |
| 30 | + svcs=$(echo $svc_ls_result | jq -r '.services[].name'); |
| 31 | + fi |
| 32 | + # Find all the local jobs in the workspace. |
| 33 | + - job_ls_result=$(./copilot-linux job ls --local --json) |
| 34 | + - job_list=$(echo $job_ls_result | jq '.jobs') |
| 35 | + - > |
| 36 | + if [ ! "$job_list" = null ]; then |
| 37 | + jobs=$(echo $job_ls_result | jq -r '.jobs[].name'); |
| 38 | + fi |
| 39 | + # Raise error if no services or jobs are found. |
| 40 | + - > |
| 41 | + if [ "$svc_list" = null ] && [ "$job_list" = null ]; then |
| 42 | + echo "No services or jobs found for the pipeline to deploy. Please create at least one service or job and push the manifest to the remote." 1>&2; |
| 43 | + exit 1; |
| 44 | + fi |
| 45 | + # Generate the cloudformation templates. |
| 46 | + # The tag is the build ID but we replaced the colon ':' with a dash '-'. |
| 47 | + # We truncate the tag (from the front) to 128 characters, the limit for Docker tags |
| 48 | + # (https://docs.docker.com/engine/reference/commandline/tag/) |
| 49 | + # Check if the `svc package` commanded exited with a non-zero status. If so, echo error msg and exit. |
| 50 | + - > |
| 51 | + for env in $pl_envs; do |
| 52 | + tag=$(sed 's/:/-/g' <<<"${CODEBUILD_BUILD_ID##*:}-${env}" | rev | cut -c 1-128 | rev) |
| 53 | + for svc in $svcs; do |
| 54 | + ./copilot-linux svc package -n $svc -e $env --output-dir './infrastructure' --tag $tag --upload-assets; |
| 55 | + if [ $? -ne 0 ]; then |
| 56 | + echo "Cloudformation stack and config files were not generated. Please check build logs to see if there was a manifest validation error." 1>&2; |
| 57 | + exit 1; |
| 58 | + fi |
| 59 | + done; |
| 60 | + for job in $jobs; do |
| 61 | + ./copilot-linux job package -n $job -e $env --output-dir './infrastructure' --tag $tag --upload-assets; |
| 62 | + if [ $? -ne 0 ]; then |
| 63 | + echo "Cloudformation stack and config files were not generated. Please check build logs to see if there was a manifest validation error." 1>&2; |
| 64 | + exit 1; |
| 65 | + fi |
| 66 | + done; |
| 67 | + done; |
| 68 | + - ls -lah ./infrastructure |
| 69 | +artifacts: |
| 70 | + files: |
| 71 | + - "infrastructure/*" |
0 commit comments