Automate code deploy from Git lab to AWS EC2 instance

One way you could achieve this with AWS CodeDeploy is by using the S3 option in conjunction with Gitlab-CI: http://docs.aws.amazon.com/codepipeline/latest/userguide/getting-started-w.html

Depending on how your project is setup, you may have the possibility to generate a distribution Zip (Gradle offers this through the application plugin). You may need to generate your "distribution" file manually if your project does not offer such a capability.

Gitlab does not offer a direct S3 integration, however through the gitlab-ci.yml you would be able to download it into the container and run the necessary upload commands to put the generated zip file on the S3 container as per the AWS instructions to trigger the deployment.

Here is an example of what your brefore-script could look like in the gitlab-ci.yml file:

before_script:
  - apt-get update --quiet --yes
  - apt-get --quiet install --yes python
  - pip install -U pip
  - pip install awscli

The AWS tutorial on how to use CodeDeploy with S3 is very detailed, so I will skip attempting to reproduce the contents here.

In regards to the actual deployment commands and actions that you are currently performing manually, AWS CodeDeploy provides the capability to run certain actions through scripts defined in the app-spec file depending on event hooks for the application:

  • http://docs.aws.amazon.com/codedeploy/latest/userguide/writing-app-spec.html
  • http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html
  • http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref-hooks.html

I hope this helps.