From 84060f09b5b8aa34d29bd6fb154ecb1172c110ec Mon Sep 17 00:00:00 2001 From: na2na-p Date: Mon, 27 Nov 2023 13:07:47 +0900 Subject: [PATCH] =?UTF-8?q?cd=E7=B5=84=E3=82=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-for-v3-main.yml | 47 ++++++++++++++++ .github/workflows/image-build-and-publish.yml | 53 +++++++++++++++++++ infra/docker/image-bake.hcl | 10 ++++ 3 files changed, 110 insertions(+) create mode 100644 .github/workflows/cd-for-v3-main.yml create mode 100644 .github/workflows/image-build-and-publish.yml create mode 100644 infra/docker/image-bake.hcl diff --git a/.github/workflows/cd-for-v3-main.yml b/.github/workflows/cd-for-v3-main.yml new file mode 100644 index 00000000..b988330a --- /dev/null +++ b/.github/workflows/cd-for-v3-main.yml @@ -0,0 +1,47 @@ +name: CD for v3(main) + +on: + push: + branches: + - v3 + workflow_dispatch: + +env: + DOCKER_TAG_API_URI: https://registry.hub.docker.com/v2/repositories/na2na/jetdisc/tags + +jobs: + Prepare: + name: Prepare + runs-on: ubuntu-latest + outputs: + tag_exists: ${{ steps.check_docker_hub.outputs.tag_exists }} + JETDISC_VERSION: ${{ steps.check_docker_hub.outputs.JETDISC_VERSION }} + steps: + - name: Check out the repo + uses: actions/checkout@v4.1.1 + - name: Get current version + id: get_version + run: | + echo "JETDISC_VERSION=$(cat package.json | jq -r '.version')" >> $GITHUB_ENV + - name: Check Docker Hub + id: check_docker_hub + run: | + DOCKER_HUB_TAGS=$(curl -s ${{ env.DOCKER_TAG_API_URI }} | jq -r '.results[] | .name') + if [ $(echo $DOCKER_HUB_TAGS | grep -c $JETDISC_VERSION) -eq 1 ]; then + echo "Tag $JETDISC_VERSION already exists on Docker Hub." + else + echo "Tag $JETDISC_VERSION does not exist on Docker Hub." + echo "tag_exists=false" >> $GITHUB_OUTPUT + echo "JETDISC_VERSION=$JETDISC_VERSION" >> $GITHUB_OUTPUT + fi + + Build-and-Push-to-Docker-Hub: + needs: + - Prepare + if: ${{ needs.Prepare.outputs.tag_exists == 'false' }} + with: + JETDISC_VERSION: ${{ needs.Prepare.outputs.JETDISC_VERSION }} + IMAGE_REPOSITORY: na2na + IMAGE_NAME: jetdisc + uses: ./.github/workflows/image-build-and-publish.yml + secrets: inherit diff --git a/.github/workflows/image-build-and-publish.yml b/.github/workflows/image-build-and-publish.yml new file mode 100644 index 00000000..52d985fc --- /dev/null +++ b/.github/workflows/image-build-and-publish.yml @@ -0,0 +1,53 @@ +name: Publish Docker Image + +on: + workflow_call: + inputs: + JETDISC_VERSION: + required: true + type: string + IMAGE_REPOSITORY: + required: true + type: string + IMAGE_NAME: + required: true + type: string + secrets: {} + +jobs: + Build-and-Push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Prepare for build + id: prepare-for-build + run: | + echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + - name: Cache Docker Build Cache + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: docker-build-cache-${{ github.ref }}-${{ github.sha }} + restore-keys: | + docker-build-cache-${{ github.ref }} + docker-build-cache- + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + with: + driver-opts: network=host + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Build Docker Image + run: | + docker buildx bake \ + -f 'infra/docker/image-bake.hcl' \ + --builder="${{ steps.buildx.outputs.name }}" \ + --set='web.tags=${{ inputs.IMAGE_REPOSITORY }}/${{ inputs.IMAGE_NAME }}:${{ inputs.JETDISC_VERSION }}' \ + --set='web.tags=${{ inputs.IMAGE_REPOSITORY }}/${{ inputs.IMAGE_NAME }}:${{ steps.prepare-for-build.outputs.sha_short }}' \ + --set='web.tags=${{ inputs.IMAGE_REPOSITORY }}/${{ inputs.IMAGE_NAME }}:release' \ + --push diff --git a/infra/docker/image-bake.hcl b/infra/docker/image-bake.hcl new file mode 100644 index 00000000..7c4ee44d --- /dev/null +++ b/infra/docker/image-bake.hcl @@ -0,0 +1,10 @@ +group "default" { + targets = ["app"] +} + +target "app" { + dockerfile = "Dockerfile" + platforms = ["linux/amd64", "linux/arm64"] + cache-to = ["type=local,dest=/tmp/.buildx-cache"] + cache-from = ["type=local,src=/tmp/.buildx-cache"] +}