-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
6e2c2f0
commit 3a739bc
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
.github/workflows/workflow-build-push-container-github-registry.md
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
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
144
.github/workflows/workflow-build-push-container-github-registry.yml
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
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 GitHub Actions / yaml-lint-check
|
||
|
||
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:$USER_TOKEN@github.com/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 }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"recommendations": [ | ||
"stkb.rewrap", | ||
"DavidAnson.vscode-markdownlint" | ||
], | ||
"unwantedRecommendations": [ | ||
|
||
] | ||
} |
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
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 | ||
} |
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