Merge pull request #81 from andreciornavei/develop #77
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: BnbBankCI | |
on: | |
push: | |
branches: | |
- staging | |
- production | |
jobs: | |
deploy-api: | |
name: Deploy Server Api | |
runs-on: ubuntu-latest | |
steps: | |
# This step checks out a copy of your repository. | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Get branch names | |
id: branch-names | |
uses: tj-actions/branch-names@v8 | |
# This step sets up Node.js environment. | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
if: success() | |
# This step installs the Serverless Framework globally. | |
- name: Install Serverless Framework | |
run: | | |
cd backend | |
npm install -g serverless serverless-prune-plugin serverless-iam-roles-per-function | |
if: success() | |
# This step sets up PHP environment with the specified version. | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: "8.2" | |
if: success() | |
# This step installs Composer dependencies with the specified options. | |
- name: Install Composer dependencies | |
run: | | |
cd backend | |
composer install --prefer-dist --optimize-autoloader --no-dev | |
if: success() | |
# This step expose variables to environment | |
- name: Expose required environment variables | |
run: | | |
echo "BRANCH=${{ steps.branch-names.outputs.current_branch }}" >> $GITHUB_ENV | |
echo "DOMAIN=${{ vars.DOMAIN }}" >> $GITHUB_ENV | |
echo "DB_URI=${{ secrets.DB_URI }}" >> $GITHUB_ENV | |
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> $GITHUB_ENV | |
echo "APP_KEY=${{ secrets.APP_KEY }}" >> $GITHUB_ENV | |
echo "APP_NAME=${{ vars.SUBDOMAIN }}-api" >> $GITHUB_ENV | |
echo "SUBDOMAIN=${{ vars.SUBDOMAIN }}-api" >> $GITHUB_ENV | |
echo "AWS_HOSTED_ZONE_ID=${{ vars.AWS_HOSTED_ZONE_ID }}" >> $GITHUB_ENV | |
echo "AWS_DEFAULT_REGION=${{ vars.AWS_DEFAULT_REGION }}" >> $GITHUB_ENV | |
echo "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV | |
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV | |
if: success() | |
# This step deploys your application to AWS Lambda using the Serverless Framework. | |
- name: Deploy to AWS Lambda | |
run: | | |
cd backend | |
echo "APP_KEY=${APP_KEY}" >> .env | |
echo "APP_NAME=${APP_NAME}" >> .env | |
echo "SUBDOMAIN=${SUBDOMAIN}" >> .env | |
echo "DOMAIN=${DOMAIN}" >> .env | |
echo "DB_URI=${DB_URI}" >> .env | |
echo "JWT_SECRET=${JWT_SECRET}" >> .env | |
echo "AWS_HOSTED_ZONE_ID=${AWS_HOSTED_ZONE_ID}" >> .env | |
echo "AWS_BUCKET_KEY=${AWS_ACCESS_KEY_ID}" >> .env | |
echo "AWS_BUCKET_SECRET=${AWS_SECRET_ACCESS_KEY}" >> .env | |
serverless deploy --config serverless.yml --stage ${BRANCH} --region ${AWS_DEFAULT_REGION} --verbose | |
if: success() | |
deploy-web: | |
name: Deploy Web App | |
runs-on: ubuntu-latest | |
steps: | |
# This step checks out a copy of your repository. | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Get branch names | |
id: branch-names | |
uses: tj-actions/branch-names@v8 | |
# This step sets up Node.js environment. | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
if: success() | |
# This step installs the Serverless Framework globally. | |
- name: Install Serverless Framework | |
run: | | |
cd frontend | |
npm install -g serverless serverless-dotenv-plugin serverless-export-outputs fast-xml-parser | |
if: success() | |
# This step expose variables to environment | |
- name: Expose required environment variables | |
run: | | |
echo "BRANCH=${{ steps.branch-names.outputs.current_branch }}" >> $GITHUB_ENV | |
echo "DOMAIN=${{ vars.DOMAIN }}" >> $GITHUB_ENV | |
echo "APP_NAME=${{ vars.SUBDOMAIN }}-web" >> $GITHUB_ENV | |
echo "SUBDOMAIN=${{ vars.SUBDOMAIN }}" >> $GITHUB_ENV | |
echo "AWS_HOSTED_ZONE_ID=${{ vars.AWS_HOSTED_ZONE_ID }}" >> $GITHUB_ENV | |
echo "AWS_DEFAULT_REGION=${{ vars.AWS_DEFAULT_REGION }}" >> $GITHUB_ENV | |
echo "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV | |
echo "AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV | |
if: success() | |
# This step expose variables to environment | |
- name: Expose required environment variables (composed) | |
run: | | |
echo "S3_DOMAIN=${APP_NAME}.$([ "${BRANCH}" != "production" ] && echo "${BRANCH}.")${DOMAIN}" >> $GITHUB_ENV | |
if: success() | |
# This step build reactjs application to be deployed. | |
- name: Build ReactJS application | |
run: | | |
cd frontend | |
rm -rf .env | |
echo "REACT_APP_API_URL=https://${SUBDOMAIN}-api.$([ "${BRANCH}" != "production" ] && echo "${BRANCH}.")${DOMAIN}" >> .env | |
yarn --dev --production=false | |
yarn build | |
if: success() | |
# This step deploys the application resources to AWS using the Serverless Framework. | |
- name: Deploy resources to AWS with Serverless Framework | |
run: | | |
cd frontend | |
rm -rf .env | |
echo "DOMAIN=${DOMAIN}" >> .env | |
echo "AWS_HOSTED_ZONE_ID=${AWS_HOSTED_ZONE_ID}" >> .env | |
serverless deploy --config serverless-cert.yml --stage ${BRANCH} --region us-east-1 --verbose | |
SLS_WEBCERT_OUTPUT=$(cat sls-webcert-output.json) | |
$(echo "${SLS_WEBCERT_OUTPUT}" | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"') | |
echo "AWS_ACM_ARN=${CertificateDomainArn}" >> .env | |
serverless deploy --config serverless.yml --stage ${BRANCH} --region ${AWS_DEFAULT_REGION} --verbose | |
if: success() | |
# This step deploys your application to AWS Lambda using the Serverless Framework. | |
- name: Release application on AWS S3 and invalidate CloudFront cache | |
run: | | |
cd frontend | |
aws s3 sync build/ s3://${S3_DOMAIN} | |
SLS_OUTPUT=$(cat sls-output.json) | |
$(echo "${SLS_OUTPUT}" | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"') | |
aws cloudfront create-invalidation --distribution-id ${CDNDistributionId} --paths "/*" | |
if: success() |