nedskalerer db i dev #508
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build | |
on: | |
push: | |
branches: | |
- main | |
env: | |
DOCKER_REGISTRY: ghcr.io | |
DOCKER_IMAGE_PREFIX: ${{ github.repository }}/spreapp- | |
RESOURCE: config/nais.yml | |
TEAM: tbd | |
jobs: | |
generate_vars: | |
runs-on: ubuntu-latest | |
outputs: | |
team: ${{ steps.set-vars.outputs.team }} | |
tag: ${{ steps.set-vars.outputs.tag }} | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
deployMatrix: ${{ steps.set-matrix.outputs.deployMatrix }} | |
emptyMatrix: ${{ steps.set-matrix.outputs.emptyMatrix }} | |
emptyDeployMatrix: ${{ steps.set-matrix.outputs.emptyDeployMatrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
- name: cache gradle wrapper | |
uses: actions/cache@v3 | |
with: | |
path: ~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }} | |
- id: set-vars | |
run: | | |
TAG=$(echo ${{ github.sha }} | cut -c1-7) | |
echo "team=${TEAM}" >> $GITHUB_OUTPUT | |
echo "tag=${TAG}" >> $GITHUB_OUTPUT | |
- id: set-changed-files | |
run: | | |
# create a comma-separated list of changed files | |
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r $GITHUB_SHA | tr '\r\n' ',' | sed -e 's/,$//') | |
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV | |
- id: set-matrix | |
run: | | |
MATRIX=$(./gradlew -q buildMatrix --console=plain) | |
DEPLOY_MATRIX=$(./gradlew -q deployMatrix --console=plain) | |
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 | |
echo "emptyMatrix=true" >> $GITHUB_OUTPUT | |
else | |
echo Setting matrix to $MATRIX | |
echo "matrix=${MATRIX}" >> $GITHUB_OUTPUT | |
echo "emptyMatrix=false" >> $GITHUB_OUTPUT | |
fi | |
if [ "$DEPLOY_MATRIX_SIZE" == '0' ]; then | |
echo "Empty deploy matrix" | |
echo "deployMatrix=[]" >> $GITHUB_OUTPUT | |
echo "emptyDeployMatrix=true" >> $GITHUB_OUTPUT | |
else | |
echo Setting deploy matrix to $DEPLOY_MATRIX | |
echo "deployMatrix=${DEPLOY_MATRIX}" >> $GITHUB_OUTPUT | |
echo "emptyDeployMatrix=false" >> $GITHUB_OUTPUT | |
fi | |
build: | |
needs: generate_vars | |
permissions: | |
packages: write | |
name: build | |
runs-on: ubuntu-latest | |
if: needs.generate_vars.outputs.emptyMatrix == 'false' | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJSON(needs.generate_vars.outputs.matrix) }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17.x' | |
- name: cache gradle wrapper | |
uses: actions/cache@v3 | |
with: | |
path: ~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }} | |
- name: test and build | |
run: ./gradlew --info ${{ matrix.project }}::test ${{ matrix.project }}::build | |
- name: Check app.jar existence | |
id: app_jar | |
uses: andstor/file-existence-action@v2 | |
with: | |
files: "${{ matrix.project }}/build/libs/app.jar" | |
- name: create docker tag | |
if: steps.app_jar.outputs.files_exists == 'true' | |
run: | | |
echo "IMAGE=${DOCKER_REGISTRY}/${DOCKER_IMAGE_PREFIX}${{ matrix.project }}:${{ needs.generate_vars.outputs.tag }}" >> $GITHUB_ENV | |
- name: build docker image | |
if: steps.app_jar.outputs.files_exists == 'true' | |
run: docker build ${{ matrix.project }} --pull -t $IMAGE -f Dockerfile | |
- name: push docker image | |
if: steps.app_jar.outputs.files_exists == 'true' | |
run: | | |
echo $GITHUB_TOKEN | docker login ${DOCKER_REGISTRY} --username $GITHUB_REPOSITORY --password-stdin | |
docker push $IMAGE | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
deploy: | |
needs: [generate_vars, build] | |
name: deploy | |
if: always() && needs.generate_vars.outputs.emptyDeployMatrix == 'false' | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJSON(needs.generate_vars.outputs.deployMatrix) }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: check if docker image exists | |
id: docker-exists | |
run: | | |
TOKEN=$(curl https://${DOCKER_REGISTRY}/token\?scope\="repository:${DOCKER_IMAGE_PREFIX}${{ matrix.project }}:pull" | jq -r '.token') | |
curl --head --fail -H "Authorization: Bearer ${TOKEN}" https://${DOCKER_REGISTRY}/v2/${DOCKER_IMAGE_PREFIX}${{ matrix.project }}/manifests/${{ needs.generate_vars.outputs.tag }} | |
IMAGE_EXISTS=$? | |
if [ ${IMAGE_EXISTS} -ne 0 ]; then | |
echo "exists=false" >> $GITHUB_OUTPUT | |
else | |
echo "exists=true" >> $GITHUB_OUTPUT | |
fi | |
- name: create env vars | |
if: steps.docker-exists.outputs.exists == 'true' | |
run: | | |
VARS_FILE="config/${{ matrix.project }}/${{ matrix.cluster }}.yml" | |
echo "VARS=$VARS_FILE" >> $GITHUB_ENV | |
echo "IMAGE=${DOCKER_REGISTRY}/${DOCKER_IMAGE_PREFIX}${{ matrix.project }}:${{ needs.generate_vars.outputs.tag }}" >> $GITHUB_ENV | |
- name: Fetch sources | |
if: steps.docker-exists.outputs.exists == 'true' | |
uses: actions/checkout@v4 | |
- name: Check for vars yml | |
if: steps.docker-exists.outputs.exists == 'true' | |
id: vars_file | |
uses: andstor/file-existence-action@v2 | |
with: | |
files: "${{ env.VARS }}" | |
- name: deploy | |
if: steps.docker-exists.outputs.exists == 'true' && steps.vars_file.outputs.files_exists == 'true' | |
uses: nais/deploy/actions/deploy@v1 | |
env: | |
CLUSTER: ${{ matrix.cluster }} | |
APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }} | |
VAR: team=${{ needs.generate_vars.outputs.team }},app=${{ matrix.project }},git_sha=${{ github.sha }} | |