Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SENP-648] feat(ecs-deploy): improve consistency of workflows parameters #83

Merged
merged 9 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions .github/workflows/ecs-deploy-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +38,13 @@ on:
terraform_version:
type: string
required: false
use_version_as_docker_image_tag:
type: boolean
default: true
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the required block to make the behaviour explicit here, I admit I don't remember which is the default and we add it whether we set it to false or true in the other fields.

required: false
version:
type: string
required: false
wait_for_stabilization:
type: boolean
default: true
Expand All @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions actions/lookup-git-hash/action.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions actions/lookup-git-tag/action.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 11 additions & 3 deletions actions/notify-deployment-in-progress/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}

Expand Down