From 42fc94412e1d34560144de725f2219b2ac89d6db Mon Sep 17 00:00:00 2001 From: alexeh Date: Mon, 9 Sep 2024 07:35:42 +0200 Subject: [PATCH] simplify deploy workflow --- .github/workflows/deploy.yml | 64 ++++++++++++------------------------ 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a9ff9dd6..d6afafc0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -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_environment_name.outputs.env_name }} runs-on: ubuntu-latest name: Build Client image and push to Amazon ECR steps: @@ -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' }} @@ -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_environment_name.outputs.env_name }} build_api: + needs: [ set_environment_name ] environment: - name: ${{ github.ref_name == 'main' && 'production' || github.ref_name }} + name: ${{ needs.set_environment_name.outputs.env_name }} runs-on: ubuntu-latest name: Build API image and push to Amazon ECR steps: @@ -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 @@ -139,6 +131,7 @@ jobs: DB_USERNAME=${{ secrets.DB_USERNAME }} DB_PASSWORD=${{ secrets.DB_PASSWORD }} JWT_SECRET=${{ secrets.JWT_SECRET }} + JWT_EXPIRES_IN=${{ vars.JWT_EXPIRES_IN }} context: . cache-from: type=gha cache-to: type=gha,mode=max @@ -146,15 +139,15 @@ jobs: 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_environment_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_environment_name.outputs.env_name }} steps: - name: Checkout code @@ -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_environment_name.outputs.env_name }} run: | cat <> docker-compose.yml services: @@ -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: | @@ -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_environment_name.outputs.env_name }} + environment_name: ${{ secrets.PROJECT_NAME}}-${{ needs.set_environment_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 \ No newline at end of file