lago/.github/workflows/deploy-preview.yml
2022-03-30 17:07:34 +02:00

210 lines
8.7 KiB
YAML

name: Deploy Preview to Staging
on:
workflow_dispatch:
inputs:
preview_name:
description: Preview Name (domain)
required: true
default: 'main'
front_branch:
description: Front Branch
required: true
default: 'main'
api_branch:
description: API Branch
required: true
default: 'main'
jobs:
build-front-image:
name: Build Front Image
runs-on: ubuntu-latest
steps:
- name: Checkout Front
uses: actions/checkout@v3
with:
repository: getlago/lago-front
ref: ${{ github.event.inputs.front_branch }}
path: front
token: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: lago-front-staging
IMAGE_TAG: ${{ github.event.inputs.front_branch }}
API_URL: https://${{ github.event.inputs.preview_name }}-api.staging.getlago.com
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg API_URL=$API_URL ./front
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
front-deploy:
name: Deploy Front
runs-on: ubuntu-latest
container:
image: getlago/deploy-staging
needs: [build-front-image]
env:
PORTER_HOST: https://dashboard.getporter.dev
PORTER_CLUSTER: ${{ secrets.PORTER_CLUSTER }}
PORTER_PROJECT: ${{ secrets.PORTER_PROJECT }}
PORTER_TOKEN: ${{ secrets.PORTER_TOKEN_2643 }}
steps:
- name: Checkout Front
uses: actions/checkout@v3
with:
repository: getlago/lago-front
ref: ${{ github.event.inputs.front_branch }}
path: front
token: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Replace Variables in values.yaml
env:
ECR_REPOSITORY: ${{ steps.login-ecr.outputs.registry }}/lago-front-staging
BRANCH: ${{ github.event.inputs.front_branch }}
DOMAIN: ${{ github.event.inputs.preview_name }}
run: envsubst < ./front/porter/values.yaml > ./front/porter/env_values.yaml
- name: Configure kubectl
run: echo "${{ secrets.KUBE_STAGING_CONFIG }}" | base64 -d > kubeconfig.yaml
- name: Get Helm deployment
id: helm_deployment
run: echo "::set-output name=deployment::$(helm --kubeconfig ./kubeconfig.yaml list -f ${{ github.event.inputs.preview_name }}-app -o=json | jq '.[0].name')"
- name: Delete Porter app
if: ${{ steps.helm_deployment.outputs.deployment != 'null' }}
run: |
echo "${{ steps.helm_deployment.outputs.deployment }}"
helm --kubeconfig ./kubeconfig.yaml uninstall ${{ github.event.inputs.preview_name }}-app
- name: Create Porter app
uses: porter-dev/porter-cli-action@v0.1.0
with:
command: create web --app ${{ github.event.inputs.preview_name }}-app --source registry --values ./front/porter/env_values.yaml --image ${{ steps.login-ecr.outputs.registry }}/lago-front-staging:${{ github.event.inputs.front_branch }}
build-api-image:
name: Build API Image
runs-on: ubuntu-latest
steps:
- name: Checkout API
uses: actions/checkout@v3
with:
repository: getlago/lago-api
ref: ${{ github.event.inputs.api_branch }}
path: api
token: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Docker tag
id: docker_tag
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: lago-api-staging
IMAGE_TAG: ${{ github.event.inputs.api_branch }}
run: echo "##[set-output name=tag;]$(echo $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG)"
- name: Build and push
uses: docker/build-push-action@v2
env:
RAILS_ENV: staging
with:
context: ./api
push: true
tags: ${{ steps.docker_tag.outputs.tag }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
api-deploy:
name: Deploy API
runs-on: ubuntu-latest
needs: [build-api-image]
container:
image: getlago/deploy-staging
env:
PORTER_HOST: https://dashboard.getporter.dev
PORTER_CLUSTER: ${{ secrets.PORTER_CLUSTER }}
PORTER_PROJECT: ${{ secrets.PORTER_PROJECT }}
PORTER_TOKEN: ${{ secrets.PORTER_TOKEN_2643 }}
steps:
- name: Checkout API
uses: actions/checkout@v3
with:
repository: getlago/lago-api
ref: ${{ github.event.inputs.api_branch }}
path: api
token: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Replace Variables
env:
ECR_REPOSITORY: ${{ steps.login-ecr.outputs.registry }}/lago-api-staging
BRANCH: ${{ github.event.inputs.api_branch }}
DATABASE_URL: postgres://lago:${{ secrets.DB_STAGING_PASSWORD }}@${{ secrets.DB_STAGING_HOST}}:5432/${{ github.event.inputs.preview_name }}
DOMAIN: ${{ github.event.inputs.preview_name }}
REDIS_URL: redis://${{ secrets.REDIS_STAGING_URL }}
run: |
envsubst < ./api/porter/api.yaml > ./api/porter/env_api.yaml
envsubst < ./api/porter/worker.yaml > ./api/porter/env_worker.yaml
- name: Configure kubectl
run: echo "${{ secrets.KUBE_STAGING_CONFIG }}" | base64 -d > kubeconfig.yaml
- name: Get Helm API deployment
id: helm_deployment
run: echo "::set-output name=deployment::$(helm --kubeconfig ./kubeconfig.yaml list -f ${{ github.event.inputs.preview_name }}-api -o=json | jq '.[0].name')"
- name: Delete API app
if: ${{ steps.helm_deployment.outputs.deployment != 'null' }}
run: |
helm --kubeconfig ./kubeconfig.yaml uninstall ${{ github.event.inputs.preview_name }}-api
- name: Create API app
uses: porter-dev/porter-cli-action@v0.1.0
with:
command: create web --app ${{ github.event.inputs.preview_name }}-api --source registry --values ./api/porter/env_api.yaml --image ${{ steps.login-ecr.outputs.registry }}/lago-api-staging:${{ github.event.inputs.api_branch }}
- name: Get Helm Worker deployment
id: helm_worker_deployment
run: echo "::set-output name=deployment::$(helm --kubeconfig ./kubeconfig.yaml list -f ${{ github.event.inputs.preview_name }}-worker -o=json | jq '.[0].name')"
- name: Delete Worker app
if: ${{ steps.helm_worker_deployment.outputs.deployment != 'null' }}
run: |
helm --kubeconfig ./kubeconfig.yaml uninstall ${{ github.event.inputs.preview_name }}-worker
- name: Create Worker app
uses: porter-dev/porter-cli-action@v0.1.0
with:
command: create worker --app ${{ github.event.inputs.preview_name }}-worker --source registry --values ./api/porter/env_worker.yaml --image ${{ steps.login-ecr.outputs.registry }}/lago-api-staging:${{ github.event.inputs.api_branch }}