From 9efee1d90e6e1a4b05cf2cb1a3e222a3c4777cfb Mon Sep 17 00:00:00 2001 From: Robert Jandow <38583713+robertjndw@users.noreply.github.com> Date: Wed, 19 Jul 2023 11:36:26 +0200 Subject: [PATCH] Debug improved Github actions file (#59) * Add new dependency between workflows * Split client and server image tag * Fix bash syntax error * Make production ready * Add latest tag to production --- .github/workflows/build_docker.yml | 65 +++++++++++++++++------------ .github/workflows/deploy_docker.yml | 39 +++++++---------- .github/workflows/dev.yml | 3 +- .github/workflows/prod.yml | 2 + docker-compose.prod.yml | 4 +- 5 files changed, 59 insertions(+), 54 deletions(-) diff --git a/.github/workflows/build_docker.yml b/.github/workflows/build_docker.yml index 3d111efb..495397c7 100644 --- a/.github/workflows/build_docker.yml +++ b/.github/workflows/build_docker.yml @@ -2,6 +2,13 @@ name: Build Docker Image on: workflow_call: + outputs: + server_image_tag: + description: "The tag of the server image that was built" + value: ${{ jobs.build.outputs.server_image_tag }} + client_image_tag: + description: "The tag of the client image that was built" + value: ${{ jobs.build.outputs.client_image_tag }} jobs: build: @@ -19,8 +26,28 @@ jobs: context: ./server path: server outputs: - image_tag: ${{ steps.output-tag.outputs.image_tag }} + server_image_tag: "${{ steps.output-tag.outputs.image_tag }}" + client_image_tag: "${{ steps.output-tag.outputs.image_tag }}" steps: + - name: Compute Tag + uses: actions/github-script@v6 + id: compute-tag + with: + result-encoding: string + script: | + if (context.eventName === "pull_request") { + return "pr-" + context.issue.number; + } + if (context.eventName === "push") { + if (context.ref.startsWith("refs/tags/")) { + return context.ref.slice(10); + } + if (context.ref === "refs/heads/develop") { + return "develop"; + } + } + return "latest"; + - name: Checkout uses: actions/checkout@v3 with: @@ -57,40 +84,26 @@ jobs: id: buildx uses: docker/setup-buildx-action@v2 - - name: Compute Tag - uses: actions/github-script@v6 - id: compute-tag - with: - result-encoding: string - script: | - if (context.eventName === "pull_request") { - return "pr-" + context.issue.number; - } - if (context.eventName === "push") { - if (context.ref.startsWith("refs/tags/")) { - return context.ref.slice(10); - } - if (context.ref === "refs/heads/develop") { - return "develop"; - } - } - return "latest"; - - - id: output-tag - run: echo "image_tag=${{ steps.compute-tag.outputs.result }}" >> "$GITHUB_OUTPUT" - - name: Build and push Docker Image uses: docker/build-push-action@v4 if: ${{ (steps.changed-files-client-folder.outputs.any_changed == 'true' && matrix.path == 'client') || (steps.changed-files-server-folder.outputs.any_changed == 'true' && matrix.path == 'server') }} - env: - IMAGE_TAG: ${{ steps.compute-tag.outputs.result }} with: context: ${{ matrix.context }} file: ${{ matrix.dockerfile }} platforms: linux/amd64,linux/arm64 push: true - tags: ${{ matrix.image }}:${{ env.IMAGE_TAG }} + tags: ${{ matrix.image }}:${{ steps.compute-tag.outputs.result }} build-args: | "SERVER_HOST=${{ vars.SERVER_HOST }}" "KEYCLOAK_HOST=${{ vars.KEYCLOAK_HOST }}" "KEYCLOAK_REALM_NAME=${{ vars.KEYCLOAK_REALM_NAME }}" + + - id: output-tag + run: | + if [[ "${{ matrix.path }}" == "client" ]] && [[ "${{ steps.changed-files-client-folder.outputs.any_changed }}" == "true" ]]; then + echo "image_tag=${{ steps.compute-tag.outputs.result }}" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.path }}" == "server" ]] && [[ "${{ steps.changed-files-server-folder.outputs.any_changed }}" == "true" ]]; then + echo "image_tag=${{ steps.compute-tag.outputs.result }}" >> "$GITHUB_OUTPUT" + else + echo "image_tag=latest" >> "$GITHUB_OUTPUT" + fi diff --git a/.github/workflows/deploy_docker.yml b/.github/workflows/deploy_docker.yml index 6316b3ee..1bbe3b23 100644 --- a/.github/workflows/deploy_docker.yml +++ b/.github/workflows/deploy_docker.yml @@ -6,12 +6,24 @@ on: environment: required: true type: string + server_image_tag: + required: true + type: string + client_image_tag: + required: true + type: string jobs: deploy: runs-on: ubuntu-latest environment: ${{ inputs.environment }} steps: + - name: Use Computed Server Tag + run: echo "The computed server tag is ${{ inputs.server_image_tag }}" + + - name: Use Computed Client Tag + run: echo "The computed client tag is ${{ inputs.client_image_tag }}" + - name: SSH to VM and Execute Docker-Compose Down uses: appleboy/ssh-action@v0.1.10 with: @@ -72,32 +84,8 @@ jobs: source: "master.cf" target: /home/${{ vars.VM_USERNAME }}/postfix-config/ - - name: Compute Tag - uses: actions/github-script@v6 - id: compute-tag - with: - result-encoding: string - script: | - if (context.eventName === "pull_request") { - return "pr-" + context.issue.number; - } - if (context.eventName === "push") { - if (context.ref.startsWith("refs/tags/")) { - return context.ref.slice(10); - } - if (context.ref === "refs/heads/develop") { - return "develop"; - } - } - return "latest"; - - - id: output-tag - run: echo "image_tag=${{ steps.compute-tag.outputs.result }}" >> "$GITHUB_OUTPUT" - - name: SSH to VM and create .env.prod file uses: appleboy/ssh-action@v0.1.10 - env: - IMAGE_TAG: ${{ steps.compute-tag.outputs.result }} with: host: ${{ vars.VM_HOST }} username: ${{ vars.VM_USERNAME }} @@ -128,7 +116,8 @@ jobs: echo "KEYCLOAK_CLIENT_SECRET=${{ secrets.KEYCLOAK_CLIENT_SECRET }}" >> .env.prod echo "IOS_SENDER_MAIL=${{ vars.IOS_SENDER_MAIL }}" >> .env.prod - echo "IMAGE_TAG=${{ env.IMAGE_TAG }}" >> .env.prod + echo "SERVER_IMAGE_TAG=${{ inputs.server_image_tag }}" >> .env.prod + echo "CLIENT_IMAGE_TAG=${{ inputs.client_image_tag }}" >> .env.prod - name: SSH to VM and Execute Docker-Compose Up uses: appleboy/ssh-action@v0.1.10 diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 618678aa..3c82e80e 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -14,4 +14,5 @@ jobs: secrets: inherit with: environment: Dev - \ No newline at end of file + server_image_tag: "${{ needs.build-dev-container.outputs.server_image_tag }}" + client_image_tag: "${{ needs.build-dev-container.outputs.client_image_tag }}" diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 5460c852..77b1f570 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -14,4 +14,6 @@ jobs: secrets: inherit with: environment: Production + server_image_tag: "latest" + client_image_tag: "latest" \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 9af08e16..5d390f5e 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -23,7 +23,7 @@ services: - ./letsencrypt:/letsencrypt server: - image: "ghcr.io/ls1intum/prompt/prompt-server:${IMAGE_TAG}" + image: "ghcr.io/ls1intum/prompt/prompt-server:${SERVER_IMAGE_TAG}" container_name: orgatool-server labels: - "traefik.enable=true" @@ -72,7 +72,7 @@ services: retries: 5 client: - image: "ghcr.io/ls1intum/prompt/prompt-client:${IMAGE_TAG}" + image: "ghcr.io/ls1intum/prompt/prompt-client:${CLIENT_IMAGE_TAG}" container_name: orgatool-client environment: - TZ=Europe/Berlin