Skip to content

fikse lagre-flyt etter endringer i domene #3413

fikse lagre-flyt etter endringer i domene

fikse lagre-flyt etter endringer i domene #3413

Workflow file for this run

name: Build
on:
push:
branches:
- main
- preprod/**
- dev/**
env:
ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }}
jobs:
generate_vars:
runs-on: ubuntu-latest
outputs:
short_sha: ${{ steps.set_short_sha.outputs.short_sha }}
matrix: ${{ steps.set_matrix.outputs.matrix }}
is_matrix_empty: ${{ steps.set_matrix.outputs.is_matrix_empty }}
deploy_matrix: ${{ steps.set_matrix.outputs.deploy_matrix }}
is_deploy_matrix_empty: ${{ steps.set_matrix.outputs.is_deploy_matrix_empty }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: gradle/actions/setup-gradle@v4
- name: Determine short SHA
id: set_short_sha
run: |
SHORT_SHA=$(echo ${{ github.sha }} | cut -c 1-7)
echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
- name: Determine changes files
run: |
# Create a comma-separated list of changed files for use in build.gradle.kts
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m $GITHUB_SHA | tr '\r\n' ',' | sed -e 's/,$//' | tr -d '"')
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
- name: Determine projects to deploy
id: set_matrix
run: |
MATRIX=$(./gradlew -q buildMatrix --console=plain)
echo $MATRIX
DEPLOY_MATRIX=$(./gradlew -q deployMatrixDev --console=plain)
echo $DEPLOY_MATRIX
MATRIX_SIZE=$(echo $MATRIX | jq '.project|length')
DEPLOY_MATRIX_SIZE=$(echo $DEPLOY_MATRIX | jq '.project|length')
if [ "$MATRIX_SIZE" == '0' ]; then
echo "Empty matrix"
echo "matrix=[]" >> $GITHUB_OUTPUT # to prevent error because matrix is empty
echo "is_matrix_empty=true" >> $GITHUB_OUTPUT
else
echo Setting matrix to $MATRIX
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
echo "is_matrix_empty=false" >> $GITHUB_OUTPUT
fi
if [ "$DEPLOY_MATRIX_SIZE" == '0' ]; then
echo "Empty deploy matrix"
echo "deploy_matrix=[]" >> $GITHUB_OUTPUT # to prevent error because matrix is empty
echo "is_deploy_matrix_empty=true" >> $GITHUB_OUTPUT
else
echo Setting deploy matrix to $DEPLOY_MATRIX
echo "deploy_matrix=$DEPLOY_MATRIX" >> $GITHUB_OUTPUT
echo "is_deploy_matrix_empty=false" >> $GITHUB_OUTPUT
fi
build:
runs-on: ubuntu-latest
permissions:
packages: write
id-token: write
needs: generate_vars
if: needs.generate_vars.outputs.is_matrix_empty == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: 21
distribution: temurin
- uses: gradle/actions/setup-gradle@v4
- name: Test and build
run: ./gradlew ${{ matrix.project }}:test ${{ matrix.project }}:build
- name: Check app.jar existence
id: app_jar
uses: andstor/file-existence-action@v3
with:
files: "apps/${{ matrix.project }}/build/libs/app.jar"
- name: build docker image
if: steps.app_jar.outputs.files_exists == 'true'
uses: nais/docker-build-push@v0
id: docker-build-push
with:
team: helsearbeidsgiver
image_suffix: im-${{ matrix.project }}
tag: ${{ needs.generate_vars.outputs.short_sha }}
dockerfile: Dockerfile
docker_context: apps/${{ matrix.project }}
- name: lag build fil
if: ${{ always() }}
run: |
echo ${{ steps.docker-build-push.outputs.image }} > "${{ matrix.project }}_build_${{ steps.docker-build-push.outcome }}.txt"
- name: laste opp build-fil
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
overwrite: true
retention-days: 1
name: "${{ matrix.project }}_build_result"
path: "${{ matrix.project }}_build_${{ steps.docker-build-push.outcome }}.txt"
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
needs: [generate_vars, build]
if: needs.generate_vars.outputs.is_deploy_matrix_empty == 'false'
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.generate_vars.outputs.deploy_matrix) }}
concurrency:
group: deploy-dev-${{ matrix.project }}
steps:
- name: Determine app configuration file
run: |
CONFIG_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml"
echo "VARS=$CONFIG_FILE" >> $GITHUB_ENV # VARS-variable is used by nais/deploy/actions/deploy
- name: Confirm app configuration file existence
id: confirm_config_file
uses: andstor/file-existence-action@v3
with:
files: "${{ env.VARS }}"
- name: Fetch sources
uses: actions/checkout@v4
- name: create env vars
run: |
# VARS og IMAGE er miljøvariabler som leses automatisk av nais/deploy/actions/deploy
VARS_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml"
echo "VARS=$VARS_FILE" >> $GITHUB_ENV
- name: laste ned build-fil
uses: actions/download-artifact@v4
with:
name: "${{ matrix.project }}_build_result"
- name: ser om vi har nais config og at bygget gikk bra
id: deploy-files
uses: andstor/file-existence-action@v3
with:
files: "${{ env.VARS }}, ${{ matrix.project }}_build_success.txt"
- name: Sett docker image som env-var
id: sett_docker_image_var
run: echo "docker_image=$(cat ${{ matrix.project }}_build_success.txt)" >> $GITHUB_ENV
- name: Deploy
if: steps.deploy-files.outputs.files_exists == 'true'
uses: nais/deploy/actions/deploy@v2
env:
CLUSTER: ${{ matrix.cluster }}
IMAGE: ${{ env.docker_image }}
RESOURCE: config/nais.yml
VAR: team=helsearbeidsgiver,app=${{ matrix.project }}