Skip to content

Instantly share code, notes, and snippets.

@igiagante-zz
Created February 25, 2021 15:05
Show Gist options
  • Save igiagante-zz/8cbc8b87b0dd91ef34e67103e812f2e0 to your computer and use it in GitHub Desktop.
Save igiagante-zz/8cbc8b87b0dd91ef34e67103e812f2e0 to your computer and use it in GitHub Desktop.
Deploy to lightsail from gitlab
deploy_to_lightsail:
stage: deploy
services:
- name: docker:dind
entrypoint: ['env', '-u', 'DOCKER_HOST']
command: ['dockerd-entrypoint.sh']
before_script:
# 1. Install AWSCLIv2 (https://stackoverflow.com/questions/60298619/awscli-version-2-on-alpine-linux#answer-61268529)
- chmod +x ./alpine.awscliv2.install.sh
- ./alpine.awscliv2.install.sh
- aws --version
# 2. Install LightsailCTL Plugin (https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-install-software)
- apk --no-cache add curl jq
- curl https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl -o /usr/local/bin/lightsailctl
- chmod +x /usr/local/bin/lightsailctl
script:
# 3. Download the docker image for this pipeline
- docker info
- echo "$GITLAB_PERSONAL_TOKEN" | docker login -u igiagante --password-stdin ${GITLAB_REGISTRY}
- docker pull ${GITLAB_IMAGE}
- aws configure set aws_access_key_id AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key AWS_SECRET_ACCESS_KEY
- aws configure set default.region AWS_DEFAULT_REGION
- cat ~/.aws/credentials
# 4. Upload the docker image for this pipeline
- aws lightsail push-container-image
--service-name ${SERVICE_NAME}
--label axis
--image registry.gitlab.com/igiagante/axis/develop:latest
--region us-east-1
# 5. Get the uploaded image (its different every time)
- PIPELINE_IMAGE_TAG=$(aws lightsail get-container-images --service ${SERVICE_NAME} | jq -r .containerImages[0].image)
# 6. Create a deployment with the uploaded docker image
- aws lightsail create-container-service-deployment >/dev/null
--service-name ${SERVICE_NAME}
--containers "{\"$SERVICE_NAME\":{\"image\":\"$PIPELINE_IMAGE_TAG\",\"ports\":{\"3308\":\"HTTP\"}}}"
--public-endpoint "{\"containerName\":\"$SERVICE_NAME\",\"containerPort\":3308,\"healthCheck\":{\"path\":\"/\"}}"
only:
- lightsail
@igiagante-zz
Copy link
Author

igiagante-zz commented Feb 25, 2021

Error:
Screen Shot 2021-02-25 at 12 53 40 PM

@igiagante-zz
Copy link
Author

I tested the same command locally and it works perfectly using the same aws credentials.

@sdesalas
Copy link

sdesalas commented Feb 25, 2021

Hiya, I think you forgot to put a $ before the following:

    - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
    - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
    - aws configure set default.region $AWS_DEFAULT_REGION

And you probably want to:

  - echo $AWS_ACCESS_KEY_ID
  - aws configure list

To find out which user you are logging into AWS as.

@igiagante-zz
Copy link
Author

Hi @sdesalas, I've just put the name of the vars as example. Basically, I'm putting the values as hardcode. It's not a problem of reading vars from Git Env. The issue is that when I try to execute any aws command, it does not work because there wasnt a successful authentication.

@sdesalas
Copy link

sdesalas commented Mar 3, 2021

Try using env variables instead of setting the values with aws configure:

deploy_to_lightsail:
  stage: deploy
  variables: 
    AWS_ACCESS_KEY_ID: <YOUR ACCESS KEY>
    AWS_SECRET_ACCESS_KEY: <YOUR ACCESS KEY>
    AWS_DEFAULT_REGION: <YOUR ACCESS KEY>

You can even put them in Settings > CI/CD Pipelines which is a bit better as they are not sitting around in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment