|
| 1 | +name: cdk release code |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + TARGET_ENVIRONMENT: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + VERSION_NUMBER: |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + COMMIT_ID: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + LOG_RETENTION_IN_DAYS: |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + secrets: |
| 19 | + CLOUD_FORMATION_DEPLOY_ROLE: |
| 20 | + required: true |
| 21 | + CDK_PULL_IMAGE_ROLE: |
| 22 | + required: true |
| 23 | +jobs: |
| 24 | + release_code: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + environment: ${{ inputs.TARGET_ENVIRONMENT }} |
| 27 | + name: deploy cdk app |
| 28 | + permissions: |
| 29 | + id-token: write |
| 30 | + contents: write |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout local github actions |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + ref: ${{ env.BRANCH_NAME }} |
| 37 | + fetch-depth: 0 |
| 38 | + sparse-checkout: | |
| 39 | + .github |
| 40 | +
|
| 41 | + - name: Configure AWS Credentials |
| 42 | + id: connect-aws-pull-image |
| 43 | + uses: aws-actions/configure-aws-credentials@v4 |
| 44 | + with: |
| 45 | + aws-region: eu-west-2 |
| 46 | + role-to-assume: ${{ secrets.CDK_PULL_IMAGE_ROLE }} |
| 47 | + role-session-name: eps-vpc-resources-pull-image |
| 48 | + |
| 49 | + - name: build_artifact download |
| 50 | + uses: actions/download-artifact@v4 |
| 51 | + with: |
| 52 | + name: build_artifact |
| 53 | + |
| 54 | + - name: extract build_artifact |
| 55 | + run: | |
| 56 | + mkdir -p .build |
| 57 | + tar -xf artifact.tar -C .build |
| 58 | +
|
| 59 | + - name: Retrieve AWS Account ID |
| 60 | + id: retrieve-account-id |
| 61 | + run: echo "ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> "$GITHUB_ENV" |
| 62 | + |
| 63 | + - name: Login to Amazon ECR |
| 64 | + id: login-ecr |
| 65 | + run: | |
| 66 | + aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin ${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com |
| 67 | +
|
| 68 | + - name: Pull cdk-utils-build from Amazon ECR |
| 69 | + run: | |
| 70 | + docker pull "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest" |
| 71 | + docker tag "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest" cdk-utils-build-repo:latest |
| 72 | +
|
| 73 | + - name: Configure AWS Credentials |
| 74 | + id: connect-aws |
| 75 | + uses: aws-actions/configure-aws-credentials@v4 |
| 76 | + with: |
| 77 | + aws-region: eu-west-2 |
| 78 | + role-to-assume: ${{ secrets.CLOUD_FORMATION_DEPLOY_ROLE }} |
| 79 | + role-session-name: eps-vpc-resources-deployment |
| 80 | + output-credentials: true |
| 81 | + |
| 82 | + - name: fix cdk.json for deployment |
| 83 | + run: | |
| 84 | + jq \ |
| 85 | + --arg VERSION_NUMBER "${{ inputs.VERSION_NUMBER }}" \ |
| 86 | + --arg COMMIT_ID "${{ inputs.COMMIT_ID }}" \ |
| 87 | + --arg logRetentionInDays "${{ inputs.LOG_RETENTION_IN_DAYS }}" \ |
| 88 | + '.context += { |
| 89 | + "VERSION_NUMBER": $VERSION_NUMBER, |
| 90 | + "COMMIT_ID": $COMMIT_ID, |
| 91 | + "logRetentionInDays": $logRetentionInDays}' \ |
| 92 | + .build/cdk.json > .build/cdk.new.json |
| 93 | + mv .build/cdk.new.json .build/cdk.json |
| 94 | +
|
| 95 | + - name: Show diff |
| 96 | + run: | |
| 97 | + docker run \ |
| 98 | + -v "$(pwd)/.build":/home/cdkuser/workspace/ \ |
| 99 | + -e AWS_ACCESS_KEY_ID=${{ steps.connect-aws.outputs.aws-access-key-id }} \ |
| 100 | + -e AWS_SECRET_ACCESS_KEY=${{ steps.connect-aws.outputs.aws-secret-access-key }} \ |
| 101 | + -e AWS_SESSION_TOKEN=${{ steps.connect-aws.outputs.aws-session-token }} \ |
| 102 | + -e AWS_REGION="eu-west-2" \ |
| 103 | + -e SHOW_DIFF="true" \ |
| 104 | + -e DEPLOY_CODE="false" \ |
| 105 | + -e CDK_APP_PATH="packages/cdk/bin/VpcResourcesApp.ts" \ |
| 106 | + cdk-utils-build-repo:latest |
| 107 | + shell: bash |
| 108 | + |
| 109 | + - name: Deploy code |
| 110 | + run: | |
| 111 | + docker run \ |
| 112 | + -v "$(pwd)/.build":/home/cdkuser/workspace/ \ |
| 113 | + -e AWS_ACCESS_KEY_ID=${{ steps.connect-aws.outputs.aws-access-key-id }} \ |
| 114 | + -e AWS_SECRET_ACCESS_KEY=${{ steps.connect-aws.outputs.aws-secret-access-key }} \ |
| 115 | + -e AWS_SESSION_TOKEN=${{ steps.connect-aws.outputs.aws-session-token }} \ |
| 116 | + -e AWS_REGION="eu-west-2" \ |
| 117 | + -e SHOW_DIFF="false" \ |
| 118 | + -e DEPLOY_CODE="true" \ |
| 119 | + -e CDK_APP_PATH="packages/cdk/bin/VpcResourcesApp.ts" \ |
| 120 | + cdk-utils-build-repo:latest |
| 121 | + shell: bash |
| 122 | + |
| 123 | + update_github_pages: |
| 124 | + runs-on: ubuntu-latest |
| 125 | + environment: ${{ inputs.TARGET_ENVIRONMENT }} |
| 126 | + if: ${{ always() && !failure() && !cancelled() }} |
| 127 | + needs: [release_code] |
| 128 | + permissions: |
| 129 | + id-token: write |
| 130 | + contents: write |
| 131 | + steps: |
| 132 | + - name: Checkout gh-pages |
| 133 | + uses: actions/checkout@v4 |
| 134 | + with: |
| 135 | + ref: gh-pages |
| 136 | + path: gh-pages |
| 137 | + |
| 138 | + - name: update release tag in github pages |
| 139 | + run: | |
| 140 | + cd gh-pages |
| 141 | + NOW=$(date +'%Y-%m-%dT%H:%M:%S') |
| 142 | + echo "tag,release_datetime" > _data/${{ inputs.TARGET_ENVIRONMENT }}_latest.csv |
| 143 | + echo "${{ inputs.VERSION_NUMBER }},${NOW}" >> _data/${{ inputs.TARGET_ENVIRONMENT }}_latest.csv |
| 144 | + echo "${{ inputs.VERSION_NUMBER }},${NOW}" >> _data/${{ inputs.TARGET_ENVIRONMENT }}_deployments.csv |
| 145 | + git config user.name github-actions |
| 146 | + git config user.email [email protected] |
| 147 | + git add _data/${{ inputs.TARGET_ENVIRONMENT }}_latest.csv |
| 148 | + git add _data/${{ inputs.TARGET_ENVIRONMENT }}_deployments.csv |
| 149 | + git commit -m 'update releases for ${{ inputs.TARGET_ENVIRONMENT }}' |
| 150 | + parallel --retries 10 --delay 3 ::: "git pull --rebase && git push" |
0 commit comments