Composite Actions | Build Docker image
This Composite Action does the following:
- Builds a Docker image with a custom command (including all the target tags)
- (Optional) Tests the image with container-structure-test
- (Optional) Pushes the image and all its tag to an ECR registry
- For tests: a valid container-structure-test config file (binary installed in the action)
- For ECR push: AWS credentials with permission to push to the ECR registry
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Gutenberg-Technology/build-docker-image-action@v1.0.0
with:
build-cmd: "docker build -t my-image:latest -t my-image:${{ github.sha }} ."
test-cmd: "container-structure-test test --image my-image --config test-config.yaml"
Basic example: build, test and push the my_image:latest
tag to an ECR registry.
jobs:
build-test-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Gutenberg-Technology/build-docker-image-action@v1.0.0
with:
build-cmd: "docker build --no-cache -t my_image:latest ."
test-cmd: "container-structure-test test --image my_image:latest --config container_structure_test.yaml"
push-to-ecr: true
ecr-repo: "my_image"
ecr-tags-to-push: "latest"
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Advanced usage: Set env
vars, use several tags, including the commit SHA, and push it to the ECR registry.
jobs:
build-test-push:
name: Build, test & push image
runs-on: ubuntu-latest
env:
IMAGE_REGISTRY: "my_image"
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.git-ref }}
- name: Set short git commit SHA
id: vars
run: |
shortSha=$(git rev-parse --short ${{ github.sha }})
echo "COMMIT_SHORT_SHA=$shortSha" >> $GITHUB_ENV
- name: Confirm git commit SHA output
run: echo ${{ env.COMMIT_SHORT_SHA }}
- uses: Gutenberg-Technology/build-docker-image-action@v1.0.0
build-cmd: "docker build --no-cache -t ${{ env.IMAGE_REGISTRY }}:latest -t ${{ env.IMAGE_REGISTRY }}:${{ env.COMMIT_SHORT_SHA }} ."
test-cmd: "container-structure-test test --image ${{ env.IMAGE_REGISTRY }}:${{ env.COMMIT_SHORT_SHA }} --config container_structure_test.yaml"
push-to-ecr: true
ecr-repo: "${{ env.IMAGE_REGISTRY }}"
ecr-tags-to-push: "latest,${{ env.COMMIT_SHORT_SHA }}"
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}