Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #63: build and push docker container to github registry #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Reusable workflow to build and push docker container to GitHub Container Registry (GCR)

- **Purpose:** Build and push a docker container to GitHub Container Registry (GCR).
- **Usage:** Call this workflow and provide the container-name, tag, and
registry as inputs. The registry should always start with
ghcr.io/<YOUR-REGISTRY-NAME> (e.g., ghcr.io/ai-cfia). If you create, push, or
merge a pull request, the workflow will be triggered and will start 2 jobs.
The first job will build and push the new image with 2 tags. The first tag is
the pull request number, and the second tag is the commit that triggered the
action. The second job will remove the previous image based on the previous
tag. If no Dockerfile is found
at the root of the repository, the workflow will be cancelled.
- **Required Secrets:**
- None
144 changes: 144 additions & 0 deletions .github/workflows/workflow-build-push-container-github-registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: Reusable workflow to build and push docker container to github container registry

on:
workflow_call:
inputs:
container-name:
required: true
type: string
tag:
required: true
type: string
registry:
required: true
type: string

jobs:
build-push-image:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v4

- name: Check if a Dockerfile is present at the root of the repo
id: check-file
run: |
if [ -f Dockerfile ]; then
echo "::set-output name=exists::true"
else
echo "::set-output name=exists::false"
fi

- name: Early exit if no Dockerfile is present
if: steps.check-file.outputs.exists == 'false'
run: |
gh run cancel ${{ github.run_id }}
gh run watch ${{ github.run_id }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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

- name: Log in to the github container registry (GCR)
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx

- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch_name

- name: Build and push (3 tags | commit number, pr number and branch name) (unmerged)
id: docker_build_and_tag_unmerged
if: github.event.pull_request.merged == false
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.number }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ steps.extract_branch_name.outputs.branch }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

- name: Build and push (2 tags | commit number and branch name) (merged)
id: docker_build_and_tag_merged
if: github.event.pull_request.merged == true
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.pull_request.base.ref }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

- name: Refresh Cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

- name: Image digest (latest)
if: github.event.pull_request.merged == false
run: echo ${{ steps.docker_build_and_tag_unmerged.outputs.digest }}

- name: Image digest (latest)
if: github.event.pull_request.merged == true
run: echo ${{ steps.docker_build_and_tag_merged.outputs.digest }}

remove-old-image:
runs-on: ubuntu-latest
needs: build-push-image
steps:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.8

- name: Install the remove-previous-image from github.com/ai-cfia/devops inside the user-site
run: python -m pip install --user git+https://$USER:[email protected]/ai-cfia/devops.git@26-as-a-devops-i-want-to-create-unit-tests-for-the-remove-previous-imagepy-script
env:
USER: ${{ secrets.USER }}
USER_TOKEN: ${{ secrets.USER_TOKEN }}

- name: Access user site-packages
run: |
USER_SITE=$(python -m site --user-site)
echo "Path to site-packages is $USER_SITE"
echo "USER_SITE=$USER_SITE" >> $GITHUB_ENV

- name: Delete the previous image (unmerged pull request)
if: github.event.pull_request.merged == false
run: python $USER_SITE/remove-previous-image/remove_previous_image.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REGISTRY: ${{ inputs.registry }}
CONTAINER_NAME: ${{ inputs.container-name }}
UNIQUE_TAG: ${{ github.event.number }}
USER: ${{ github.actor }}
CURRENT_COMMIT: ${{ inputs.tag }}

- name: Delete the previous image (merged pull request)
if: github.event.pull_request.merged == true
run: python $USER_SITE/remove-previous-image/remove_previous_image.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REGISTRY: ${{ inputs.registry }}
CONTAINER_NAME: ${{ inputs.container-name }}
UNIQUE_TAG: ${{ github.event.pull_request.base.ref }}
USER: ${{ github.actor }}
CURRENT_COMMIT: ${{ inputs.tag }}
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"stkb.rewrap",
"DavidAnson.vscode-markdownlint"
],
"unwantedRecommendations": [

]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.rulers": [80],
"files.trimTrailingWhitespace": true,
"files.trimFinalNewlines": true,
"files.insertFinalNewline": true
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Here are the available workflows and their associated documentation:
Pages](.github/workflows/workflow-gh-pages-deployment.md)
- [Reusable Vercel
Deployment](.github/workflows/workflow-vercel-deployment.md)
- [Reusable Build
and Push to GCR](.github/workflows/workflow-build-push-container-github-registry.md)

## Usage

Expand Down