From 70718d6becdc64b351b189c774080f610bbba826 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Wed, 10 Apr 2024 17:52:42 +0200 Subject: [PATCH 1/9] feat(ecs-deploy): make docker_image_tag optionnal --- .github/workflows/ecs-deploy-v2.yml | 43 ++++++++++++++++--- README.md | 4 +- actions/lookup-git-hash/action.yaml | 13 ++++++ actions/lookup-git-tag/action.yaml | 13 ++++++ .../notify-deployment-in-progress/action.yaml | 12 ++++-- 5 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 actions/lookup-git-hash/action.yaml create mode 100644 actions/lookup-git-tag/action.yaml diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index 298ee5f..6968d91 100644 --- a/.github/workflows/ecs-deploy-v2.yml +++ b/.github/workflows/ecs-deploy-v2.yml @@ -12,7 +12,8 @@ on: default: "" docker_image_tag: type: string - required: true + required: false + description: "DEPRECATED: use version instead" environment: type: string required: true @@ -37,6 +38,12 @@ on: terraform_version: type: string required: false + use_version_as_docker_image_tag: + type: boolean + default: true + version: + type: string + required: false wait_for_stabilization: type: boolean default: true @@ -90,27 +97,49 @@ jobs: - name: Terraform validate run: terraform validate - - name: Get current version - id: current_version + - name: Get former version from ECS + if: inputs.docker_image_tag || inputs.use_version_as_docker_image_tag + id: ecs_lookup run: | CURRENT_TASK_DEFINITION="$(aws ecs list-task-definitions | jq --arg SERVICE "$SERVICE" --arg ENVIRONMENT "$ENV" -r -c '.taskDefinitionArns[] | select(contains($SERVICE + "-" + $ENVIRONMENT))')" CURRENT_IMAGE_TAG="$(aws ecs describe-task-definition --task-definition "$CURRENT_TASK_DEFINITION" | jq --arg SERVICE "$SERVICE" -r -c '.taskDefinition.containerDefinitions[] | select(.name == $SERVICE) | .image' | cut -d':' -f2)" echo "image_tag=$CURRENT_IMAGE_TAG" >> "$GITHUB_OUTPUT" + + - name: Get former version from Git + id: git_hash + if: "! (inputs.docker_image_tag || inputs.use_version_as_docker_image_tag)" + uses: sencrop/github-workflows/actions/look-git-hash@master + - name: Notify deployment in progress uses: sencrop/github-workflows/actions/notify-deployment-in-progress@master with: service: ${{ inputs.service }} environment: ${{ inputs.environment }} dd_api_key: ${{ secrets.DD_API_KEY }} - current_version: ${{ steps.current_version.outputs.image_tag }} - deployed_version: ${{ inputs.docker_image_tag }} + former_version: ${{ steps.ecs_lookup.outputs.image_tag || steps.git_hash.output.previous }} + new_version: ${{ inputs.docker_image_tag || steps.git_hash.output.current }} slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }} + # TODO: remove this step once docker_image_tag has been removed + - name: Build tf_vars (using docker_image_tag) + if: inputs.docker_image_tag + id: tf_vars_deprecated + run: | + TF_VAR_DOCKER_IMAGE_TAG="-var docker_image_tag=${{ inputs.docker_image_tag }}" + echo "docker_image_tag=$TF_VAR_DOCKER_IMAGE_TAG" >> "$GITHUB_OUTPUT" + + - name: Build tf_vars (using version) + if: "! inputs.docker_image_tag && inputs.use_version_as_docker_image_tag" + id: tf_vars + run: | + TF_VAR_DOCKER_IMAGE_TAG="-var docker_image_tag=${{ inputs.version }}" + echo "docker_image_tag=$TF_VAR_DOCKER_IMAGE_TAG" >> "$GITHUB_OUTPUT" + + - name: Terraform apply run: | - # shellcheck disable=SC2086 - terraform apply -var "docker_image_tag=${DOCKER_IMAGE_TAG}" -var-file="${ENV}.tfvars" -auto-approve -input=false ${EXTRA_ARGS} + terraform apply ${{ steps.tf_vars.output.docker_image_tag || steps.tf_vars_deprecated.output.docker_image_tag }} -var-file ${ENV}.tfvars -auto-approve -input=false ${EXTRA_ARGS} - name: Wait for stabilization if: ${{ inputs.wait_for_stabilization }} diff --git a/README.md b/README.md index 2a29c02..591adc2 100644 --- a/README.md +++ b/README.md @@ -127,12 +127,14 @@ jobs: uses: sencrop/github-workflows/.github/workflows/ecs-deploy-v2.yml@master secrets: inherit with: - docker_image_tag: tag-from-the-build-step + version: some-version environment: "preproduction or production" service: my-service slack_channel: my-ops-slack-channel ``` +If your service uses a static docker image tag you may set the flag `use_version_as_docker_image_tag` to `false`. + ### ecs-start diff --git a/actions/lookup-git-hash/action.yaml b/actions/lookup-git-hash/action.yaml new file mode 100644 index 0000000..d68768b --- /dev/null +++ b/actions/lookup-git-hash/action.yaml @@ -0,0 +1,13 @@ +--- +name: "Lookup git hash" + +runs: + using: "composite" + steps: + - name: Lookup Git hash + id: git_hash + run: | + REF="$(git rev-parse --short HEAD)" + PREVIOUS_REF="$(git rev-parse --short HEAD~1)" + echo "current=$REF" >> "${GITHUB_OUTPUT}" + echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" diff --git a/actions/lookup-git-tag/action.yaml b/actions/lookup-git-tag/action.yaml new file mode 100644 index 0000000..c37dfa2 --- /dev/null +++ b/actions/lookup-git-tag/action.yaml @@ -0,0 +1,13 @@ +--- +name: "Lookup git tag" + +runs: + using: "composite" + steps: + - name: Get version from tags + id: git_tag + run: | + REF="${GITHUB_REF#refs/tags/}" + PREVIOUS_REF="$(git tag -l 'v*' --sort=-v:refname | head -n2 | tail -n1)" + echo "current=$REF" >> "${GITHUB_OUTPUT}" + echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" diff --git a/actions/notify-deployment-in-progress/action.yaml b/actions/notify-deployment-in-progress/action.yaml index c54dc0c..cdb983d 100644 --- a/actions/notify-deployment-in-progress/action.yaml +++ b/actions/notify-deployment-in-progress/action.yaml @@ -5,16 +5,22 @@ description: "Track a deployment in progress" inputs: current_version: type: string - required: true + description: "DEPRECATED: use new_version instead" dd_api_key: type: string required: true deployed_version: type: string - required: true + description: "DEPRECATED: use former_version instead" + former_version: + type: string + deployed_version: + type: string environment: type: string required: true + new_version: + type: string service: type: string required: true @@ -35,7 +41,7 @@ runs: if: inputs.environment == 'production' && inputs.slack_notify == 'true' with: channel-id: ${{ inputs.slack_channel }} - slack-message: ":ship: New deployment of `${{ inputs.service }}` (version `${{ inputs.deployed_version }}`) in progress (<${{ github.server_url }}/${{ github.repository }}/compare/${{ inputs.current_version }}...${{ inputs.deployed_version }}|CHANGELOG>)" + slack-message: ":ship: New deployment of `${{ inputs.service }}` (version `${{ inputs.new_version || inputs.deployed_version }}`) in progress (<${{ github.server_url }}/${{ github.repository }}/compare/${{ inputs.former_version || inputs.current_version }}...${{ inputs.new_version || inputs.deployed_version }}|CHANGELOG>)" env: SLACK_BOT_TOKEN: ${{ inputs.SLACK_BOT_TOKEN }} From 4d1fada6a80a77d29c3356a2dfc4b28e0bdcc886 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 11:47:32 +0200 Subject: [PATCH 2/9] fix: issue reported by linter --- .github/workflows/ecs-deploy-v2.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index 6968d91..47245f3 100644 --- a/.github/workflows/ecs-deploy-v2.yml +++ b/.github/workflows/ecs-deploy-v2.yml @@ -117,8 +117,8 @@ jobs: service: ${{ inputs.service }} environment: ${{ inputs.environment }} dd_api_key: ${{ secrets.DD_API_KEY }} - former_version: ${{ steps.ecs_lookup.outputs.image_tag || steps.git_hash.output.previous }} - new_version: ${{ inputs.docker_image_tag || steps.git_hash.output.current }} + former_version: ${{ steps.ecs_lookup.outputs.image_tag || steps.git_hash.outputs.previous }} + new_version: ${{ inputs.docker_image_tag || steps.git_hash.outputs.current }} slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }} # TODO: remove this step once docker_image_tag has been removed @@ -139,7 +139,8 @@ jobs: - name: Terraform apply run: | - terraform apply ${{ steps.tf_vars.output.docker_image_tag || steps.tf_vars_deprecated.output.docker_image_tag }} -var-file ${ENV}.tfvars -auto-approve -input=false ${EXTRA_ARGS} + # shellcheck disable=SC2086 + terraform apply ${{ steps.tf_vars.outputs.docker_image_tag || steps.tf_vars_deprecated.outputs.docker_image_tag }} -var-file=${ENV}.tfvars -auto-approve -input=false ${EXTRA_ARGS} - name: Wait for stabilization if: ${{ inputs.wait_for_stabilization }} From 9c6e0457e593bfce30f6816df5a16492536b2423 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 11:53:18 +0200 Subject: [PATCH 3/9] fix: set explicitly required --- .github/workflows/ecs-deploy-v2.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index 47245f3..0091661 100644 --- a/.github/workflows/ecs-deploy-v2.yml +++ b/.github/workflows/ecs-deploy-v2.yml @@ -41,6 +41,7 @@ on: use_version_as_docker_image_tag: type: boolean default: true + required: false version: type: string required: false @@ -109,7 +110,7 @@ jobs: - name: Get former version from Git id: git_hash if: "! (inputs.docker_image_tag || inputs.use_version_as_docker_image_tag)" - uses: sencrop/github-workflows/actions/look-git-hash@master + uses: sencrop/github-workflows/actions/lookup-git-hash@master - name: Notify deployment in progress uses: sencrop/github-workflows/actions/notify-deployment-in-progress@master From 643d8988f5f751175788c3cf2c6935a4256c9b9d Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 17:02:56 +0200 Subject: [PATCH 4/9] fix --- actions/lookup-git-hash/action.yaml | 1 + actions/lookup-git-tag/action.yaml | 1 + 2 files changed, 2 insertions(+) diff --git a/actions/lookup-git-hash/action.yaml b/actions/lookup-git-hash/action.yaml index d68768b..b25b72e 100644 --- a/actions/lookup-git-hash/action.yaml +++ b/actions/lookup-git-hash/action.yaml @@ -11,3 +11,4 @@ runs: PREVIOUS_REF="$(git rev-parse --short HEAD~1)" echo "current=$REF" >> "${GITHUB_OUTPUT}" echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" + shell: bash diff --git a/actions/lookup-git-tag/action.yaml b/actions/lookup-git-tag/action.yaml index c37dfa2..e9ccd51 100644 --- a/actions/lookup-git-tag/action.yaml +++ b/actions/lookup-git-tag/action.yaml @@ -11,3 +11,4 @@ runs: PREVIOUS_REF="$(git tag -l 'v*' --sort=-v:refname | head -n2 | tail -n1)" echo "current=$REF" >> "${GITHUB_OUTPUT}" echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" + shell: bash From a647d399f41373099a6bc5c5733c01f79bfdcc90 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 17:14:10 +0200 Subject: [PATCH 5/9] fix --- .github/workflows/ecs-deploy-v2.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index 0091661..dd2644d 100644 --- a/.github/workflows/ecs-deploy-v2.yml +++ b/.github/workflows/ecs-deploy-v2.yml @@ -63,7 +63,6 @@ jobs: CI_IAM_ROLE: ${{ inputs.ci_iam_role }} DOCKER_IMAGE_TAG: ${{ inputs.docker_image_tag }} ENV: ${{ inputs.environment }} - EXTRA_ARGS: ${{ inputs.extra_args }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_REPOSITORY_URL: ${{ github.server_url }}/${{ github.repository }} GITHUB_ACTIONS_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} @@ -141,7 +140,7 @@ jobs: - name: Terraform apply run: | # shellcheck disable=SC2086 - terraform apply ${{ steps.tf_vars.outputs.docker_image_tag || steps.tf_vars_deprecated.outputs.docker_image_tag }} -var-file=${ENV}.tfvars -auto-approve -input=false ${EXTRA_ARGS} + terraform apply ${{ steps.tf_vars.outputs.docker_image_tag || steps.tf_vars_deprecated.outputs.docker_image_tag }} -var-file=${{ inputs.environment }}.tfvars -auto-approve -input=false ${{ inputs.extra_args }} - name: Wait for stabilization if: ${{ inputs.wait_for_stabilization }} From 56c109013228516d242a106ace34092ad9271a8d Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 17:23:30 +0200 Subject: [PATCH 6/9] fix --- actions/notify-deployment-in-progress/action.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/actions/notify-deployment-in-progress/action.yaml b/actions/notify-deployment-in-progress/action.yaml index cdb983d..df71296 100644 --- a/actions/notify-deployment-in-progress/action.yaml +++ b/actions/notify-deployment-in-progress/action.yaml @@ -14,8 +14,6 @@ inputs: description: "DEPRECATED: use former_version instead" former_version: type: string - deployed_version: - type: string environment: type: string required: true From 89498b64a342620e70b030d1bfe935963e50b864 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 17:57:49 +0200 Subject: [PATCH 7/9] fix --- actions/notify-deployment-in-progress/action.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/actions/notify-deployment-in-progress/action.yaml b/actions/notify-deployment-in-progress/action.yaml index df71296..476b329 100644 --- a/actions/notify-deployment-in-progress/action.yaml +++ b/actions/notify-deployment-in-progress/action.yaml @@ -6,19 +6,23 @@ inputs: current_version: type: string description: "DEPRECATED: use new_version instead" + required: false dd_api_key: type: string required: true deployed_version: type: string description: "DEPRECATED: use former_version instead" + required: false former_version: type: string + required: false environment: type: string required: true new_version: type: string + required: false service: type: string required: true From 8d53dfb349afcdc2b446933c3a507998a092b30a Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 18:01:11 +0200 Subject: [PATCH 8/9] fix --- .github/workflows/ecs-deploy-v2.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index dd2644d..fec32dd 100644 --- a/.github/workflows/ecs-deploy-v2.yml +++ b/.github/workflows/ecs-deploy-v2.yml @@ -118,7 +118,7 @@ jobs: environment: ${{ inputs.environment }} dd_api_key: ${{ secrets.DD_API_KEY }} former_version: ${{ steps.ecs_lookup.outputs.image_tag || steps.git_hash.outputs.previous }} - new_version: ${{ inputs.docker_image_tag || steps.git_hash.outputs.current }} + new_version: ${{ inputs.docker_image_tag || inputs.version }} slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }} # TODO: remove this step once docker_image_tag has been removed From dc6592bd2d2b3ad5ceb9b32d31eabe9b043a6b07 Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Mon, 15 Apr 2024 18:08:01 +0200 Subject: [PATCH 9/9] fix --- actions/lookup-git-hash/action.yaml | 2 +- actions/lookup-git-tag/action.yaml | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/actions/lookup-git-hash/action.yaml b/actions/lookup-git-hash/action.yaml index b25b72e..a616df0 100644 --- a/actions/lookup-git-hash/action.yaml +++ b/actions/lookup-git-hash/action.yaml @@ -4,7 +4,7 @@ name: "Lookup git hash" runs: using: "composite" steps: - - name: Lookup Git hash + - name: Lookup current and previous Git hash id: git_hash run: | REF="$(git rev-parse --short HEAD)" diff --git a/actions/lookup-git-tag/action.yaml b/actions/lookup-git-tag/action.yaml index e9ccd51..ccedda4 100644 --- a/actions/lookup-git-tag/action.yaml +++ b/actions/lookup-git-tag/action.yaml @@ -4,11 +4,11 @@ name: "Lookup git tag" runs: using: "composite" steps: - - name: Get version from tags - id: git_tag - run: | - REF="${GITHUB_REF#refs/tags/}" - PREVIOUS_REF="$(git tag -l 'v*' --sort=-v:refname | head -n2 | tail -n1)" - echo "current=$REF" >> "${GITHUB_OUTPUT}" - echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" - shell: bash + - name: Lookup current and previous Git tag + id: git_tag + run: | + REF="${GITHUB_REF#refs/tags/}" + PREVIOUS_REF="$(git tag -l 'v*' --sort=-v:refname | head -n2 | tail -n1)" + echo "current=$REF" >> "${GITHUB_OUTPUT}" + echo "previous=$PREVIOUS_REF" >> "${GITHUB_OUTPUT}" + shell: bash