diff --git a/.github/workflows/ecs-deploy-v2.yml b/.github/workflows/ecs-deploy-v2.yml index 298ee5f..fec32dd 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,13 @@ on: terraform_version: type: string required: false + use_version_as_docker_image_tag: + type: boolean + default: true + required: false + version: + type: string + required: false wait_for_stabilization: type: boolean default: true @@ -55,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 }} @@ -90,27 +97,50 @@ 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/lookup-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.outputs.previous }} + 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 + - 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.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 }} 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..a616df0 --- /dev/null +++ b/actions/lookup-git-hash/action.yaml @@ -0,0 +1,14 @@ +--- +name: "Lookup git hash" + +runs: + using: "composite" + steps: + - name: Lookup current and previous 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}" + shell: bash diff --git a/actions/lookup-git-tag/action.yaml b/actions/lookup-git-tag/action.yaml new file mode 100644 index 0000000..ccedda4 --- /dev/null +++ b/actions/lookup-git-tag/action.yaml @@ -0,0 +1,14 @@ +--- +name: "Lookup git tag" + +runs: + using: "composite" + steps: + - 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 diff --git a/actions/notify-deployment-in-progress/action.yaml b/actions/notify-deployment-in-progress/action.yaml index c54dc0c..476b329 100644 --- a/actions/notify-deployment-in-progress/action.yaml +++ b/actions/notify-deployment-in-progress/action.yaml @@ -5,16 +5,24 @@ description: "Track a deployment in progress" inputs: current_version: type: string - required: true + description: "DEPRECATED: use new_version instead" + required: false dd_api_key: type: string required: true deployed_version: type: string - required: true + 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 @@ -35,7 +43,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 }}