Skip to content

Commit

Permalink
issue #63: build and push docker container to github registry (#66)
Browse files Browse the repository at this point in the history
* issue #63: build and push docker container to github registry

* issue #63: fixed context for build-and-push-on-pr-merge

* issue #63: added EOF

* issue #63: Removed build-and-push-on-commit, not necessary for now

* issue #63: fixe workflow

* issue #63: created script for the second job

* issue #63: check if a dockerfile is present inside the repo

* issue #63: cancel the action if no dockerfile is found

* issue #63: added logging message

* issue #63: manage multiple branch

* issue #63: updated the doc and download the script

* issue #63: fixed issues and added the doc to the readme

* issue #63: removed on push_branches for this repository

* issue #63: changed the url for the good one (testing)

* issue #63: support merged branch and added .vscode

* issue #63: fix duplicated step name

* installing the script with python -m pip install instead of downloading the whole repo
  • Loading branch information
ThomasCardin authored Feb 6, 2024
1 parent 6e2c2f0 commit 3a739bc
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/workflow-build-push-container-github-registry.md
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

Check warning on line 1 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

1:1 [document-start] missing document start "---"

Check failure on line 1 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

1:81 [line-length] line too long (87 > 80 characters)

on:

Check warning on line 3 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

3:1 [truthy] truthy value should be one of [false, true]
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

Check failure on line 60 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

60:81 [line-length] line too long (90 > 80 characters)
id: extract_branch_name

- name: Build and push (3 tags | commit number, pr number and branch name) (unmerged)

Check failure on line 63 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

63:81 [line-length] line too long (91 > 80 characters)
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 }}

Check failure on line 71 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

71:81 [line-length] line too long (90 > 80 characters)
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }}

Check failure on line 72 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

72:81 [line-length] line too long (81 > 80 characters)
${{ inputs.registry }}/${{ inputs.container-name }}:${{ steps.extract_branch_name.outputs.branch }}

Check failure on line 73 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

73:81 [line-length] line too long (111 > 80 characters)
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 }}

Check failure on line 85 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

85:81 [line-length] line too long (81 > 80 characters)
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.pull_request.base.ref }}

Check failure on line 86 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

86:81 [line-length] line too long (105 > 80 characters)
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

Check failure on line 112 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

112:81 [line-length] line too long (99 > 80 characters)
run: python -m pip install --user git+https://$USER:$USER_TOKEN@github.com/ai-cfia/devops.git@26-as-a-devops-i-want-to-create-unit-tests-for-the-remove-previous-imagepy-script

Check failure on line 113 in .github/workflows/workflow-build-push-container-github-registry.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

113:81 [line-length] line too long (183 > 80 characters)
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

0 comments on commit 3a739bc

Please sign in to comment.