|
| 1 | +name: Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + ref: |
| 7 | + description: "Version to deploy" |
| 8 | + required: true |
| 9 | + openssl_lambda_tag: |
| 10 | + description: "OpenSSL Lambda container tag to deploy" |
| 11 | + required: true |
| 12 | + |
| 13 | +permissions: |
| 14 | + id-token: write |
| 15 | + packages: write |
| 16 | + |
| 17 | +env: |
| 18 | + STACK_NAME: ${{ vars.STACK_NAME }} |
| 19 | + AWS_REGION: ${{ vars.AWS_REGION }} |
| 20 | + FORCE_COLOR: 3 |
| 21 | + JSII_SILENCE_WARNING_UNTESTED_NODE_VERSION: 1 |
| 22 | + REGISTRY: ghcr.io |
| 23 | + |
| 24 | +jobs: |
| 25 | + print-inputs: |
| 26 | + name: Print inputs |
| 27 | + runs-on: ubuntu-22.04 |
| 28 | + steps: |
| 29 | + - name: Print inputs |
| 30 | + run: | |
| 31 | + echo ref=${{ github.event.inputs.ref }} |
| 32 | + echo openssl_lambda_tag=${{ github.event.inputs.openssl_lambda_tag }} |
| 33 | +
|
| 34 | + docker: |
| 35 | + name: Push Docker images to ECR |
| 36 | + |
| 37 | + runs-on: ubuntu-22.04 |
| 38 | + |
| 39 | + environment: production |
| 40 | + |
| 41 | + strategy: |
| 42 | + matrix: |
| 43 | + image: |
| 44 | + - openssl-lambda |
| 45 | + include: |
| 46 | + - image: openssl-lambda |
| 47 | + tag: ${{ github.event.inputs.openssl_lambda_tag }} |
| 48 | + |
| 49 | + steps: |
| 50 | + - name: Log in to the repo's container registry |
| 51 | + uses: docker/login-action@5f4866a30a54f16a52d2ecb4a3898e9e424939cf |
| 52 | + with: |
| 53 | + registry: ${{ env.REGISTRY }} |
| 54 | + username: ${{ github.actor }} |
| 55 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + |
| 57 | + - name: Pull Docker image from repository registry |
| 58 | + run: | |
| 59 | + docker pull ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.image }}:${{ matrix.tag }} |
| 60 | +
|
| 61 | + - name: Configure AWS credentials |
| 62 | + uses: aws-actions/configure-aws-credentials@v4 |
| 63 | + with: |
| 64 | + role-to-assume: ${{ secrets.AWS_ROLE }} |
| 65 | + role-session-name: github-action-hello-nrfcloud-backend |
| 66 | + aws-region: ${{ vars.AWS_REGION }} |
| 67 | + |
| 68 | + - name: Get credentials for ECR |
| 69 | + id: token |
| 70 | + run: | |
| 71 | + CREDS=$(aws ecr get-authorization-token | jq -r '.authorizationData[0].authorizationToken') |
| 72 | + PARTS=($(echo $CREDS | tr ':' '\n')) |
| 73 | + TOKEN=${PARTS[1]} |
| 74 | + echo "token=$TOKEN" >> $GITHUB_OUTPUT |
| 75 | + echo "::add-mask::$TOKEN" |
| 76 | +
|
| 77 | + - name: Get repository on ECR |
| 78 | + id: repositoryUri |
| 79 | + run: | |
| 80 | + REPO_URI=$(aws ecr describe-repositories --repository-names ${{ env.STACK_NAME }}-${{ matrix.image }} | jq -r '.repositories[0].repositoryUri') |
| 81 | + echo "repositoryUri=$REPO_URI" >> $GITHUB_OUTPUT |
| 82 | +
|
| 83 | + - name: Log in to the repo's container registry |
| 84 | + uses: docker/login-action@5f4866a30a54f16a52d2ecb4a3898e9e424939cf |
| 85 | + with: |
| 86 | + registry: ${{ steps.repositoryUri.outputs.repositoryUri }} |
| 87 | + username: AWS |
| 88 | + password: ${{ steps.token.outputs.token }} |
| 89 | + |
| 90 | + - name: Tag Docker image for ECR |
| 91 | + run: | |
| 92 | + docker tag ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.image }}:${{ matrix.tag }} ${{ steps.repositoryUri.outputs.repositoryUri }}:${{ matrix.tag }} |
| 93 | +
|
| 94 | + - name: Check if Docker image exists on ECR |
| 95 | + id: check-docker-image |
| 96 | + continue-on-error: true |
| 97 | + run: | |
| 98 | + docker manifest inspect ${{ steps.repositoryUri.outputs.repositoryUri }}:${{ matrix.tag }} |
| 99 | +
|
| 100 | + - name: Push Docker image to ECR |
| 101 | + if: steps.check-docker-image.outcome == 'failure' |
| 102 | + run: | |
| 103 | + docker push ${{ steps.repositoryUri.outputs.repositoryUri }}:${{ matrix.tag }} |
| 104 | +
|
| 105 | + deploy: |
| 106 | + runs-on: ubuntu-22.04 |
| 107 | + |
| 108 | + environment: production |
| 109 | + |
| 110 | + needs: docker |
| 111 | + |
| 112 | + env: |
| 113 | + FORCE_COLOR: 3 |
| 114 | + JSII_SILENCE_WARNING_UNTESTED_NODE_VERSION: 1 |
| 115 | + OPENSSL_LAMBDA_CONTAINER_TAG: |
| 116 | + ${{ github.event.inputs.openssl_lambda_tag }} |
| 117 | + |
| 118 | + steps: |
| 119 | + - uses: actions/checkout@v4 |
| 120 | + with: |
| 121 | + ref: ${{ github.event.inputs.ref }} |
| 122 | + |
| 123 | + - name: Determine released version |
| 124 | + id: version |
| 125 | + run: | |
| 126 | + git fetch --tags |
| 127 | + VERSION=`git describe --abbrev=0 --tags --always | tr -d '\n'` |
| 128 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 129 | +
|
| 130 | + - uses: actions/setup-node@v4 |
| 131 | + with: |
| 132 | + node-version: "20.x" |
| 133 | + cache: "npm" |
| 134 | + |
| 135 | + - name: Install dependencies |
| 136 | + run: npm ci --no-audit |
| 137 | + |
| 138 | + - name: Configure AWS credentials |
| 139 | + uses: aws-actions/configure-aws-credentials@v4 |
| 140 | + with: |
| 141 | + role-to-assume: ${{ secrets.AWS_ROLE }} |
| 142 | + role-session-name: github-action-hello-nrfcloud-backend |
| 143 | + aws-region: ${{ vars.AWS_REGION }} |
| 144 | + |
| 145 | + - run: npx cdk diff |
| 146 | + |
| 147 | + - name: Deploy solution stack |
| 148 | + run: npx cdk deploy --all --require-approval never |
0 commit comments