|
| 1 | +# Builds a docker image, then tags it with the github sha and pushes it to our Amazon ECR registry |
| 2 | + |
| 3 | +inputs: |
| 4 | + module-name: |
| 5 | + description: "Name of the module to build. Used as the default image name and src dir unless 'image-name' or 'src-path' are used." |
| 6 | + required: true |
| 7 | + build-for-environment: |
| 8 | + description: "The backend environment we are building for (API calls are pointed to). This should be one of (development, staging, production)." |
| 9 | + required: true |
| 10 | + extra-build-args: |
| 11 | + description: "Extra args passed to 'docker build'." |
| 12 | + required: false |
| 13 | + src-path: |
| 14 | + description: "What folder to be (generally to find the Dockerfile in) default is root of repo" |
| 15 | + required: false |
| 16 | + image-ref: |
| 17 | + description: "The version number or sha used in creating image tag" |
| 18 | + required: false |
| 19 | + |
| 20 | + |
| 21 | +runs: |
| 22 | + using: 'composite' |
| 23 | + steps: |
| 24 | + - uses: FranzDiebold/github-env-vars-action@v2 |
| 25 | + # https://github.com/aws-actions/amazon-ecr-login |
| 26 | + - name: Set up Docker Buildx |
| 27 | + uses: docker/setup-buildx-action@v3 |
| 28 | + - name: Login to Amazon ECR |
| 29 | + id: login-ecr |
| 30 | + uses: aws-actions/amazon-ecr-login@v2 |
| 31 | + - shell: bash |
| 32 | + run: | |
| 33 | + # Env var parsing |
| 34 | +
|
| 35 | + INPUT_SRC_PATH=${{ inputs.src-path }} |
| 36 | + SRC_PATH=${INPUT_SRC_PATH:-"./"} |
| 37 | + INPUT_IMAGE_REF=${{ inputs.image-ref }} |
| 38 | + IMAGE_REF=${INPUT_IMAGE_REF:-$CI_SHA} |
| 39 | + IMAGE_NAME=${{ inputs.module-name }} |
| 40 | + REPO_IMAGE=${{ steps.login-ecr.outputs.registry }}/$IMAGE_NAME |
| 41 | + DOCKER_BUILDKIT=1 |
| 42 | + ENVIRONMENT=${{ inputs.build-for-environment }} |
| 43 | + BRANCH_NAME=${{env.ENV_NAME}} |
| 44 | + IMAGE_TAG=$ENVIRONMENT-$IMAGE_REF |
| 45 | +
|
| 46 | + # Create repo if needed |
| 47 | + aws ecr create-repository --repository-name $IMAGE_NAME && \ |
| 48 | + aws ecr set-repository-policy --repository-name $IMAGE_NAME --policy-text "$(cat ${{ github.action_path }}/shared-ecr-policy.json)" || \ |
| 49 | + true # Just let this fail if the repo already exists |
| 50 | +
|
| 51 | + docker buildx create --name=remote-buildkit-agent --driver=remote --use tcp://remote-buildkit-agent.infrastructure.svc.cluster.local:80 || true # Create the builder (might already exist) |
| 52 | +
|
| 53 | + cd $SRC_PATH |
| 54 | + BUILD_ARGS="--build-arg BUILD_FOR_ENVIRONMENT=$ENVIRONMENT --build-arg IMAGE_TAG=$IMAGE_TAG" |
| 55 | +
|
| 56 | + # Finally, build our runner container |
| 57 | + docker buildx build ${{ inputs.extra-build-args }} $BUILD_ARGS -t $REPO_IMAGE:$IMAGE_TAG -t $REPO_IMAGE:latest --push . |
0 commit comments