Skip to content

Instantly share code, notes, and snippets.

@mkutz
Last active October 15, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkutz/03723b8f5752f45f4aa11a5d6b3c25a7 to your computer and use it in GitHub Desktop.
Save mkutz/03723b8f5752f45f4aa11a5d6b3c25a7 to your computer and use it in GitHub Desktop.
Build and reused deployment GitHub workflow
name: Build
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.determine-tags.outputs.version }}
steps:
- uses: actions/setup-java@v2
with:
distribution: temurin
java-version: 11
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Cache SonarCloud packages
uses: actions/cache@v2
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-maven
- name: Determine tags
id: determine-tags
run: |
echo "::set-output name=version::v$(git rev-list HEAD --count)"
echo "::set-output name=commit::$(git rev-parse --short HEAD)"
- run: |
./mvnw clean versions:set -DnewVersion=${{ steps.determine-tags.outputs.version }}-${{ steps.determine-tags.outputs.commit }} \
--batch-mode --no-transfer-progress
- run: |
./mvnw verify \
--batch-mode --no-transfer-progress
- if: ${{ github.actor != 'dependabot[bot]' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}
run: |
./mvnw sonar:sonar \
--batch-mode --no-transfer-progress
- name: Build container image
run: |
docker build --pull . \
--tag ${{ env.IMAGE_NAME }}:v${{ steps.determine-tags.outputs.version }} \
--tag ${{ env.IMAGE_NAME }}:${{ steps.determine-tags.outputs.commit }} \
--tag ${{ env.IMAGE_NAME }}:latest
- name: Push container image
run: |
docker push ${{ env.IMAGE_NAME }}:v${{ steps.determine-tags.outputs.version }}
docker push ${{ env.IMAGE_NAME }}:${{ steps.determine-tags.outputs.commit }}
docker push ${{ env.IMAGE_NAME }}:latest
deploy-dev:
if: github.ref == 'refs/heads/main'
needs:
build
uses: <repo-owner>/<repo-name>/.github/workflows/deploy.yml@main
with:
target: dev
version: ${{ needs.build.outputs.version }}
secrets:
deploy_token: ${{ secrets.deploy_token }}
deploy-int:
if: github.ref == 'refs/heads/main'
needs:
build
uses: rewe-digital-fulfillment/log-fulfillment-picking/.github/workflows/deploy.yml@main
with:
target: int
version: ${{ needs.build.outputs.version }}
secrets:
deploy_token: ${{ secrets.deploy_token }}
deploy-prd:
if: github.ref == 'refs/heads/main'
needs:
build
uses: rewe-digital-fulfillment/log-fulfillment-picking/.github/workflows/deploy.yml@main
with:
target: prod
version: ${{ needs.build.outputs.version }}
secrets:
deploy_token: ${{ secrets.deploy_token }}
name: Deploy
on:
workflow_dispatch:
inputs:
target:
description: Stage to deploy to (dev, int or prod)
required: true
version:
description: Version to be deployed
required: true
workflow_call:
inputs:
target:
description: Stage to deploy to (dev, int or prod)
required: true
type: string
version:
description: Version to be deployed
required: true
type: string
secrets:
deploy_token:
required: true
jobs:
deploy:
runs-on: ubuntu-latest
env:
TARGET: ${{ github.event.inputs.target }}${{ inputs.target }} # needed as workflow_dispatch and workflow_call have different inputs
VERSION: ${{ github.event.inputs.version }}${{ inputs.version }} # needed as workflow_dispatch and workflow_call have different inputs
environment: ${{ github.event.inputs.target }}${{ inputs.target }} # allows to define environment protection rules
steps:
- uses: actions/checkout@v2
- run: deploy.sh ${{ env.TARGET }} ${{ env.VERSION }} ${{ secrets.deploy_token }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment