Skip to content

Instantly share code, notes, and snippets.

@insekticid
Created July 24, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insekticid/d5a499bfe10236a630440db91bd87e00 to your computer and use it in GitHub Desktop.
Save insekticid/d5a499bfe10236a630440db91bd87e00 to your computer and use it in GitHub Desktop.
aws codebuild status to gitlab pipeline
# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: terraform:latest
variables:
CODEBUILD_PROJECT: myprojectname
CODECOMMIT_REPOSITORY: projectrepo
services:
- docker:dind
.codebuild: &codebuild_job_definition
variables:
_GIT_STRATEGY: none
services:
- docker:dind
script:
- pwd & ls -al
- setup_aws_cli
- codecomit_sync
- startbuild
- getstatus
run codebuild:
<<: *codebuild_job_definition
# only:
# - tags
.auto_devops: &auto_devops |
[[ "$TRACE" ]] && set -x
export CI_APPLICATION_TAG=$CI_COMMIT_REF_SLUG
[[ "$CI_COMMIT_TAG" ]] && export CI_APPLICATION_TAG=$CI_COMMIT_TAG
function codecomit_sync() {
echo "Syncing to AWS CodeCommit"
#git remote set-url --add aws https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/$CODECOMMIT_REPOSITORY
git push -f -vvv -u https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/$CODECOMMIT_REPOSITORY HEAD:refs/heads/$CI_COMMIT_REF_NAME
echo "Sync done"
echo ""
}
function setup_aws_cli() {
aws configure set default.region $AWS_REGION
aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID"
aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY"
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
echo "aws cli configured"
}
function startbuild() {
export AWS_OUTPUT=$(aws codebuild start-build --project-name $CODEBUILD_PROJECT --source-version=$CI_COMMIT_SHA)
echo "$AWS_OUTPUT"
export AWS_BUILD_ID="$(echo $AWS_OUTPUT | jq -r '.build.id')"
echo ""
echo "Build id $AWS_BUILD_ID"
echo "https://eu-west-1.console.aws.amazon.com/codesuite/codebuild/projects/$CODEBUILD_PROJECT/build/$AWS_BUILD_ID/log?region=eu-west-1"
}
function getstatus() {
export AWS_OUTPUT=$(aws codebuild batch-get-builds --ids "$AWS_BUILD_ID")
export AWS_BUILD_PHASE="$(echo $AWS_OUTPUT | jq -r '.builds[0].currentPhase')"
export AWS_BUILD_STATUS="$(echo $AWS_OUTPUT | jq -r '.builds[0].buildStatus')"
if [ "$AWS_BUILD_STATUS" = "IN_PROGRESS" ]; then
echo "Current status: $AWS_BUILD_PHASE... Retrying in 15 seconds..."
sleep 15
getstatus
else
echo "Finished"
if [ "$AWS_BUILD_STATUS" = "SUCCEEDED" ]; then
echo "Build succeeded! $AWS_BUILD_STATUS"
exit 0
else
echo "Build failed with message: $AWS_BUILD_STATUS"
echo ""
echo $AWS_OUTPUT | jq -r -C '.builds[0].phases[5].contexts'
exit 255
fi
fi
}
before_script:
- *auto_devops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment