Skip to content

Commit

Permalink
OPHLUDOS-203-5: Deploy built image to ECR
Browse files Browse the repository at this point in the history
  • Loading branch information
anttu committed Jul 4, 2024
1 parent 9c74eea commit 31ab2a2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
32 changes: 32 additions & 0 deletions .github/actions/deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Deploy to environment
description: Copy image to ECR and deploy CDK
inputs:
env:
description: 'Environment for deployment'
required: true

runs:
using: composite
steps:
- name: Fetch history for all branches and tags
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Login to GitHub Container Registry
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Configure utility AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::505953557276:role/ludos-gh-actions-ecr-push-role
aws-region: eu-west-1

- name: 05-deploy.sh
working-directory: ./
shell: bash
run: ./deploy-scripts/05-deploy-${{ inputs.env }}.sh
13 changes: 12 additions & 1 deletion .github/workflows/build_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ jobs:
steps:
- uses: actions/[email protected]
- name: Run 04-lint.sh
run: ./deploy-scripts/04-lint.sh
run: ./deploy-scripts/04-lint.sh

deploy:
name: '05-deploy.sh'
permissions:
packages: read
runs-on: "ubuntu-22.04"
steps:
- name: Deploy
uses: ./.github/actions/deploy
with:
env: 'dev'
38 changes: 38 additions & 0 deletions deploy-scripts/05-deploy-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail

# shellcheck source=../scripts/common-functions.sh
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/../scripts/common-functions.sh"

# shellcheck source=./deploy-functions.sh
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/deploy-functions.sh"

function main() {
parse_env_from_script_name 05-deploy
setup

start_gh_actions_group "Deploying to ${ENV}"
upload_image_to_ecr
}

function upload_image_to_ecr() {
require_built_image

docker tag "${github_image_tag}" "${ecr_image_tag}"
docker push "${ecr_image_tag}"
}

function setup() {
cd "${repo}"
require_command docker
require_docker_compose
configure_aws_credentials
get_ecr_login_credentials
}

function get_ecr_login_credentials() {
aws --profile oph-ludos-utility \
ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 505953557276.dkr.ecr.eu-west-1.amazonaws.com
}

main "$@"
8 changes: 4 additions & 4 deletions deploy-scripts/deploy-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ function image_exists_locally {
}

function require_built_image {
if image_exists_locally "$image_tag"; then
info "$image_tag already exists locally"
if image_exists_locally "${github_image_tag}"; then
info "${github_image_tag} already exists locally"
else
info "Pulling $image_tag because it does not exist locally"
docker pull "$image_tag"
info "Pulling ${github_image_tag} because it does not exist locally"
docker pull "${github_image_tag}"
fi
}

0 comments on commit 31ab2a2

Please sign in to comment.