Build Docker images #1562
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Docker images | |
on: | |
push: | |
branches: [main] | |
schedule: | |
- cron: '42 7 * * *' # run at 7:42 UTC (morning) every day | |
permissions: | |
contents: read | |
env: | |
RUST_IMAGE_TAG: latest | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | |
DOCKER_REPO: ${{ secrets.DOCKERHUB_USERNAME }}/cargo-chef | |
jobs: | |
rust_image_tag_matrix: | |
name: Generate Rust Docker image tag matrix | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- | |
id: set-matrix | |
run: | | |
(echo -n 'matrix=[' \ | |
&& curl --silent https://raw.githubusercontent.com/docker-library/official-images/master/library/rust \ | |
| grep -E Tags: \ | |
| cut -d ' ' -f 2- \ | |
| sed 's/, /\n/g' \ | |
| sed 's/\(.*\)/"\1",/g' \ | |
| tr '\n' ' ' \ | |
| sed '$ s/..$//' \ | |
&& echo ']') >> "$GITHUB_OUTPUT" | |
build_and_push: | |
name: Build and push | |
needs: [rust_image_tag_matrix] | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
rust_image_tag: ${{fromJSON(needs.rust_image_tag_matrix.outputs.matrix)}} | |
steps: | |
- | |
name: Checkout | |
uses: actions/checkout@v4 | |
- | |
name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- | |
name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- | |
name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- | |
# Get package version from git tags | |
name: Get package version | |
id: package_version | |
run: |- | |
git fetch --tags | |
VER=$(git tag --sort="-v:refname" | head -n 1 | cut -d"v" -f2) | |
echo "result=$VER" >> "$GITHUB_OUTPUT" | |
- | |
# Check if version matches ^\d+\.\d+\.\d+$ | |
name: Determine if release version | |
id: is_release_version | |
run: | | |
if [[ ${{ steps.package_version.outputs.result }} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "result=true" >> "$GITHUB_OUTPUT" | |
fi | |
- | |
name: Determine if duplicated | |
id: is_duplicated | |
run: | | |
CHEF_IMAGE=$DOCKER_REPO:$CHEF_IMAGE_TAG | |
CHEF_IMAGE_TAG=$CHEF_PACKAGE_VERSION-rust-$RUST_IMAGE_TAG | |
if DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $CHEF_IMAGE >/dev/null; then | |
echo "There is already a pushed image with ${CHEF_IMAGE_TAG} as tag. Skipping." | |
echo "result=true" >> "$GITHUB_OUTPUT" | |
else | |
echo "result=false" >> "$GITHUB_OUTPUT" | |
fi | |
- | |
name: Build and push without `latest` tag | |
if: ${{ steps.is_release_version.outputs.result == 'true' && steps.is_duplicated.outputs.result == 'false' && matrix.rust_image_tag != 'latest' }} | |
run: | | |
RUST_IMAGE_TAG=${{ matrix.rust_image_tag }} | |
CHEF_PACKAGE_VERSION=${{ steps.package_version.outputs.result }} | |
CHEF_IMAGE=$DOCKER_REPO:$CHEF_PACKAGE_VERSION-rust-$RUST_IMAGE_TAG | |
CHEF_IMAGE_LATEST=$DOCKER_REPO:latest-rust-$RUST_IMAGE_TAG | |
docker buildx build \ | |
--tag $CHEF_IMAGE \ | |
--tag $CHEF_IMAGE_LATEST \ | |
--build-arg=BASE_IMAGE=rust:$RUST_IMAGE_TAG \ | |
--build-arg=CHEF_TAG=$CHEF_PACKAGE_VERSION \ | |
--platform linux/amd64,linux/arm64 \ | |
--push \ | |
./docker | |
- | |
# Latest Rust version, latest cargo-chef version | |
name: Build and push with `latest` tag | |
if: ${{ steps.is_release_version.outputs.result == 'true' && steps.is_duplicated.outputs.result == 'false' && matrix.rust_image_tag == 'latest' }} | |
run: | | |
RUST_IMAGE_TAG=${{ matrix.rust_image_tag }} | |
CHEF_PACKAGE_VERSION=${{ steps.package_version.outputs.result }} | |
CHEF_IMAGE=$DOCKER_REPO:$CHEF_PACKAGE_VERSION-rust-$RUST_IMAGE_TAG | |
CHEF_IMAGE_LATEST=$DOCKER_REPO:latest-rust-$RUST_IMAGE_TAG | |
docker buildx build \ | |
--tag $CHEF_IMAGE \ | |
--tag $CHEF_IMAGE_LATEST \ | |
--tag $DOCKER_REPO:latest \ | |
--build-arg=BASE_IMAGE=rust:$RUST_IMAGE_TAG \ | |
--build-arg=CHEF_TAG=$CHEF_PACKAGE_VERSION \ | |
--platform linux/amd64,linux/arm64 \ | |
--push \ | |
./docker |