From cd733b789a71bbf3434e1820653e709ca8579889 Mon Sep 17 00:00:00 2001 From: asaph96 Date: Sat, 20 Jul 2024 12:07:30 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20add=20RC=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/docker-release.yml | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 .github/workflows/docker-release.yml diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml new file mode 100644 index 0000000..b26bdd1 --- /dev/null +++ b/.github/workflows/docker-release.yml @@ -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