-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (76 loc) · 3.01 KB
/
image-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: "Image Build"
on:
workflow_dispatch:
inputs:
tag:
required: true
type: string
workflow_call:
inputs:
tag:
required: true
type: string
pull_request:
types:
- opened
- synchronize
- reopened
run-name: "Image Build for tag ${{ github.ref_name }}"
permissions:
packages: write
contents: write
pull-requests: write
issues: write
jobs:
build-and-push:
runs-on: ubuntu-22.04
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Create safe tag for image
id: safe_tag
run: |
if [ "${{ inputs.tag }}" != "" ]; then
echo "Tag input exists: ${{ inputs.tag }}"
echo "SAFE_TAG=$(echo ${{ inputs.tag }} | sed 's/[^a-zA-Z0-9.]/-/g')" >> $GITHUB_OUTPUT
else
if [ "${{ github.event_name }}" == "pull_request" ] && [ "${{ github.event.pull_request.head.ref }}" != "main" ]; then
echo "SAFE_TAG=$(echo ${{ github.event.pull_request.head.ref }} | sed 's/[^a-zA-Z0-9.]/-/g')-${{ github.run_id }}-${{ github.run_number }}" >> $GITHUB_OUTPUT
else
echo "SAFE_TAG=$(echo ${{ inputs.tag }} | sed 's/[^a-zA-Z0-9.]/-/g')" >> $GITHUB_OUTPUT
fi
fi
- name: Set up Docker Buildx
id: setup_buildx
uses: docker/setup-buildx-action@v3
- name: Log into ghcr
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push to ghcr
id: build_publish
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64, linux/arm64
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ghcr.io/ministryofjustice/hmpps-ldap-automation:${{ steps.safe_tag.outputs.SAFE_TAG }}
- name: Comment image tag on PR
env:
GH_TOKEN: ${{ github.token }}
if: github.event_name == 'pull_request'
run: |
gh pr comment ${{ github.event.pull_request.number }} --body 'Image built and pushed to `ghcr.io/ministryofjustice/hmpps-ldap-automation:${{ steps.safe_tag.outputs.SAFE_TAG }}`'
- name: Slack failure notification
if: ${{ failure() }}
uses: slackapi/slack-github-action@70cd7be8e40a46e8b0eced40b0de447bdb42f68e # v1.26.0
with:
payload: |
{"blocks":[{"type": "section","text": {"type": "mrkdwn","text": ":no_entry: Failed GitHub Action:"}},{"type": "section","fields":[{"type": "mrkdwn","text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }}>"},{"type": "mrkdwn","text": "*Job:*\n${{ github.job }}"},{"type": "mrkdwn","text": "*Repo:*\n${{ github.repository }}"}]}]}
env:
SLACK_WEBHOOK_URL: ${{ secrets.PWO_PUBLIC_SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK