Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
ci(github-action): use output generated by semantic-release to push d…
Browse files Browse the repository at this point in the history
…ocker image

  ## what
  - use semantic-release plugin 'semantic-release-export-data' to store
    output in github actions whether a new version will be release ornot.
  - remove job 'extract-envs'
  - set NEXT_VERSION env depending if semantic-release will be released
    or not
  - push docker image IF a new version is being released

  ## how
  - use semantic-release plugin 'semantic-release-export-data' to store
    output in github actions whether a new version will be release ornot.
    - semantic-release plugin semantic-release-export-data will write
      output to github on whether a new version will be released. if a
      new version is being released, the tagname of the release version
  - set NEXT_VERSION env depending if semantic-release will be released
    or not
    - if there's NO new version, set NEXT_VERSION to the latest git tag
    - if there's a NEW version, set NEXT_VERSION to the new release
      version
  - push docker image IF a new version is being released
    - semantic-release will set an output to github-action to determine
      a new version will be released.

  ## why
  - eliminates one job
  - the entire process of extracting next release version is complicated
    and can go wrong
  - using 'semantic-release-export-data' plugin makes it easier
    extracting whether there's a new release and the new release version

  ## where

  ## usage
  • Loading branch information
Clumsy-Coder committed Jul 29, 2023
1 parent de8f098 commit 0b06e32
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ jobs:
run: npm run lint

################################################################################################
# extract semantic-release project next version and store them as github outputs.
# Will be used for building docker images
extract-envs:
# build docker image
docker-build:
# needs: extract-envs
needs: install
runs-on: ubuntu-latest
outputs:
NEXT_VERSION: ${{ steps.next-version.outputs.NEXT_VERSION }}
NEXT_VERSION: ${{ steps.dockerBuild-output.outputs.NEXT_VERSION }}
new-release-published: ${{ steps.get-next-version.outputs.new-release-published }}
new-release-version: ${{ steps.get-next-version.outputs.new-release-version }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/cache@v3
id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true'
Expand All @@ -80,46 +84,37 @@ jobs:
restore-keys: |
${{ runner.os }}-node-
# applies new version to package.json
# if there's no new version, package.json version will remain unchanged
- name: extract next version and apply to package.json
run: npx semantic-release -d | grep 'Release note' | grep -Po '(\d+)\.(\d+)\.(\d+)' | xargs npm version --allow-same-version --no-git-tag-version
- name: Extract next semantic-release version
# run: npx semantic-release --dry-run --branches="*"
run: npx semantic-release --dry-run
id: get-next-version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: store NEXT_VERSION as env
# needed incase semantic-release doesn't run on branches other than 'master' or 'development'
- name: Set NEXT_VERSION if there's NO new release
if: |
steps.get-next-version.outputs.new-release-published == '' ||
steps.get-next-version.outputs.new-release-published == 'false'
run: |
echo $(node -p "require('./package').version")
node -p "require('./package').version" | awk '{print "NEXT_VERSION=" $1}' >> $GITHUB_ENV
echo ${{ env.NEXT_VERSION }}
- name: store NEXT_VERSION to github output
id: next-version
run: echo "::set-output name=NEXT_VERSION::${{ env.NEXT_VERSION }}"
################################################################################################
# build docker image
# uses envs extracted from job "extract-env"
# uses envs from global env
docker-build:
needs: extract-envs
runs-on: ubuntu-latest
outputs:
NEXT_VERSION: ${{ steps.dockerBuild-output.outputs.NEXT_VERSION }}
LATEST_GIT_TAG: ${{ steps.dockerBuild-output.outputs.LATEST_GIT_TAG }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set NEXT_VERSION if there's a NEW release
if: steps.get-next-version.outputs.new-release-published == 'true'
run: |
echo ${{ steps.get-next-version.outputs.new-release-version }}
echo "NEXT_VERSION=${{ steps.get-next-version.outputs.new-release-version }}" >> $GITHUB_ENV
- name: Set Environment Variables
run: |
echo "BUILD_DATE=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
echo "GIT_SHA=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_ENV
echo "GIT_REF=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" >> $GITHUB_ENV
echo "NEXT_VERSION=${{ needs.extract-envs.outputs.NEXT_VERSION }}" >> $GITHUB_ENV
echo "LATEST_GIT_TAG=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
echo "GHCR_IMAGE=$(echo 'console.log("ghcr.io/${{ github.repository }}".toLowerCase())' | node -)" >> $GITHUB_ENV
- run: echo $GITHUB_ENV

- name: Create .env.local for NextJS
run: |
printf 'NEXT_PUBLIC_BUILD_VERSION=%s\n' '${{ env.NEXT_VERSION }}' >> .env.local
Expand Down Expand Up @@ -164,17 +159,11 @@ jobs:

- name: Push Docker images to GitHub Container Registry
# only push if on master branch AND there's a new version to push
if: ${{ github.ref == 'refs/heads/master' && env.NEXT_VERSION != env.LATEST_GIT_TAG }}
if: steps.get-next-version.outputs.new-release-published == 'true'
run: |
docker push ${{ env.GHCR_IMAGE }}:latest
docker push ${{ env.GHCR_IMAGE }}:${{ env.NEXT_VERSION }}
- name: store NEXT_VERSION and LATEST_GIT_TAG to github output
id: dockerBuild-output
run: |
echo "::set-output name=NEXT_VERSION::${{ env.NEXT_VERSION }}"
echo "::set-output name=LATEST_GIT_TAG::${{ env.LATEST_GIT_TAG }}"
################################################################################################

semantic-release:
Expand Down

0 comments on commit 0b06e32

Please sign in to comment.