From 89c2952514140c5541ea19543b0ece84d5a0ffda Mon Sep 17 00:00:00 2001 From: Jerome Dassonville Date: Wed, 10 Apr 2024 17:52:42 +0200 Subject: [PATCH] feat(ecs-deploy): make docker_image_tag optionnal --- .github/workflows/ecs-deploy-v2.yml | 42 +++++++++++++++---- 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, 73 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..86ee970 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 + require: false wait_for_stabilization: type: boolean default: true @@ -90,27 +97,48 @@ 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 + 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 + run : | + TV_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 : | + TV_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 }} -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 }}