From b13b650ccb7b118724024c11d38e48da4a795165 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 16:33:08 +0100 Subject: [PATCH 01/20] check for image --- .github/workflows/deploy-dev.yml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 59c1b95a..5457bce1 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -1,4 +1,4 @@ -name: Deploy Dev +name: Deploy to Dev on: workflow_dispatch: @@ -7,8 +7,29 @@ on: type: string description: "The tag of the docker images to deploy" required: true + jobs: + check-if-image-exists: + runs-on: ubuntu-latest + steps: + - name: Check if image exists + run: | + IMAGE_NAME=ls1intum/apollon_standalone + + ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64) + TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \ + https://ghcr.io/v2/${IMAGE_NAME}/tags/list \ + | jq -r --arg TAG "${{ inputs.image-tag }}" '.tags[] | select(. == $TAG)') + + if [ -z "$TAG_EXISTS" ]; then + echo "Image ${IMAGE_NAME}:${{ inputs.image-tag }} does not exist." + exit 1 + else + echo "Image ${IMAGE_NAME}:${{ inputs.image-tag }} exists." + fi + deploy: + needs: check-if-image-exists # TODO: uses: ls1intum/.github/.github/workflows/deploy-docker-compose.yml@main uses: ./.github/workflows/deploy-docker-compose-shared.yml with: From 1c59379b38e9c98258e1f328b152f9b49a72d4b2 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 16:39:52 +0100 Subject: [PATCH 02/20] log payload --- .github/workflows/deploy-dev.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 5457bce1..3f970add 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -12,6 +12,11 @@ jobs: check-if-image-exists: runs-on: ubuntu-latest steps: + - name: Log workflow_dispatch payload + run: | + echo "Workflow Dispatch Payload:" + echo "${{ toJson(github.event) }}" | jq + - name: Check if image exists run: | IMAGE_NAME=ls1intum/apollon_standalone From d45ea45707d928203d0cf78cb5045e9d5c72eeb5 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:01:56 +0100 Subject: [PATCH 03/20] improve workflow --- .github/workflows/deploy-dev.yml | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 3f970add..6091fa4f 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -5,32 +5,36 @@ on: inputs: image-tag: type: string - description: "The tag of the docker images to deploy" - required: true + description: "The tag of the docker images to deploy (default: pr- of branch or latest)" jobs: check-if-image-exists: runs-on: ubuntu-latest steps: - - name: Log workflow_dispatch payload + - name: Retrieve PR number + id: pr-number run: | - echo "Workflow Dispatch Payload:" - echo "${{ toJson(github.event) }}" | jq + REF=$(echo "${{ github.event.ref.ref }}" | sed -n 's#^refs/heads/##p') + PULLS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${REF}") + PR_NUMBER=$(echo "$PULLS" | jq -r '.[0].number') + echo "::set-output name=pr-number::$PR_NUMBER" - name: Check if image exists run: | IMAGE_NAME=ls1intum/apollon_standalone + IMAGE_TAG=${{ inputs.image-tag }} || "pr-${{ steps.pr-number.outputs.pr-number }}" ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64) TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \ https://ghcr.io/v2/${IMAGE_NAME}/tags/list \ - | jq -r --arg TAG "${{ inputs.image-tag }}" '.tags[] | select(. == $TAG)') + | jq -r --arg TAG "${IMAGE_TAG}" '.tags[] | select(. == $TAG)') if [ -z "$TAG_EXISTS" ]; then - echo "Image ${IMAGE_NAME}:${{ inputs.image-tag }} does not exist." + echo "Image ${IMAGE_NAME}:${IMAGE_TAG} does not exist." exit 1 else - echo "Image ${IMAGE_NAME}:${{ inputs.image-tag }} exists." + echo "Image ${IMAGE_NAME}:${IMAGE_TAG} exists." + echo "::set-output name=image-tag::$IMAGE_TAG"}" fi deploy: @@ -40,7 +44,7 @@ jobs: with: environment: Dev docker-compose-file: "./docker-compose.prod.yml" - image-tag: ${{ inputs.image-tag }} + image-tag: ${{ needs.check-if-image-exists.outputs.image-tag }} env-vars: | DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} From d157abb135fc6e06e1fb82587b66272da4a7812e Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:09:31 +0100 Subject: [PATCH 04/20] update workflow --- .github/workflows/deploy-dev.yml | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 6091fa4f..f96b44a9 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -11,18 +11,35 @@ jobs: check-if-image-exists: runs-on: ubuntu-latest steps: - - name: Retrieve PR number - id: pr-number + - name: Retrieve Image Tag + id: retrieve-image-tag run: | + if [ -n "${{ inputs.image-tag }}" ]; then + echo "::set-output name=image-tag::${{ inputs.image-tag }}" + exit 0 + fi + REF=$(echo "${{ github.event.ref.ref }}" | sed -n 's#^refs/heads/##p') + # If ref = main + if [ "$REF" = "main" ]; then + echo "::set-output name=image-tag::latest" + fi + PULLS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${REF}") PR_NUMBER=$(echo "$PULLS" | jq -r '.[0].number') - echo "::set-output name=pr-number::$PR_NUMBER" + + if [ -z "$PR_NUMBER" ]; then + echo "No PR found for branch $REF." + exit 1 + else + echo "PR #$PR_NUMBER found for branch $REF." + echo "::set-output name=image-tag::pr-$PR_NUMBER" + fi - name: Check if image exists run: | IMAGE_NAME=ls1intum/apollon_standalone - IMAGE_TAG=${{ inputs.image-tag }} || "pr-${{ steps.pr-number.outputs.pr-number }}" + IMAGE_TAG="${{ steps.retrieve-image-tag.outputs.image-tag }}" ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64) TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \ @@ -34,7 +51,6 @@ jobs: exit 1 else echo "Image ${IMAGE_NAME}:${IMAGE_TAG} exists." - echo "::set-output name=image-tag::$IMAGE_TAG"}" fi deploy: @@ -44,7 +60,7 @@ jobs: with: environment: Dev docker-compose-file: "./docker-compose.prod.yml" - image-tag: ${{ needs.check-if-image-exists.outputs.image-tag }} + image-tag: ${{ needs.retrieve-image-tag.outputs.image-tag }} env-vars: | DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} From 2739de1f65fd731a7971fce523666dbe2add947d Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:14:18 +0100 Subject: [PATCH 05/20] fix set-output --- .github/workflows/build-and-push-shared.yml | 2 +- .github/workflows/deploy-dev.yml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-and-push-shared.yml b/.github/workflows/build-and-push-shared.yml index 7a7047ec..99813bac 100644 --- a/.github/workflows/build-and-push-shared.yml +++ b/.github/workflows/build-and-push-shared.yml @@ -80,7 +80,7 @@ jobs: - name: Set image tag output id: set-tag - run: echo "::set-output name=image-tag::${{ steps.meta.outputs.version }}" + run: echo "image-tag=${{ steps.meta.outputs.version }}" >> $GITHUB_OUTPUT - name: Build and push Docker Image uses: docker/build-push-action@v6 diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index f96b44a9..b34da3a8 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -15,14 +15,14 @@ jobs: id: retrieve-image-tag run: | if [ -n "${{ inputs.image-tag }}" ]; then - echo "::set-output name=image-tag::${{ inputs.image-tag }}" + echo "image-tag=${{ inputs.image-tag }}" >> $GITHUB_OUTPUT exit 0 fi REF=$(echo "${{ github.event.ref.ref }}" | sed -n 's#^refs/heads/##p') # If ref = main if [ "$REF" = "main" ]; then - echo "::set-output name=image-tag::latest" + echo "image-tag=latest" >> $GITHUB_OUTPUT fi PULLS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${REF}") @@ -33,7 +33,7 @@ jobs: exit 1 else echo "PR #$PR_NUMBER found for branch $REF." - echo "::set-output name=image-tag::pr-$PR_NUMBER" + echo "image-tag=pr-$PR_NUMBER" >> $GITHUB_OUTPUT fi - name: Check if image exists From b6ce8173dd037fd16d1f62589b456e7de194dc8b Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:15:59 +0100 Subject: [PATCH 06/20] fix ref --- .github/workflows/deploy-dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index b34da3a8..624e40bd 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -19,7 +19,7 @@ jobs: exit 0 fi - REF=$(echo "${{ github.event.ref.ref }}" | sed -n 's#^refs/heads/##p') + REF=$(echo "${{ github.event.ref }}" | sed -n 's#^refs/heads/##p') # If ref = main if [ "$REF" = "main" ]; then echo "image-tag=latest" >> $GITHUB_OUTPUT From a7a4422fb232165cb3a339bb870271e09ad232df Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:33:27 +0100 Subject: [PATCH 07/20] log inputs --- .github/workflows/deploy-docker-compose-shared.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index a5e2827c..2bca54f3 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -27,6 +27,13 @@ jobs: environment: name: ${{ inputs.environment }} steps: + - name: Log inputs + run: | + echo "Environment: ${{ inputs.environment }}" + echo "Docker Compose File: ${{ inputs.docker-compose-file }}" + echo "Image Tag: ${{ inputs.image-tag }}" + echo "Environment Variables: ${{ inputs.env-vars }}" + - name: Checkout Repository uses: actions/checkout@v4 From 0bd35c03f0a49c0098d24f6bf477456501bf709a Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:38:24 +0100 Subject: [PATCH 08/20] fix workflow --- .github/workflows/deploy-dev.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 624e40bd..54b65f22 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -10,6 +10,8 @@ on: jobs: check-if-image-exists: runs-on: ubuntu-latest + outputs: + image-tag: ${{ steps.retrieve-image-tag.outputs.image-tag }} steps: - name: Retrieve Image Tag id: retrieve-image-tag @@ -60,7 +62,7 @@ jobs: with: environment: Dev docker-compose-file: "./docker-compose.prod.yml" - image-tag: ${{ needs.retrieve-image-tag.outputs.image-tag }} + image-tag: ${{ needs.check-if-image-exists.outputs.image-tag }} env-vars: | DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} From aa6611d254f0a8e8c883feb950b1bf880bd9ff2a Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:43:21 +0100 Subject: [PATCH 09/20] add debug log --- .github/workflows/deploy-dev.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 54b65f22..495a3566 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -13,6 +13,12 @@ jobs: outputs: image-tag: ${{ steps.retrieve-image-tag.outputs.image-tag }} steps: + - name: Debug Environment Variables + run: | + echo "DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }}" + echo "APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }}" + + - name: Retrieve Image Tag id: retrieve-image-tag run: | From fe44644e846480a6fb0ed5b454ffb5dbe18bd4e2 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:47:36 +0100 Subject: [PATCH 10/20] try fix? --- .github/workflows/deploy-dev.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 495a3566..4920f723 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -10,6 +10,7 @@ on: jobs: check-if-image-exists: runs-on: ubuntu-latest + environment: Dev outputs: image-tag: ${{ steps.retrieve-image-tag.outputs.image-tag }} steps: From 98f9a40ca365cc82cd884f3b569745140f3757f1 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:50:43 +0100 Subject: [PATCH 11/20] fix env --- .github/workflows/deploy-dev.yml | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 4920f723..9e4fa6fc 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -8,18 +8,16 @@ on: description: "The tag of the docker images to deploy (default: pr- of branch or latest)" jobs: - check-if-image-exists: + prepare-deploy: runs-on: ubuntu-latest environment: Dev outputs: image-tag: ${{ steps.retrieve-image-tag.outputs.image-tag }} + env-vars: | + DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} + APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} + steps: - - name: Debug Environment Variables - run: | - echo "DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }}" - echo "APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }}" - - - name: Retrieve Image Tag id: retrieve-image-tag run: | @@ -63,14 +61,12 @@ jobs: fi deploy: - needs: check-if-image-exists + needs: prepare-deploy # TODO: uses: ls1intum/.github/.github/workflows/deploy-docker-compose.yml@main uses: ./.github/workflows/deploy-docker-compose-shared.yml with: environment: Dev docker-compose-file: "./docker-compose.prod.yml" - image-tag: ${{ needs.check-if-image-exists.outputs.image-tag }} - env-vars: | - DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} - APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} + image-tag: ${{ needs.prepare-deploy.outputs.image-tag }} + env-vars: ${{ needs.prepare-deploy.outputs.env-vars }} secrets: inherit From ba44a103c4dca547ac001e67af9c8cd41fdb1a7b Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 17:51:37 +0100 Subject: [PATCH 12/20] fix description --- .github/workflows/deploy-dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 9e4fa6fc..990eaaa6 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -5,7 +5,7 @@ on: inputs: image-tag: type: string - description: "The tag of the docker images to deploy (default: pr- of branch or latest)" + description: "Image tag to deploy (default: pr- if PR exists, latest for main)" jobs: prepare-deploy: From 4482343ecc5bd71c44cbf41bcdb4c792ae6f2bcd Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:02:46 +0100 Subject: [PATCH 13/20] ignore env --- .../workflows/deploy-docker-compose-shared.yml | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index 2bca54f3..6b014a07 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -27,13 +27,6 @@ jobs: environment: name: ${{ inputs.environment }} steps: - - name: Log inputs - run: | - echo "Environment: ${{ inputs.environment }}" - echo "Docker Compose File: ${{ inputs.docker-compose-file }}" - echo "Image Tag: ${{ inputs.image-tag }}" - echo "Environment Variables: ${{ inputs.env-vars }}" - - name: Checkout Repository uses: actions/checkout@v4 @@ -56,13 +49,7 @@ jobs: # Check if docker-compose.prod.yml exists if [ -f "$COMPOSE_FILE" ]; then echo "$COMPOSE_FILE found." - - # Check if .env exists - if [ -f ".env" ]; then - docker compose -f "$COMPOSE_FILE" --env-file=".env" down --remove-orphans --rmi all - else - docker compose -f "$COMPOSE_FILE" down --remove-orphans --rmi all - fi + docker compose -f "$COMPOSE_FILE" down --remove-orphans --rmi all else echo "$COMPOSE_FILE does not exist. Skipping docker compose down." fi From 2ec3106aa6ba4f56c6da50ef45d0a5e855be72b8 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:24:13 +0100 Subject: [PATCH 14/20] generalize workflow --- .github/workflows/deploy-dev.yml | 57 ++------------ .../deploy-docker-compose-shared.yml | 76 +++++++++++++++++-- docker-compose.prod.yml | 2 - 3 files changed, 75 insertions(+), 60 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 990eaaa6..99f41dde 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -5,68 +5,25 @@ on: inputs: image-tag: type: string - description: "Image tag to deploy (default: pr- if PR exists, latest for main)" + description: "Image tag to deploy (default: pr- if PR exists, latest for default branch)" -jobs: - prepare-deploy: +jobs: + prepare-env: runs-on: ubuntu-latest environment: Dev outputs: - image-tag: ${{ steps.retrieve-image-tag.outputs.image-tag }} env-vars: | DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} - - steps: - - name: Retrieve Image Tag - id: retrieve-image-tag - run: | - if [ -n "${{ inputs.image-tag }}" ]; then - echo "image-tag=${{ inputs.image-tag }}" >> $GITHUB_OUTPUT - exit 0 - fi - - REF=$(echo "${{ github.event.ref }}" | sed -n 's#^refs/heads/##p') - # If ref = main - if [ "$REF" = "main" ]; then - echo "image-tag=latest" >> $GITHUB_OUTPUT - fi - - PULLS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${REF}") - PR_NUMBER=$(echo "$PULLS" | jq -r '.[0].number') - - if [ -z "$PR_NUMBER" ]; then - echo "No PR found for branch $REF." - exit 1 - else - echo "PR #$PR_NUMBER found for branch $REF." - echo "image-tag=pr-$PR_NUMBER" >> $GITHUB_OUTPUT - fi - - - name: Check if image exists - run: | - IMAGE_NAME=ls1intum/apollon_standalone - IMAGE_TAG="${{ steps.retrieve-image-tag.outputs.image-tag }}" - - ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64) - TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \ - https://ghcr.io/v2/${IMAGE_NAME}/tags/list \ - | jq -r --arg TAG "${IMAGE_TAG}" '.tags[] | select(. == $TAG)') - - if [ -z "$TAG_EXISTS" ]; then - echo "Image ${IMAGE_NAME}:${IMAGE_TAG} does not exist." - exit 1 - else - echo "Image ${IMAGE_NAME}:${IMAGE_TAG} exists." - fi deploy: - needs: prepare-deploy + needs: prepare-env # TODO: uses: ls1intum/.github/.github/workflows/deploy-docker-compose.yml@main uses: ./.github/workflows/deploy-docker-compose-shared.yml with: environment: Dev docker-compose-file: "./docker-compose.prod.yml" - image-tag: ${{ needs.prepare-deploy.outputs.image-tag }} - env-vars: ${{ needs.prepare-deploy.outputs.env-vars }} + main-image-name: ls1intum/apollon_standalone + image-tag: ${{ inputs.image-tag }} + env-vars: ${{ needs.prepare-env.outputs.env-vars }} secrets: inherit diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index 6b014a07..3f5412df 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -12,25 +12,85 @@ on: type: string default: "./docker-compose.yml" description: "Path to the Docker Compose file (Default: ./docker-compose.yml)" + main-image-name: + type: string + description: "The name of the main image for checking if it exists with the given tag" + required: true image-tag: type: string - default: latest - description: "Tag of the Docker images to deploy (Default: latest)" + description: "Image tag to deploy (default: pr- if PR exists, latest for default branch)" env-vars: type: string description: "Additional environment variables in KEY=VALUE format, separated by newlines" required: false jobs: + prepare-deploy: + runs-on: ubuntu-latest + environment: Dev + outputs: + image-tag-to-deploy: ${{ steps.retrieve-image-tag.outputs.image-tag }} + + steps: + - name: Log env vars + run: | + echo "ENVIRONMENT=${{ inputs.environment }}" + echo "DOCKER_COMPOSE_FILE=${{ inputs.docker-compose-file }}" + echo "MAIN_IMAGE_NAME=${{ inputs.main-image-name }}" + echo "IMAGE_TAG=${{ inputs.image-tag }}" + echo "ENV_VARS=${{ inputs.env-vars }}" + + - name: Retrieve image tag to deploy + id: retrieve-image-tag + run: | + if [ -n "${{ inputs.image-tag }}" ]; then + echo "image-tag-to-deploy=${{ inputs.image-tag }}" >> $GITHUB_OUTPUT + exit 0 + fi + + REF=$(echo "${{ github.event.ref }}" | sed -n 's#^refs/heads/##p') + if [ "$REF" = "${{ github.event.repository.default_branch }}" ]; then + echo "image-tag-to-deploy=latest" >> $GITHUB_OUTPUT + fi + + PULLS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${REF}") + PR_NUMBER=$(echo "$PULLS" | jq -r '.[0].number') + + if [ -z "$PR_NUMBER" ]; then + echo "No PR found for branch $REF." + exit 1 + else + echo "PR #$PR_NUMBER found for branch $REF." + echo "image-tag-to-deploy=pr-$PR_NUMBER" >> $GITHUB_OUTPUT + fi + + - name: Check if image exists + run: | + IMAGE_NAME="${{ inputs.main-image-name }}" + IMAGE_TAG="${{ steps.retrieve-image-tag.outputs.image-tag-to-deploy }}" + + ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64) + TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \ + https://ghcr.io/v2/${IMAGE_NAME}/tags/list \ + | jq -r --arg TAG "${IMAGE_TAG}" '.tags[] | select(. == $TAG)') + + if [ -z "$TAG_EXISTS" ]; then + echo "Image ${IMAGE_NAME}:${IMAGE_TAG} does not exist." + exit 1 + else + echo "Image ${IMAGE_NAME}:${IMAGE_TAG} exists." + fi + deploy: + needs: prepare-deploy runs-on: ubuntu-latest environment: name: ${{ inputs.environment }} steps: - - name: Checkout Repository + - name: Checkout repository uses: actions/checkout@v4 - - name: SSH to VM and Execute Docker-Compose Down (if exists) + - name: SSH to VM and execute docker compose down (if exists) uses: appleboy/ssh-action@v1.0.3 with: host: ${{ vars.VM_HOST }} @@ -54,7 +114,7 @@ jobs: echo "$COMPOSE_FILE does not exist. Skipping docker compose down." fi - - name: Copy Docker Compose File to VM Host + - name: Copy docker compose file to VM host uses: appleboy/scp-action@v0.1.7 with: host: ${{ vars.VM_HOST }} @@ -67,7 +127,7 @@ jobs: source: ${{ inputs.docker-compose-file }} target: /home/${{ vars.VM_USERNAME }} - - name: SSH to VM and Create .env File + - name: SSH to VM and create .env file uses: appleboy/ssh-action@v1.0.3 with: host: ${{ vars.VM_HOST }} @@ -81,12 +141,12 @@ jobs: touch .env echo "ENVIRONMENT=${{ inputs.environment }}" > .env - echo "IMAGE_TAG=${{ inputs.image-tag }}" >> .env + echo "IMAGE_TAG=${{ needs.prepare-deploy.outputs.image-tag-to-deploy }}" >> .env if [ "${{ inputs.env-vars }}" != "" ]; then echo "${{ inputs.env-vars }}" >> .env fi - - name: SSH to VM and Execute Docker Compose Up + - name: SSH to VM and execute docker compose up uses: appleboy/ssh-action@v1.0.3 with: host: ${{ vars.VM_HOST }} diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 54ac0cba..617e86b7 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,5 +1,3 @@ -version: '3.8' - services: redis: image: redis/redis-stack-server:7.4.0-v1 From 3ad8acedc79126c7cb81dbf26e4376b67626c15d Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:27:15 +0100 Subject: [PATCH 15/20] fix env --- .github/workflows/deploy-dev.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 99f41dde..2a2d72da 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -12,9 +12,13 @@ jobs: runs-on: ubuntu-latest environment: Dev outputs: - env-vars: | - DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} - APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} + env-vars: ${{ steps.set-env.outputs.env-vars }} + steps: + - name: Set env + id: set-env + run: | + echo "DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }}" >> $GITHUB_OUTPUT + echo "APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }}" >> $GITHUB_OUTPUT deploy: needs: prepare-env From 552e284a3e3393d327b51f112246c985d40be5a0 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:29:40 +0100 Subject: [PATCH 16/20] fix env --- .github/workflows/deploy-dev.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 2a2d72da..5b2a4b9d 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -12,13 +12,12 @@ jobs: runs-on: ubuntu-latest environment: Dev outputs: - env-vars: ${{ steps.set-env.outputs.env-vars }} + env-vars: | + DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }} + APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }} steps: - - name: Set env - id: set-env - run: | - echo "DEPLOYMENT_URL=${{ vars.DEPLOYMENT_URL }}" >> $GITHUB_OUTPUT - echo "APOLLON_REDIS_DIAGRAM_TTL=${{ vars.APOLLON_REDIS_DIAGRAM_TTL }}" >> $GITHUB_OUTPUT + - name: Do nothing + run: echo "Nothing to do here" deploy: needs: prepare-env From fabc3bf34019a150442392b3c028b9036ab1dcb7 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:30:52 +0100 Subject: [PATCH 17/20] remove log --- .github/workflows/deploy-docker-compose-shared.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index 3f5412df..004760e9 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -32,14 +32,6 @@ jobs: image-tag-to-deploy: ${{ steps.retrieve-image-tag.outputs.image-tag }} steps: - - name: Log env vars - run: | - echo "ENVIRONMENT=${{ inputs.environment }}" - echo "DOCKER_COMPOSE_FILE=${{ inputs.docker-compose-file }}" - echo "MAIN_IMAGE_NAME=${{ inputs.main-image-name }}" - echo "IMAGE_TAG=${{ inputs.image-tag }}" - echo "ENV_VARS=${{ inputs.env-vars }}" - - name: Retrieve image tag to deploy id: retrieve-image-tag run: | From e2802c8e0f4137eb735e2be3fac6ed2deb25f1e9 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:34:29 +0100 Subject: [PATCH 18/20] restore env --- .github/workflows/deploy-docker-compose-shared.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index 004760e9..dccacced 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -101,7 +101,13 @@ jobs: # Check if docker-compose.prod.yml exists if [ -f "$COMPOSE_FILE" ]; then echo "$COMPOSE_FILE found." - docker compose -f "$COMPOSE_FILE" down --remove-orphans --rmi all + + # Check if .env exists + if [ -f ".env" ]; then + docker compose -f "$COMPOSE_FILE" --env-file=".env" down --remove-orphans --rmi all + else + docker compose -f "$COMPOSE_FILE" down --remove-orphans --rmi all + fi else echo "$COMPOSE_FILE does not exist. Skipping docker compose down." fi From 87afe1b380175c363bf30896db5f338b0d18e617 Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 18:42:08 +0100 Subject: [PATCH 19/20] fix name --- .github/workflows/deploy-docker-compose-shared.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-docker-compose-shared.yml b/.github/workflows/deploy-docker-compose-shared.yml index dccacced..76821c14 100644 --- a/.github/workflows/deploy-docker-compose-shared.yml +++ b/.github/workflows/deploy-docker-compose-shared.yml @@ -29,7 +29,7 @@ jobs: runs-on: ubuntu-latest environment: Dev outputs: - image-tag-to-deploy: ${{ steps.retrieve-image-tag.outputs.image-tag }} + image-tag-to-deploy: ${{ steps.retrieve-image-tag.outputs.image-tag-to-deploy }} steps: - name: Retrieve image tag to deploy From 9d6a15dd2ee9796596ae50d5ca370f398fd94acc Mon Sep 17 00:00:00 2001 From: "Felix T.J. Dietrich" Date: Fri, 6 Dec 2024 21:28:50 +0100 Subject: [PATCH 20/20] mount volumes --- docker-compose.prod.yml | 2 ++ docker-compose.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 617e86b7..90b8ff7b 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -20,6 +20,8 @@ services: - APOLLON_REDIS_URL=redis://apollon-redis:6379 - APOLLON_REDIS_DIAGRAM_TTL=${APOLLON_REDIS_DIAGRAM_TTL} - DEPLOYMENT_URL=${DEPLOYMENT_URL} + volumes: + - /opt/apollon/diagrams:/app/diagrams restart: unless-stopped ports: - "8080:8080" diff --git a/docker-compose.yml b/docker-compose.yml index 92b0edc4..6bc6114e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,8 @@ services: - APOLLON_REDIS_URL=redis://apollon_redis:6379 - APOLLON_REDIS_DIAGRAM_TTL=${APOLLON_REDIS_DIAGRAM_TTL} - DEPLOYMENT_URL=${DEPLOYMENT_URL} + volumes: + - ./diagrams:/app/diagrams restart: always networks: - apollon_network