|
| 1 | +# yamllint disable rule:line-length |
| 2 | +--- |
| 3 | +name: "ecr-container-build-push" |
| 4 | +description: "Build a container image and upload it to ECR" |
| 5 | +inputs: |
| 6 | + aws-creds-vault-path: |
| 7 | + default: secret/data/v2/data-special/infra1-user-ecr-rw |
| 8 | + description: "Vault path to AWS credentials used for helm push" |
| 9 | + aws-creds-vault-role: |
| 10 | + default: ecr-push |
| 11 | + description: "Vault auth role for reading AWS credentials" |
| 12 | + aws-region: |
| 13 | + default: "us-east-1" |
| 14 | + description: "AWS region to use for ECR" |
| 15 | + ecr-repos: |
| 16 | + description: "Repository (as defined in gooddata/terraform-ecr/repositories)" |
| 17 | + required: true |
| 18 | + ecr-url: |
| 19 | + description: "ECR registry default URL (without prefix/suffix)" |
| 20 | + required: true |
| 21 | + vault-url: |
| 22 | + description: "Vault API URL (default okay in almost all cases)" |
| 23 | + required: true |
| 24 | + build-args: |
| 25 | + description: "Arguments for container build file (ARG in Dockerfile)." |
| 26 | + required: false |
| 27 | + default: "" |
| 28 | + build-context: |
| 29 | + description: "Context (working directory) where the build should be executed" |
| 30 | + default: "." |
| 31 | + build-tags: |
| 32 | + description: "Tags (newline delimited)" |
| 33 | + required: true |
| 34 | + container-file: |
| 35 | + description: "File (with a path) to use for build" |
| 36 | + default: "Dockerfile" |
| 37 | + push-image: |
| 38 | + description: "Whether to really push to registry" |
| 39 | + default: "true" |
| 40 | + debug: |
| 41 | + description: "Turn on debug messages" |
| 42 | + default: "false" |
| 43 | + platforms: |
| 44 | + description: "List of target platforms for build" |
| 45 | + default: "linux/amd64" |
| 46 | + labels: |
| 47 | + description: "List of labels for image" |
| 48 | + default: "" |
| 49 | + secrets: |
| 50 | + description: "List of secrets to expose to the build (e.g., key=string, GIT_AUTH_TOKEN=mytoken)" |
| 51 | + default: "" |
| 52 | + secret-envs: |
| 53 | + description: "List of secret env vars to expose to the build (e.g., key=envname, MY_SECRET=MY_ENV_VAR)" |
| 54 | + default: "" |
| 55 | + provenance: |
| 56 | + description: "Generate provenance attestation for the build" |
| 57 | + default: "true" |
| 58 | +outputs: |
| 59 | + digest: |
| 60 | + description: "Image digest" |
| 61 | + value: ${{ steps.build_push.outputs.digest }} |
| 62 | + imageid: |
| 63 | + description: "Image ID" |
| 64 | + value: ${{ steps.build_push.outputs.imageid }} |
| 65 | + metadata: |
| 66 | + description: "Image metadata" |
| 67 | + value: ${{ steps.build_push.outputs.metadata }} |
| 68 | +runs: |
| 69 | + using: "composite" |
| 70 | + steps: |
| 71 | + - name: Check container file |
| 72 | + env: |
| 73 | + CONTAINERFILE: ${{ inputs.container-file }} |
| 74 | + shell: bash |
| 75 | + run: | |
| 76 | + test -f $CONTAINERFILE |
| 77 | + - name: Get required Vault secrets |
| 78 | + id: secrets |
| 79 | + uses: hashicorp/vault-action@v3 |
| 80 | + with: |
| 81 | + url: ${{ inputs.vault-url }} |
| 82 | + method: jwt |
| 83 | + path: jwt/github |
| 84 | + role: ${{ inputs.aws-creds-vault-role }} |
| 85 | + secrets: | |
| 86 | + ${{ inputs.aws-creds-vault-path }} aws_ecr_access_key | AWS_ACCESS_KEY ; |
| 87 | + ${{ inputs.aws-creds-vault-path }} aws_ecr_secret_key | AWS_SECRET_KEY ; |
| 88 | + - name: Configure AWS credentials |
| 89 | + uses: aws-actions/configure-aws-credentials@v4 |
| 90 | + with: |
| 91 | + aws-access-key-id: ${{ env.AWS_ACCESS_KEY }} |
| 92 | + aws-secret-access-key: ${{ env.AWS_SECRET_KEY }} |
| 93 | + aws-region: ${{ inputs.aws-region }} |
| 94 | + - name: Expand tags with ECR url and ECR repo |
| 95 | + id: expand_tags |
| 96 | + env: |
| 97 | + ECR_URL: ${{ inputs.ecr-url }} |
| 98 | + ECR_REPOS: ${{ inputs.ecr-repos }} |
| 99 | + BUILD_TAGS: ${{ inputs.build-tags }} |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + eval REPO=$ECR_REPOS |
| 103 | + { |
| 104 | + echo "EXPANDED_TAGS<<EOF" |
| 105 | + for BTAG in $BUILD_TAGS; do |
| 106 | + echo $ECR_URL/$REPO:$BTAG |
| 107 | + done |
| 108 | + echo EOF |
| 109 | + } >> "$GITHUB_ENV" |
| 110 | + echo "REPO=$REPO" >> "$GITHUB_ENV" |
| 111 | +
|
| 112 | + - name: Build and push Docker images (legacy) |
| 113 | + shell: bash |
| 114 | + env: |
| 115 | + CONTAINERFILE: ${{ inputs.container-file }} |
| 116 | + BUILD_CONTEXT: ${{ inputs.build-context }} |
| 117 | + BUILD_ARGS: ${{ inputs.build-args }} |
| 118 | + ECR_URL: ${{ inputs.ecr-url }} |
| 119 | + AWS_REGION: ${{ inputs.aws-region }} |
| 120 | + run: | |
| 121 | + # Login to ECR |
| 122 | + aws ecr get-login-password --region $AWS_REGION | \ |
| 123 | + docker login --username AWS --password-stdin $ECR_URL |
| 124 | +
|
| 125 | + # Parse build args |
| 126 | + DOCKER_BUILD_ARGS="" |
| 127 | + if [ -n "$BUILD_ARGS" ]; then |
| 128 | + while IFS= read -r arg; do |
| 129 | + [ -n "$arg" ] && DOCKER_BUILD_ARGS="$DOCKER_BUILD_ARGS --build-arg $arg" |
| 130 | + done <<< "$BUILD_ARGS" |
| 131 | + fi |
| 132 | +
|
| 133 | + # Build and push each tag |
| 134 | + for TAG in $EXPANDED_TAGS; do |
| 135 | + docker build -f $CONTAINERFILE \ |
| 136 | + $DOCKER_BUILD_ARGS \ |
| 137 | + -t $TAG \ |
| 138 | + $BUILD_CONTEXT |
| 139 | + docker push $TAG |
| 140 | + done |
0 commit comments