Skip to content

Schedule a release and image #76

Schedule a release and image

Schedule a release and image #76

name: Schedule a release and image
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 3'
env:
REGISTRY: ghcr.io
IMAGE_NAME: altinn/altinn-access-management-frontend
jobs:
getlatestrelease:
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
issues: write
packages: write
steps:
- name: setup current year
id: get_year
run: |
echo "currentyear=$(date +%Y)" >> $GITHUB_ENV
- name: Get Latest Release using github api
uses: octokit/[email protected]
continue-on-error: true
id: get_latest_release_version
with:
route: GET /repos/altinn/altinn-access-management-frontend/releases/latest
mediaType: | # The | is significant!
format: raw
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: print status
run: |
if [ "${{ steps.get_latest_release_version.outputs.status }}" == "200" ]; then
echo "Latest release version: ${{ fromJson(steps.get_latest_release_version.outputs.data).tag_name }}"
else
echo "Release could not be found. Request failed with status '${{ steps.get_latest_release_version.outputs.status }}'"
fi
- name: split release version f.ex v2023.1 will be split into output0 v2023 and output1 1
if: ${{ steps.get_latest_release_version.outputs.status == '200' }}
uses: winterjung/split@v2
id: split
with:
msg: '${{ fromJson(steps.get_latest_release_version.outputs.data).tag_name }}'
separator: '.'
- name: split 'v' and year f.ex v2023 will be split into output0 v and output1 2023
if: ${{ steps.get_latest_release_version.outputs.status == '200' }}
uses: winterjung/split@v2
id: splitv
with:
msg: '${{steps.split.outputs._0}}'
separator: 'v'
- name: Increment release version f.ex the release version for the year is incremented
if: ${{ steps.get_latest_release_version.outputs.status == '200' }}
id: update_version
env:
NUM: ${{ steps.split.outputs._1 }}
run: |
echo "VERSION=$(($NUM + 1))" >> $GITHUB_ENV
- name: Format new release version when there is atleast 1 previous release. If the release is created in the new year, the current year is set and the identity is reset to 1.
if: ${{ steps.get_latest_release_version.outputs.status == '200' }}
id: format_version
env:
RELEASE_YEAR: ${{steps.splitv.outputs._1}}
run: |
if [ ${{env.currentyear}} -gt ${{env.RELEASE_YEAR}} ]
then
echo "RELEASEVERSION=v${{env.currentyear}}.1" >> $GITHUB_OUTPUT
else
echo "RELEASEVERSION=v${{env.RELEASE_YEAR}}.${{env.VERSION}}" >> $GITHUB_OUTPUT
fi
- name: Set release version if 0 releases. The initial version is set to the v{currentyear}.{identity} f.ex v2023.1
id: releaseversion
run: |
if [ ${{ steps.get_latest_release_version.outputs.status }} = '200' ]
then
echo "RELEASE=${{ steps.format_version.outputs.RELEASEVERSION }}" >> $GITHUB_OUTPUT
else
echo "RELEASE=v${{env.currentyear}}.1" >> $GITHUB_OUTPUT
fi
- name: Print release version
run: |
echo "Release version: ${{ steps.releaseversion.outputs.RELEASE }}"
outputs:
RELEASEVERSION: ${{ steps.releaseversion.outputs.RELEASE }}
tag:
name: Tag image with release version
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
issues: write
packages: write
needs: [getlatestrelease]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: setup git config
run: |
# setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
git config user.name "github-actions[bot]"
git config user.email ""
git config -l | grep url
- name: Get last commit id
id: get_commit_sha
run: echo "commitid=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Login to GHCR
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: version
uses: battila7/get-version-action@v2
- name: Add Version tag to Docker Image
uses: shrink/actions-docker-registry-tag@v4
with:
registry: ${{ env.REGISTRY }}
repository: '${{ env.IMAGE_NAME }}'
target: '${{ steps.get_commit_sha.outputs.commitid }}'
tags: ${{ needs.getlatestrelease.outputs.RELEASEVERSION }}
update_release_draft:
name: Release Drafter with releaseversion that was formatted
needs: [getlatestrelease, tag]
permissions:
contents: write # for release-drafter/release-drafter to create a github release
pull-requests: write # for release-drafter/release-drafter to add label to PR
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
# Drafts your next Release notes as Pull Requests are merged into "master"
- uses: release-drafter/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag: ${{ needs.getlatestrelease.outputs.RELEASEVERSION }}
version: ${{ needs.getlatestrelease.outputs.RELEASEVERSION }}