-
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.
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,117 @@ | ||
name: Docker CI/CD | ||
# This workflow is designed to build a Docker image, upload it to Docker Hub, | ||
# and create a GitHub release with the image tar file every time there's a commit | ||
# on branches other than 'main'. | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
ref: | ||
description: "Branch to pull from" | ||
required: false | ||
|
||
pull_request: | ||
types: closed | ||
branches: | ||
- "!main" | ||
- "releases/**" | ||
paths-ignore: [ "./github" ] | ||
|
||
env: | ||
REGISTRY: docker.io | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
jobs: | ||
build-and-push: | ||
if: github.event.pull_request.merged == true | ||
outputs: | ||
image_tag: ${{ steps.set_image_tag.outputs.IMAGE_TAG }} | ||
ref: ${{ steps.set_ref.outputs.ref }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Determine ref | ||
id: set_ref | ||
run: | | ||
if [[ ${{ github.event.inputs.ref }} ]]; then | ||
echo "ref=${{ github.event.inputs.ref }}" >> $GITHUB_OUTPUT | ||
else | ||
echo "ref=${{ github.ref_name }}" >> $GITHUB_OUTPUT | ||
fi | ||
shell: bash | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ steps.set_ref.outputs.ref }} | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ secrets.DOCKER_REGISTRY_USER }} | ||
password: ${{ secrets.DOCKER_REGISTRY_PASSWD }} | ||
|
||
- name: Determine IMAGE_TAG | ||
id: set_image_tag | ||
run: | | ||
if [[ ${{ github.event.inputs.tag }} ]]; then | ||
echo "IMAGE_TAG=${{ github.event.inputs.ref }}_RC-${sha}" >> $GITHUB_OUTPUT | ||
else | ||
sha=$(git rev-parse --short HEAD) | ||
echo "IMAGE_TAG=${{ steps.set_ref.outputs.ref }}_RC-${sha}" >> $GITHUB_OUTPUT | ||
fi | ||
shell: bash | ||
|
||
- name: Build and push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
file: ./Dockerfile | ||
push: true | ||
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.set_image_tag.outputs.IMAGE_TAG }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max | ||
|
||
create-release: | ||
needs: build-and-push | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ needs.build-and-push.outputs.ref }} | ||
|
||
- name: Load Docker image | ||
run: | | ||
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }} | ||
docker save ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }} > image.tar | ||
- name: Upload Docker image as artifact | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: docker-image | ||
path: image.tar | ||
|
||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ needs.build-and-push.outputs.image_tag }} | ||
release_name: Release ${{ needs.build-and-push.outputs.image_tag }} | ||
draft: false | ||
prerelease: false | ||
body: "Release of ${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }}" | ||
|
||
# - name: Upload Docker image to GitHub Release | ||
# uses: actions/upload-release-asset@v1 | ||
# env: | ||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# with: | ||
# upload_url: ${{ steps.create_release.outputs.upload_url }} | ||
# asset_path: ./image.tar | ||
# asset_name: docker-image-${{ needs.build-and-push.outputs.image_tag }}.tar | ||
# asset_content_type: application/octet-stream |