Skip to content

Commit

Permalink
simplify deploy workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 9, 2024
1 parent 22d467a commit ded55c8
Showing 1 changed file with 21 additions and 43 deletions.
64 changes: 21 additions & 43 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ on:

jobs:

set_environment_name:
runs-on: ubuntu-latest
outputs:
env_name: ${{ steps.set_env.outputs.env_name }}
steps:
- id: set_env
run: echo "env_name=${{ github.ref_name == 'main' && 'production' || github.ref_name }}" >> $GITHUB_OUTPUT


build_client:
needs: [ set_environment_name ]
environment:
name: ${{ github.ref_name == 'main' && 'production' || github.ref_name }}
name: ${{ needs.set_envionment_name.outputs.env_name }}
runs-on: ubuntu-latest
name: Build Client image and push to Amazon ECR
steps:
Expand All @@ -36,15 +46,6 @@ jobs:
- 'client/**'
- '.github/workflows/**'
- name: Extract branch name
if: ${{ github.event_name == 'workflow_dispatch' || steps.client-changes.outputs.client == 'true' }}
run: |
{
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
echo "branch=${branch}"
echo "branch_upper=${branch^^}"
} >> $GITHUB_OUTPUT
id: extract_branch
- name: Configure AWS credentials
if: ${{ github.event_name == 'workflow_dispatch' || steps.client-changes.outputs.client == 'true' }}
Expand Down Expand Up @@ -80,11 +81,12 @@ jobs:
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ secrets.CLIENT_REPOSITORY_NAME }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ secrets.CLIENT_REPOSITORY_NAME }}:${{ steps.extract_branch.outputs.branch == 'main' && 'production' || steps.extract_branch.outputs.branch }}
${{ steps.login-ecr.outputs.registry }}/${{ secrets.CLIENT_REPOSITORY_NAME }}:${{ needs.set_envionment_name.outputs.env_name }}
build_api:
needs: [ set_environment_name ]
environment:
name: ${{ github.ref_name == 'main' && 'production' || github.ref_name }}
name: ${{ needs.set_envionment_name.outputs.env_name }}
runs-on: ubuntu-latest
name: Build API image and push to Amazon ECR
steps:
Expand All @@ -99,16 +101,6 @@ jobs:
- 'api/**'
- '.github/workflows/**'
- name: Extract branch name
if: ${{ github.event_name == 'workflow_dispatch' || steps.api-changes.outputs.api == 'true' }}
run: |
{
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
echo "branch=${branch}"
echo "branch_upper=${branch^^}"
} >> $GITHUB_OUTPUT
id: extract_branch

- name: Configure AWS credentials
if: ${{ github.event_name == 'workflow_dispatch' || steps.api-changes.outputs.api == 'true' }}
uses: aws-actions/configure-aws-credentials@v4
Expand Down Expand Up @@ -139,22 +131,23 @@ jobs:
DB_USERNAME=${{ secrets.DB_USERNAME }}
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
JWT_SECRET=${{ secrets.JWT_SECRET }}
JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./api/Dockerfile
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ secrets.API_REPOSITORY_NAME }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ secrets.API_REPOSITORY_NAME }}:${{ steps.extract_branch.outputs.branch == 'main' && 'production' || steps.extract_branch.outputs.branch }}
${{ steps.login-ecr.outputs.registry }}/${{ secrets.API_REPOSITORY_NAME }}:${{ needs.set_envionment_name.outputs.env_name }}

deploy:
name: Deploy Services to Amazon EBS
needs: [ build_client, build_api ]
needs: [ set_environment_name, build_client, build_api ]
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name == 'main' && 'production' || github.ref_name }}
name: ${{ needs.set_envionment_name.outputs.env_name }}

steps:
- name: Checkout code
Expand All @@ -171,22 +164,13 @@ jobs:
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Extract branch name
run: |
{
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
echo "branch=${branch}"
echo "branch_upper=${branch^^}"
} >> $GITHUB_OUTPUT
id: extract_branch

- name: Generate docker compose file
working-directory: infrastructure/source_bundle
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY_CLIENT: ${{ secrets.CLIENT_REPOSITORY_NAME }}
ECR_REPOSITORY_API: ${{ secrets.API_REPOSITORY_NAME }}
IMAGE_TAG: ${{ steps.extract_branch.outputs.branch == 'main' && 'production' || steps.extract_branch.outputs.branch }}
IMAGE_TAG: ${{ needs.set_envionment_name.outputs.env_name }}
run: |
cat <<EOF >> docker-compose.yml
services:
Expand All @@ -213,12 +197,6 @@ jobs:
- client
EOF
- name: Upload Docker Compose File as Artifact
uses: actions/upload-artifact@v2
with:
name: docker-compose-file
path: infrastructure/source_bundle/docker-compose.yml

- name: Generate zip file
working-directory: infrastructure/source_bundle
run: |
Expand All @@ -235,8 +213,8 @@ jobs:
with:
aws_access_key: ${{ secrets.PIPELINE_USER_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.PIPELINE_USER_SECRET_ACCESS_KEY }}
application_name: ${{ secrets.PROJECT_NAME}}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || steps.extract_branch.outputs.branch }}
environment_name: ${{ secrets.PROJECT_NAME}}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || steps.extract_branch.outputs.branch }}-env
application_name: ${{ secrets.PROJECT_NAME}}-${{ needs.set_envionment_name.outputs.env_name }}
environment_name: ${{ secrets.PROJECT_NAME}}-${{ needs.set_envionment_name.outputs.env_name }}-env
region: ${{ secrets.AWS_REGION }}
version_label: ${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
deployment_package: infrastructure/source_bundle/deploy.zip

0 comments on commit ded55c8

Please sign in to comment.