From 505c200f387ee9ae369585385b936a2cbe0d7454 Mon Sep 17 00:00:00 2001 From: Boris Capitanu Date: Mon, 13 May 2024 00:44:29 -0500 Subject: [PATCH] Split CI and CD workflows Separated the Continuous Integration and Continuous Deployment workflows into two separate files, ci.yml and cd.yml respectively. This split offers better clarity and manageability. Removed Docker build and push from the CI workflow and moved it to the newly created CD workflow. --- .github/workflows/cd.yml | 104 +++++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 97 ++---------------------------------- 2 files changed, 108 insertions(+), 93 deletions(-) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..588a1d2 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,104 @@ +name: CD + +on: + workflow_dispatch: + inputs: + environment: + description: 'Deployment environment (dev|stage|prod)' + required: true + default: 'dev' + + push: + branches: + - develop + - release + +permissions: + contents: read + +env: + JAVA_VERSION: 21 + DOCKER_REGISTRY: ghcr.io + DOCKER_IMAGE_NAME: ${{ github.repository }} + HTRC_NEXUS_DRHTRC_PWD: ${{ secrets.HTRC_NEXUS_DRHTRC_PWD }} + +jobs: + docker-build: + name: Build and push Docker image + runs-on: ubuntu-latest + permissions: + packages: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Generate Dockerfile + run: sbt "Docker/stage" + - name: Show GitHub context + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: echo "$GITHUB_CONTEXT" + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }} + tags: | + type=sha,prefix={{branch}}-,priority=750,enable=${{ startsWith(github.ref, 'refs/heads/') }} + type=ref,event=branch + type=ref,event=pr + type=pep440,pattern={{version}} + type=pep440,pattern={{major}}.{{minor}} + type=raw,value=latest,enable={{is_default_branch}} + labels: | + org.opencontainers.image.vendor=HathiTrust Research Center + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.DOCKER_REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push Docker images + uses: docker/build-push-action@v5 + with: + context: target/docker/stage/ + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + - name: Repository Dispatch + if: github.event.inputs.environment == '' + uses: peter-evans/repository-dispatch@v3 + env: + HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} + with: + token: ${{ secrets.PAT }} + repository: htrc/torchlite-argocd + event-type: argocd + client-payload: >- + { + "image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", + "commit_msg": "[${{ github.head_ref || github.ref_name }}]: ${{ env.HEAD_COMMIT_MESSAGE }}", + "repository": "${{ github.event.repository.name }}", + "environment": "${{ github.event.inputs.environment }}", + "ref": "${{ github.head_ref || github.ref_name }}" + } + - name: Manual Repository Dispatch + if: github.event.inputs.environment != '' + uses: peter-evans/repository-dispatch@v3 + env: + HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} + with: + token: ${{ secrets.PAT }} + repository: htrc/torchlite-argocd + event-type: argocd-manual + client-payload: >- + { + "image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", + "commit_msg": "[${{ github.head_ref || github.ref_name }}]: Manual deployment by ${{ github.triggering_actor }}", + "repository": "${{ github.event.repository.name }}", + "environment": "${{ github.event.inputs.environment }}", + "ref": "${{ github.head_ref || github.ref_name }}" + } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8dd9d7b..65968df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,25 +1,17 @@ name: CI on: - workflow_dispatch: - inputs: - environment: - description: 'Deployment environment (dev|stage|prod)' - required: true - default: 'dev' - push: - branches: - - develop - - release + branches: [ "develop", "release" ] + + pull_request: + branches: [ "develop", "release" ] permissions: contents: read env: JAVA_VERSION: 21 - DOCKER_REGISTRY: ghcr.io - DOCKER_IMAGE_NAME: ${{ github.repository }} HTRC_NEXUS_DRHTRC_PWD: ${{ secrets.HTRC_NEXUS_DRHTRC_PWD }} jobs: @@ -44,84 +36,3 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false - - docker-build: - name: Build and push Docker image - runs-on: ubuntu-latest - permissions: - packages: write - needs: ci - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Generate Dockerfile - run: sbt "Docker/stage" - - name: Show GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_NAME }} - tags: | - type=sha,prefix={{branch}}-,priority=750,enable=${{ startsWith(github.ref, 'refs/heads/') }} - type=ref,event=branch - type=ref,event=pr - type=pep440,pattern={{version}} - type=pep440,pattern={{major}}.{{minor}} - type=raw,value=latest,enable={{is_default_branch}} - labels: | - org.opencontainers.image.vendor=HathiTrust Research Center - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ${{ env.DOCKER_REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push Docker images - uses: docker/build-push-action@v5 - with: - context: target/docker/stage/ - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - - name: Repository Dispatch - if: github.event.inputs.environment == '' - uses: peter-evans/repository-dispatch@v3 - env: - HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} - with: - token: ${{ secrets.PAT }} - repository: htrc/torchlite-argocd - event-type: argocd - client-payload: >- - { - "image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", - "commit_msg": "[${{ github.head_ref || github.ref_name }}]: ${{ env.HEAD_COMMIT_MESSAGE }}", - "repository": "${{ github.event.repository.name }}", - "environment": "${{ github.event.inputs.environment }}", - "ref": "${{ github.head_ref || github.ref_name }}" - } - - name: Manual Repository Dispatch - if: github.event.inputs.environment != '' - uses: peter-evans/repository-dispatch@v3 - env: - HEAD_COMMIT_MESSAGE: ${{ toJSON(github.event.head_commit.message) }} - with: - token: ${{ secrets.PAT }} - repository: htrc/torchlite-argocd - event-type: argocd-manual - client-payload: >- - { - "image": "${{ fromJSON(steps.meta.outputs.json).tags[0] }}", - "commit_msg": "[${{ github.head_ref || github.ref_name }}]: Manual deployment by ${{ github.triggering_actor }}", - "repository": "${{ github.event.repository.name }}", - "environment": "${{ github.event.inputs.environment }}", - "ref": "${{ github.head_ref || github.ref_name }}" - }