Skip to content

Instantly share code, notes, and snippets.

@MexsonFernandes
Created January 10, 2021 07:07
Show Gist options
  • Save MexsonFernandes/ecc8b4c3e24e644f27587570c28b7f00 to your computer and use it in GitHub Desktop.
Save MexsonFernandes/ecc8b4c3e24e644f27587570c28b7f00 to your computer and use it in GitHub Desktop.
Gitlab pipeline deploy app on AWS Beanstalk
stages:
- build
- deploy
variables:
S3_BUCKET: "bucket"
S3_ARCHIVE_PATH_PROD: "production/"
build:
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
except:
- master
build docker master:
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
deploy prod:
stage: deploy
image: python:latest
before_script:
- export VERSION_TIME=$(date --utc +%Y-%m-%d_%H-%M-%SZ)
- echo $AWS_APP_NAME-$VERSION_TIME
- export AWS_ARCHIVE=$AWS_APP_NAME-$VERSION_TIME.zip
- export AWS_VERSION=$AWS_APP_NAME-$VERSION_TIME
- apt-get update --assume-yes
- apt-get install zip --assume-yes
- pip install awscli
script:
- git checkout -B "$CI_BUILD_REF_NAME" "$CI_BUILD_REF"
- zip -r $AWS_ARCHIVE . -x *.git* node_modules/\*
- aws s3 cp $AWS_ARCHIVE s3://$S3_BUCKET/$S3_ARCHIVE_PATH_PROD
- aws elasticbeanstalk create-application-version
--application-name $AWS_APP_NAME
--version-label $AWS_VERSION
--description "$(git log -1 --pretty=%B)"
--source-bundle S3Bucket="$S3_BUCKET",S3Key="$S3_ARCHIVE_PATH_PROD$AWS_ARCHIVE"
--region $AWS_DEFAULT_REGION
--auto-create-application
- aws elasticbeanstalk update-environment
--application-name $AWS_APP_NAME
--environment-name $AWS_ENV_NAME
--version-label $AWS_VERSION
only:
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment