Skip to content

Commit

Permalink
Use GitHub Actions to Push to DockerHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhakar Kumar committed Jul 3, 2023
1 parent 1db3699 commit 31d0967
Show file tree
Hide file tree
Showing 25 changed files with 882 additions and 18 deletions.
124 changes: 124 additions & 0 deletions .github/workflows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Workflows in container-images

This repository uses workflows to build the Dockerfiles hosted in this repository and publish them to container registries.

## Overview

There are 2 kinds of YML files used here:
1. `build-and-publish-docker-image.yml`, which specifies a reusable workflow, which MUST be called from a workflow configuration file.
2. Other YML files in the `.github/workflows` directory call this reusable-workflow.

Each of these workflows:
a. Monitors a specific directory. For example, the file `matlab-deps-r2023a-ubuntu20.04.yml` monitors the directory `container-images/matlab-deps/r2023a/ubuntu20.04`
b. Is triggered when changes are made to the directory it monitors. This should build and publish to the configured registries.

## Triggers and Scheduled Jobs

All workflows are scheduled to run on Monday at 00:00.
Workflows are also triggered when you push any changes to the directories with Dockerfiles.
Workflows can be manually triggered from the "Actions" tab.

## Directory structure

The container-images repository has the following folder structure:

The root folder lists the hosted container images, each of which is called the `BASE_IMAGE`:

1. matlab-deps
2. polyspace-deps
3. matlab-runtime-deps (From R2023b)

These folders list various releases of MATLAB, each of which is called the `MATLAB_RELEASE`:

1. r2019b
2. r2020a ... and so on

Each folder may list one or more OS flavours, each of which is called the `OS`:
1. aws-batch
2. ubi8 / ubi9
3. ubuntu20.04 / ubuntu22.04

Each of these folders should have a `Dockerfile` and some accompanying files:

1. Dockerfile
2. base-dependencies.txt
3. *.sh `(scripts)`

## Images Pushed to DockerHub:

### Tags created
The `MATLAB_RELEASE` is used to create two tags.
One starting with lower case `r` and another with upper case `R`:
1. The letter `r` in lower case in `MATLAB_RELEASE`. Here after called: `r_MATLAB_RELEASE`
2. The letter `R` is upper case in `MATLAB_RELEASE`. Here after called: `R_MATLAB_RELEASE`

The images pushed for each Dockerfile will use the following naming schema:
` mathworks/${BASE_IMAGE}:${r_MATLAB_RELEASE}-${OS} `
` mathworks/${BASE_IMAGE}:${R_MATLAB_RELEASE}-${OS} `

### Latest Tag
Every `BASE_IMAGE` needs a `MATLAB_RELEASE` & `OS` flavor that marks its `latest` image.
Set the variable `should_add_latest_tag` to `true` in the workflow file to specify which image should carry the `latest` tag.
**Note: Remember to set the variable to `false` when marking a new image as the `latest` one. **

Setting the `should_add_latest_tag` field to `true` in a workflow will push images without the OS specified in the tag:
` mathworks/${BASE_IMAGE}:${r_MATLAB_RELEASE} `
` mathworks/${BASE_IMAGE}:${R_MATLAB_RELEASE} `

For example:
```yml
#matlab-deps-r2023a-ubuntu20.04.yml
...
with:
docker_build_context: './matlab-deps/r2023a/ubuntu20.04'
base_image_name: mathworks/matlab-deps
matlab_release_tag: 'r2023a'
os_info_tag: 'ubuntu20.04'
should_add_latest_tag: true
...
```
Will generate the following tags:
1. r2023a
1. R2023a
1. r2023a-ubuntu20.04
1. R2023a-ubuntu20.04
1. latest

## Workflow Description

Each workflow must set the following `inputs` to the `reusable-workflow`:
```YML
docker_build_context:
description: 'Relative path to folder with Dockerfile. Ex: ./matlab-deps/r2023a/ubuntu20.04 '
required: true
type: string
base_image_name:
description: 'Name of base image. Example: mathworks/matlab-deps'
required: true
type: string
matlab_release_tag:
description: 'Name of matlab release. Example: r2023a'
required: true
type: string
os_info_tag:
description: 'Allowed values: aws-batch, ubi8, ubi9, ubuntu20.04 ubuntu22.04'
required: true
type: string
should_add_latest_tag:
description: 'Specify if this image should also be tagged as latest'
required: true
type: boolean
```
Each `reusable-workflow` job consists of the following steps:

