--- tags: gitlab, ci-cd, gitlab-ci --- # Gitlab CI/CD Upload Built app to AWS S3 ## Overview usually this job pipeline is used in frontend (desktop/web/mobile) apps that ## Requirements ### Variables: - `AWS_ACCESS_KEY_ID` - `AWS_SECRET_ACCES_KEY` - `S3_BUCKET` ## Code `.gitlab-ci.yml` ```yaml= upload_to_s3: stage: deploy # stage assuming apps has been built image: amazon/aws-cli # Only upload built app on tag commit only: - tags # assuming the folder built is zipped in a file 'dist.zip' variables: BUILD_APP: ./dist.zip script: # upload to s3 with name of tag name .zip - s3 cp $BUILD_APP s3://$S3_BUCKET/$CI_PROJECT_PATH/${CI_COMMIT_TAG}.zip # Upload to s3 with name latest.zip - s3 cp $BUILD_APP s3://$S3_BUCKET/$CI_PROJECT_PATH/latest.zip ``` ## Example ### Vue js App ```yaml= stages: - install_dependencies - build - package - deploy .node_modules_cache: &node_modules_cache cache: key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR paths: - node_modules/ policy: pull-push .before_ssh_deploy_scripts: &before_ssh_deploy_scripts before_script: - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )" - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' - apk add rsync npm_install: stage: install_dependencies image: node:lts <<: *node_modules_cache script: - npm ci only: refs: - tags - master changes: - package-lock.json build: stage: build image: node:lts <<: *node_modules_cache script: - '[[ -d "./node_modules" ]] || npm ci' - npm run build artifacts: expire_in: 1 hour paths: - dist only: - tags - master create_zip: stage: package needs: ['build'] image: ruby script: - zip -r dist.zip dist/ artifacts: expire_in: paths: - dist.zip only: - tags upload_to_s3: stage: deploy needs: ['create_zip'] image: amazon/aws-cli variables: BUILD_APP: dist.zip script: - s3 cp $BUILD_APP s3://$S3_BUCKET/$CI_PROJECT_PATH/${CI_COMMIT_TAG}.zip - s3 cp $BUILD_APP s3://$S3_BUCKET/$CI_PROJECT_PATH/latest.zip only: - tags deploy_staging: stage: deploy needs: ['build'] image: kroniak/ssh-client <<: *before_ssh_deploy_scripts script: - rsync -aq dist/ $DEPLOY_STAGING_SSH_HOST:/var/www/html/staging only: - master deploy_production: stage: deploy needs: ['build'] image: kroniak/ssh-client <<: *before_ssh_deploy_scripts script: - rsync -aq dist/ $DEPLOY_PROD_SSH_HOST:/var/www/html/live only: - tags ```