From 466ac55102011ab88d818e11727b81ee6fd11a79 Mon Sep 17 00:00:00 2001 From: Michael Richardson Date: Wed, 8 Nov 2023 07:25:29 +1300 Subject: [PATCH] reusable build and release workflow (#3) Introduced re-usable build and release workflow --- .github/workflows/build-and-release.yaml | 87 ++++++++++++++++++++++ .github/workflows/release-main.yaml | 11 +++ .github/workflows/release-pull-request.yml | 80 +------------------- 3 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 .github/workflows/build-and-release.yaml create mode 100644 .github/workflows/release-main.yaml diff --git a/.github/workflows/build-and-release.yaml b/.github/workflows/build-and-release.yaml new file mode 100644 index 0000000..83cf268 --- /dev/null +++ b/.github/workflows/build-and-release.yaml @@ -0,0 +1,87 @@ +name: Build and Release + +on: + workflow_call: + inputs: + octopus-channel: + description: 'The Octopus release channel. Either "Branches" or "Release"' + required: true + type: string + +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + OCTOPUS_SPACE: 'SHIPPED23' + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + # This is used to complete the identity challenge + # with sigstore/fulcio when running outside of PRs. + id-token: write + + steps: + - name: Checkout + uses: actions/checkout@v3 + + # github.repository as / + - name: Capture image name + run: | + echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >>${GITHUB_ENV} + + - name: Get branch name + id: branch-name + uses: tj-actions/branch-names@v7 + + # Set up BuildKit Docker container builder to be able to build + # multi-platform images and export cache + # https://github.com/docker/setup-buildx-action + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 + with: + context: . + push: true + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Login to Octopus + uses: OctopusDeploy/login@v1 + with: + server: https://michrich.octopus.app + service_account_id: 1269d528-af54-4ab9-8a4b-902ac894a865 + + - name: Create a release in Octopus Deploy 🐙 + uses: OctopusDeploy/create-release-action@v3 + with: + project: 'SHIPPED23' + channel: ${{inputs.octopus-channel}} + release_number: ${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} + package_version: ${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} + git_ref: ${{ (github.ref_type == 'tag' && github.event.repository.default_branch ) || (github.head_ref || github.ref) }} + git_commit: ${{ github.event.after || github.event.pull_request.head.sha }} diff --git a/.github/workflows/release-main.yaml b/.github/workflows/release-main.yaml new file mode 100644 index 0000000..378e513 --- /dev/null +++ b/.github/workflows/release-main.yaml @@ -0,0 +1,11 @@ +name: Release - Main + +on: + push: + branches: [ "main" ] + +jobs: + build-and-release: + uses: MJRichardson/shipped23/.github/workflows/build-and-release.yaml@release-main + with: + octopus-channel: "Release" \ No newline at end of file diff --git a/.github/workflows/release-pull-request.yml b/.github/workflows/release-pull-request.yml index e056943..ccb6e7e 100644 --- a/.github/workflows/release-pull-request.yml +++ b/.github/workflows/release-pull-request.yml @@ -4,80 +4,8 @@ on: pull_request: branches: [ "main" ] -env: - # Use docker.io for Docker Hub if empty - REGISTRY: ghcr.io - OCTOPUS_SPACE: 'SHIPPED23' - jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - # This is used to complete the identity challenge - # with sigstore/fulcio when running outside of PRs. - id-token: write - - steps: - - name: Checkout - uses: actions/checkout@v3 - - # github.repository as / - - name: Capture image name - run: | - echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >>${GITHUB_ENV} - - - name: Get branch name - id: branch-name - uses: tj-actions/branch-names@v7 - - # Set up BuildKit Docker container builder to be able to build - # multi-platform images and export cache - # https://github.com/docker/setup-buildx-action - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 - - # Extract metadata (tags, labels) for Docker - # https://github.com/docker/metadata-action - - name: Extract Docker metadata - id: meta - uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - - - name: Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - # Build and push Docker image with Buildx (don't push on PR) - # https://github.com/docker/build-push-action - - name: Build and push Docker image - id: build-and-push - uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 - with: - context: . - push: true - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - - - name: Login to Octopus - uses: OctopusDeploy/login@v1 - with: - server: https://michrich.octopus.app - service_account_id: 1269d528-af54-4ab9-8a4b-902ac894a865 - - - name: Create a release in Octopus Deploy 🐙 - uses: OctopusDeploy/create-release-action@v3 - with: - project: 'SHIPPED23' - channel: 'Branches' - release_number: ${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} - package_version: ${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }} - git_ref: ${{ (github.ref_type == 'tag' && github.event.repository.default_branch ) || (github.head_ref || github.ref) }} - git_commit: ${{ github.event.after || github.event.pull_request.head.sha }} + build-and-release: + uses: MJRichardson/shipped23/.github/workflows/build-and-release.yaml@release-main + with: + octopus-channel: "Branches"