1. Check-out the repository into a GitHub Actions runner.
1. Setup Image Tags: Configures tags to have both Pascal & camel case tags (R2023a, r2023a)
1. Login to DockerHub Container Registry
1. Build the image & push
1. If the variable "should_add_latest_tag" is present that an additional "latest" tag is added to the image.

----
Copyright 2023 The MathWorks, Inc.
----
120 changes: 120 additions & 0 deletions .github/workflows/build-and-publish-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright 2023 The MathWorks, Inc.

name: Build and Publish the Container Image

# images build with this workflow are created with the following naming conventions:
# ${base_image_name}:${matlab_release}-${os}

# built images are pushed to DockerHub. Note: Both camel & Pascal case versions of tags are generated and published

# This workflow is only be triggered when called from another workflow.
on:
workflow_call:
inputs:
docker_build_context:
description: 'Relative path to folder with Dockerfile. Ex: ./matlab-deps/r2023a/ubuntu20.04 '
required: true
type: string
base_image_name:
description: 'Name of base image. Example: mathworks/matlab-deps'
required: true
type: string
matlab_release_tag:
description: 'Name of matlab release. Example: r2023a'
required: true
type: string
os_info_tag:
description: 'Allowed values: aws-batch, ubi8, ubi9, ubuntu20.04 ubuntu22.04'
required: true
type: string
is_default_os:
description: 'Specify whether the OS being tagged is desired default OS. For example, This field is used to configure that matlab-deps/r2023a should point to matlab-deps/r2023a-ubuntu20.04'
required: true
type: boolean
should_add_latest_tag:
description: 'Specify if this image should also be tagged as latest'
required: true
type: boolean

jobs:
build-push-image:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name : Setup Image Tags
id: setup_image_tags
run: |
RAW_MATLAB_RELEASE=${{ inputs.matlab_release_tag }} \
&& LOWER_CASE_MATLAB_RELEASE=${RAW_MATLAB_RELEASE,,} \
&& echo "TAG_RELEASE_ONLY_CAMEL_CASE=${LOWER_CASE_MATLAB_RELEASE}" >> "$GITHUB_OUTPUT" \
&& echo "TAG_RELEASE_ONLY_PASCAL_CASE=${LOWER_CASE_MATLAB_RELEASE^}" >> "$GITHUB_OUTPUT" \
&& echo "TAG_RELEASE_AND_OS_CAMEL_CASE=${LOWER_CASE_MATLAB_RELEASE}-${{ inputs.os_info_tag }}" >> "$GITHUB_OUTPUT" \
&& echo "TAG_RELEASE_AND_OS_PASCAL_CASE=${LOWER_CASE_MATLAB_RELEASE^}-${{ inputs.os_info_tag }}" >> "$GITHUB_OUTPUT"
# See here for example: https://docs.docker.com/build/ci/github-actions/push-multi-registries/
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# - name: Login to GitHub Container Registry
# uses: docker/login-action@v2
# with:
# registry: ghcr.io
# username: ${{ github.repository_owner }}
# password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04
- name: Build & Push Image
if: ${{ inputs.should_add_latest_tag == false && inputs.is_default_os == false }}
uses: docker/build-push-action@v4
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
push: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04, r2023a, R2023a
- name: Build & Push Image for latest OS
if: ${{ inputs.should_add_latest_tag == false && inputs.is_default_os == true }}
uses: docker/build-push-action@v4
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
push: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_PASCAL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
# Example tags: r2023a-ubuntu20.04, R2023a-ubuntu20.04, r2023a, R2023a, latest
- name: Build & Push Image with latest Tag for latest OS
if: ${{ inputs.should_add_latest_tag == true && inputs.is_default_os == true }}
uses: docker/build-push-action@v4
with:
context: ${{ inputs.docker_build_context }}
platforms: linux/amd64
push: true
tags: |
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_ONLY_PASCAL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_CAMEL_CASE }}
${{ inputs.base_image_name }}:${{ steps.setup_image_tags.outputs.TAG_RELEASE_AND_OS_PASCAL_CASE }}
${{ inputs.base_image_name }}:latest
# Invalid combination would error out.
- name: Invalid combination of should_add_latest_tag set to true and is_default_os set to false
if: ${{ inputs.should_add_latest_tag == true && inputs.is_default_os == false }}
run: |
echo "Invalid situation detected. A workflow marked as latest must also set the default os to be true. "
exit 1
29 changes: 29 additions & 0 deletions .github/workflows/matlab-deps-r2019b-aws-batch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build & Publish matlab-deps/r2019b/aws-batch
name: matlab-deps-r2019b-aws-batch

