promote-command #788
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: Promote | |
on: | |
repository_dispatch: | |
types: [promote-command] | |
env: | |
IMAGE_BASE: ghcr.io/${{ github.repository }} | |
NAMESPACE: k9saksbehandling | |
jobs: | |
promote: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
issues: write | |
steps: | |
- name: Henter informasjon fra issuet | |
id: issue_comment | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
github.rest.issues.get( { | |
owner: context.issue.owner, | |
repo: context.issue.repo, | |
issue_number: ${{ github.event.client_payload.github.payload.issue.number }} }) | |
.then(response => { | |
core.setOutput('sha', response.data.body); | |
core.setOutput('title', response.data.title); | |
}); | |
- name: Setter IMAGE som brukes i deploy-filen & TAG for kode checkout | |
env: | |
TAG_INPUT: ${{ steps.issue_comment.outputs.title }} | |
CLUSTER_INPUT: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 }} | |
run: | | |
echo "IMAGE=$IMAGE_BASE:$(echo $TAG_INPUT | awk '{print $NF}')" >> $GITHUB_ENV | |
echo "TAG=$(echo $TAG_INPUT | awk -F- '{print $NF}')" >> $GITHUB_ENV | |
echo "CLUSTER=$CLUSTER_INPUT" >> $GITHUB_ENV | |
- name: Sjekk ut kode | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ steps.issue_comment.outputs.sha }} | |
- name: Promoter til cluster og namespace | |
uses: nais/deploy/actions/deploy@v1 | |
env: | |
APIKEY: ${{ secrets.DEPLOY_KEY }} | |
CLUSTER: ${{ env.CLUSTER }} | |
RESOURCE: nais/${{ env.CLUSTER }}.yml | |
- name: Oppdater kommentar med deployment status ved feil | |
if: failure() | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const issue = { owner: context.issue.owner, | |
repo: context.issue.repo, | |
issue_number: ${{ github.event.client_payload.github.payload.issue.number }} } | |
github.rest.issues.createComment({...issue, | |
title: 'Deploy av ${{ env.TAG }}', | |
body: 'promote til ${{ env.CLUSTER }} ${{ env.NAMESPACE }} feilet'}) | |
github.rest.issues.addLabels({...issue, | |
labels: ['deployment-failed','${{ env.CLUSTER }}-${{ env.NAMESPACE }}']}) | |
- name: Oppdater kommentar med deployment status | |
if: success() | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const issue = { owner: context.issue.owner, | |
repo: context.issue.repo, | |
issue_number: ${{ github.event.client_payload.github.payload.issue.number }} } | |
github.rest.issues.createComment({...issue, | |
body: 'promote til ${{ env.CLUSTER }} ${{ env.NAMESPACE }} utført'}) | |
github.rest.issues.addLabels({...issue, labels: ['deployment','${{ env.CLUSTER }}-${{ env.NAMESPACE }}']}) | |
- name: Klargjør for å tagge prodsatt commit | |
if: success() && env.CLUSTER == 'prod-fss' | |
run: | | |
echo "NOW=$(TZ=Europe/Oslo date +'%Y%m%d.%H%M%S')" >> $GITHUB_ENV | |
echo "COMMIT_SHA=$(git rev-parse ${{ env.TAG }})" >> $GITHUB_ENV | |
- name: Tagg prodsatt commit | |
if: success() && env.CLUSTER == 'prod-fss' | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const latestName = "PROD" | |
const tagName = "PROD_${{ env.NOW }}" | |
console.log("lager tagg=" + tagName) | |
github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'refs/tags/' + tagName, | |
sha: "${{ env.COMMIT_SHA }}" | |
}).then((res) => { | |
console.log("laget tagg=" + tagName) | |
}) | |
const latestRef = 'tags/' + latestName | |
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) | |
await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'refs/tags/' + latestName, | |
sha: "${{ env.COMMIT_SHA }}" | |
}).then((res) => { | |
console.log("laget tagg=" + latestName) | |
}) |