fjerner ubrukt input #2
Workflow file for this run
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: Deploy | ||
on: | ||
workflow_call: | ||
inputs: | ||
image: | ||
required: true | ||
type: string | ||
description: 'Image tag to deploy (example 2023.03.10.080433-f821119)' | ||
cluster: | ||
required: true | ||
type: string | ||
description: 'The cluster to deploy to (dev-fss|dev-gcp|prod-fss|prod-gcp)' | ||
namespace: | ||
required: false | ||
type: string | ||
description: 'The namespace to deploy to' | ||
default: 'k9saksbehandling' | ||
gar: | ||
required: false | ||
type: string | ||
description: 'Deploy using Google Artifact Registry' | ||
default: 'false' | ||
naiserator_file: | ||
required: false | ||
type: string | ||
description: 'The naiserator.yaml file name in .deploy/ to use' | ||
default: 'nais/naiserator.yaml' | ||
input_vars_file: | ||
required: false | ||
type: string | ||
description: 'The naiserator.yaml file name in .deploy/ to use' | ||
image_suffix: | ||
required: false | ||
type: string | ||
description: 'The suffix of the image to be deployed' | ||
default: '' | ||
branch: | ||
required: false | ||
type: string | ||
description: 'Alternative branch to deploy from' | ||
override_image_name: | ||
required: false | ||
type: string | ||
description: 'Complete image name to deploy. When set, image and image_suffix inputs are ignored. For use when image to upload don't have navikt/ prefix' | ||
default: '' | ||
env: | ||
IMAGE: ${{ inputs.image }} # Brukes i naiserator.yaml | ||
PROD_TAG: latest_${{ inputs.cluster }}_${{ inputs.namespace }}${{ inputs.image_suffix }} | ||
jobs: | ||
commit_url: | ||
name: comparison_url | ||
runs-on: 'ubuntu-latest' | ||
if: contains(inputs.cluster, 'prod') | ||
steps: | ||
- name: Construct comparison url for last deploy | ||
id: compare_commits | ||
run: | | ||
IMAGE_SHA=$(echo '${{ inputs.image }}' | awk -F'-' '{print $2}') | ||
url="https://github.com/${{ github.repository }}/compare/${{ env.PROD_TAG }}...${IMAGE_SHA}" >> $GITHUB_OUTPUT | ||
echo "Diff last deploy to ${{ env.PROD_TAG }} : <${url}>" >> $GITHUB_STEP_SUMMARY | ||
deploy: | ||
name: deploy | ||
#Tillater én aktiv deployment per miljø. Dette fordi deployments som aldri blir kjørt feiler etter 30 dager og notifikasjon om feilet bygg | ||
#cuncurrency gjør at pending deployments blir kansellert og får status cancel istedenfor failed, som da kan filtreres vekk (i mailklienten). | ||
concurrency: ${{ inputs.cluster }}:${{ inputs.namespace }}${{ inputs.image_suffix }} | ||
environment: ${{ inputs.cluster }}:${{ inputs.namespace }}${{ inputs.image_suffix }} | ||
runs-on: 'ubuntu-latest' | ||
permissions: | ||
contents: write #for å lage tag | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # ratchet:actions/checkout@v4 | ||
with: | ||
ref: ${{ (inputs.branch != '' && inputs.branch) || '' }} | ||
- name: Deploy ${{ inputs.cluster }} fra Github | ||
if: inputs.gar == 'false' | ||
uses: nais/deploy/actions/deploy@v2 | ||
env: | ||
PRINT_PAYLOAD: true | ||
CLUSTER: ${{ inputs.cluster }} | ||
RESOURCE: ${{ inputs.naiserator_file }} | ||
VARS: ${{ inputs.input_vars_file }} | ||
- name: Login GAR | ||
uses: nais/login@4ce7445c84f09114e481a1d2c164f563e9e2ba8e # ratchet:nais/login@v0 | ||
if: inputs.gar == 'true' | ||
id: login | ||
with: | ||
project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }} | ||
identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }} | ||
team: ${{ inputs.namespace }} | ||
- name: Deploy ${{ inputs.cluster }} fra GAR | ||
if: inputs.gar == 'true' | ||
uses: nais/deploy/actions/deploy@v2 | ||
env: | ||
PRINT_PAYLOAD: true | ||
CLUSTER: ${{ inputs.cluster }} | ||
IMAGE: ${{ inputs.override_image_name != '' && inputs.override_image_name || format('{0}/{1}{2}:{3}', steps.login.outputs.registry, github.repository, inputs.image_suffix, inputs.image) }} | ||
RESOURCE: ${{ inputs.naiserator_file }} | ||
VARS: ${{ inputs.input_vars_file }} | ||
- name: Finn commit sha fra image navn | ||
if: contains(inputs.cluster, 'prod') && success() | ||
run: | | ||
echo "TAG_COMMIT=$(echo '${{ inputs.image }}' | awk -F'-' '{print $2}')" >> $GITHUB_ENV | ||
- name: Tagg prodsatt commit | ||
if: contains(inputs.cluster, 'prod') && success() | ||
uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const partial_sha = '${{ env.TAG_COMMIT }}' | ||
const latestName = '${{ env.PROD_TAG }}' | ||
const latestRef = 'tags/' + latestName | ||
let tag_commit; | ||
try { | ||
const commit_path = 'GET /repos/' + context.repo.owner + '/' + context.repo.repo + '/commits/' + partial_sha | ||
let commitDetails = await github.request(commit_path, { | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: partial_sha | ||
}); | ||
tag_commit = commitDetails.data.sha; | ||
console.log("Fant full sha: " + tag_commit); | ||
} catch (err) { | ||
console.log("feilet ved henting av commit", err); | ||
throw Error("feilet ved henting av commit: " + partial_sha); | ||
} | ||
console.log("ser om tagg=" + latestRef + " finnes...") | ||
try { | ||
let previous = await github.rest.git.getRef({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: latestRef | ||
}) | ||
console.log(latestRef + " finnes! sletter...") | ||
let slettet = await github.rest.git.deleteRef({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: latestRef | ||
}) | ||
console.log("slettet gamle " + latestName) | ||
} catch (err) { | ||
if (err.status == 404) { | ||
console.log("fant ikke ref " + latestRef) | ||
} else { | ||
console.log(err) | ||
throw Error("Feil ved henting/sletting av " + latestRef + ": " + err.status) | ||
} | ||
} | ||
console.log("lager tagg=" + latestName + " på commit " + tag_commit) | ||
await github.rest.git.createRef({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: 'refs/tags/' + latestName, | ||
sha: tag_commit | ||
}).then((res) => { | ||
console.log("laget tagg=" + latestName) | ||
}) |