# Define when builds will occur:
on:
# Run workflow when there is a push to the 'main' branch & push includes changes to any files in described path
push:
branches:
- 'main'
paths:
- 'matlab-deps/r2019b/aws-batch/**'

# Run at 00:00 on every Monday (1st Day of the Week) (See: crontab.guru)
schedule:
- cron: '0 0 * * 1'

workflow_dispatch:

jobs:
build-and-publish-docker-image:
uses: ./.github/workflows/build-and-publish-docker-image.yml
secrets: inherit
with:
docker_build_context: './matlab-deps/r2019b/aws-batch'
base_image_name: mathworks/matlab-deps
matlab_release_tag: 'r2019b'
os_info_tag: 'aws-batch'
is_default_os: false
should_add_latest_tag: false
29 changes: 29 additions & 0 deletions .github/workflows/matlab-deps-r2019b-ubuntu18.04.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build & Publish matlab-deps/r2019b/ubuntu18.04
name: matlab-deps-r2019b-ubuntu18.04

# Define when builds will occur:
on:
# Run workflow when there is a push to the 'main' branch & push includes changes to any files in described path
push:
branches:
- 'main'
paths:
- 'matlab-deps/r2019b/ubuntu18.04/**'

# Run at 00:00 on every Monday (1st Day of the Week) (See: crontab.guru)
schedule:
- cron: '0 0 * * 1'

workflow_dispatch:

jobs:
build-and-publish-docker-image:
uses: ./.github/workflows/build-and-publish-docker-image.yml
secrets: inherit
with:
docker_build_context: './matlab-deps/r2019b/ubuntu18.04'
base_image_name: mathworks/matlab-deps
matlab_release_tag: 'r2019b'
os_info_tag: 'ubuntu18.04'
is_default_os: true
should_add_latest_tag: false
29 changes: 29 additions & 0 deletions .github/workflows/matlab-deps-r2020a-aws-batch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build & Publish matlab-deps/r2020a/aws-batch
name: matlab-deps-r2020a-aws-batch

# Define when builds will occur:
on:
# Run workflow when there is a push to the 'main' branch & push includes changes to any files in described path
push:
branches:
- 'main'
paths:
- 'matlab-deps/r2020a/aws-batch/**'

# Run at 00:00 on every Monday (1st Day of the Week) (See: crontab.guru)
schedule:
- cron: '0 0 * * 1'

workflow_dispatch:

jobs:
build-and-publish-docker-image:
uses: ./.github/workflows/build-and-publish-docker-image.yml
secrets: inherit
with:
docker_build_context: './matlab-deps/r2020a/aws-batch'
base_image_name: mathworks/matlab-deps
matlab_release_tag: 'r2020a'
os_info_tag: 'aws-batch'
is_default_os: false
should_add_latest_tag: false
29 changes: 29 additions & 0 deletions .github/workflows/matlab-deps-r2020a-ubuntu18.04.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build & Publish matlab-deps/r2020a/ubuntu18.04
name: matlab-deps-r2020a-ubuntu18.04

# Define when builds will occur:
on:
# Run workflow when there is a push to the 'main' branch & push includes changes to any files in described path
push:
branches:
- 'main'
paths:
- 'matlab-deps/r2020a/ubuntu18.04/**'

# Run at 00:00 on every Monday (1st Day of the Week) (See: crontab.guru)
schedule:
- cron: '0 0 * * 1'

workflow_dispatch:

jobs:
build-and-publish-docker-image:
uses: ./.github/workflows/build-and-publish-docker-image.yml
secrets: inherit
with:
docker_build_context: './matlab-deps/r2020a/ubuntu18.04'
base_image_name: mathworks/matlab-deps
matlab_release_tag: 'r2020a'
os_info_tag: 'ubuntu18.04'
is_default_os: true
should_add_latest_tag: false
Loading

0 comments on commit 31d0967

Please sign in to comment.