-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-docker-dispatch.yml
More file actions
146 lines (138 loc) · 6.41 KB
/
Copy pathbuild-docker-dispatch.yml
File metadata and controls
146 lines (138 loc) · 6.41 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: build-docker-dispatch
on:
repository_dispatch:
types: [trigger-docker-build]
workflow_dispatch:
inputs:
owner_repo:
description: "Name of the repository containing the dockerfile source (OWNER/REPO)"
required: true
ref:
description: "The git ref (branch, tag, or SHA) of the source repo to checkout"
required: true
default: main
dockerfile_path:
description: "Path to the Dockerfile in the source repository (e.g.
'path/to/Dockerfile')"
required: true
default: Dockerfile
docker_image_name:
description: "Name of the docker image to build (e.g. 'bwa', 'mosuite', 'spac')"
required: true
docker_image_tag:
description: "Tag to apply to the built docker image (e.g. 'v0.3.0')"
required: true
permissions:
contents: read
pull-requests: read
jobs:
build-docker:
runs-on: ubuntu-latest
steps:
- name: process payload
id: process-payload
run: |
if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
owner_repo="${{ github.event.client_payload.owner_repo }}"
ref="${{ github.event.client_payload.ref }}"
dockerfile_path="${{ github.event.client_payload.dockerfile_path }}"
docker_image_name="${{ github.event.client_payload.docker_image_name }}"
docker_image_tag="${{ github.event.client_payload.docker_image_tag }}"
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
owner_repo="${{ github.event.inputs.owner_repo }}"
ref="${{ github.event.inputs.ref }}"
dockerfile_path="${{ github.event.inputs.dockerfile_path }}"
docker_image_name="${{ github.event.inputs.docker_image_name }}"
docker_image_tag="${{ github.event.inputs.docker_image_tag }}"
else
echo "Unsupported event: ${GITHUB_EVENT_NAME}"
exit 1
fi
repo_name=$(echo "$owner_repo" | cut -d'/' -f2)
context_dir="${repo_name}/${docker_image_name}"
new_branch="update-${docker_image_name}-${docker_image_tag}"
echo "owner_repo=$owner_repo" >> $GITHUB_OUTPUT
echo "ref=$ref" >> $GITHUB_OUTPUT
echo "repo_name=$repo_name" >> $GITHUB_OUTPUT
echo "src_dockerfile_path=$dockerfile_path" >> $GITHUB_OUTPUT
echo "new_dockerfile_path=$context_dir/Dockerfile.$docker_image_tag" >> $GITHUB_OUTPUT
echo "docker_image_name=$docker_image_name" >> $GITHUB_OUTPUT
echo "docker_image_tag=$docker_image_tag" >> $GITHUB_OUTPUT
echo "context_dir=$context_dir" >> $GITHUB_OUTPUT
echo "new_branch=$new_branch" >> $GITHUB_OUTPUT
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.CCBR_BOT_APP_ID }}
private-key: ${{ secrets.CCBR_BOT_PRIVATE_KEY }}
owner: CCBR
- uses: actions/checkout@v6
name: Checkout Dockers2
with:
persist-credentials: true
token: ${{ steps.generate-token.outputs.token }}
- uses: actions/checkout@v6
name: Checkout source repository
with:
fetch-depth: 0
repository: ${{ steps.process-payload.outputs.owner_repo }}
ref: ${{ steps.process-payload.outputs.ref }}
path: dispatch-repo
- name: Commit new Dockerfile
env:
CONTEXT_DIR: ${{ steps.process-payload.outputs.context_dir }}
DOCKER_IMAGE_NAME: ${{ steps.process-payload.outputs.docker_image_name }}
DOCKER_IMAGE_TAG: ${{ steps.process-payload.outputs.docker_image_tag }}
NEW_DOCKERFILE_PATH: "${{ steps.process-payload.outputs.new_dockerfile_path }}"
SRC_DOCKERFILE_PATH: "${{ steps.process-payload.outputs.src_dockerfile_path }}"
run: |
git config --global user.name 'CCBR-bot[bot]'
git config --global user.email '258092125+CCBR-bot[bot]@users.noreply.github.com'
original_branch=$(git branch --show-current)
branch_name="${{ steps.process-payload.outputs.new_branch }}"
# fetch the remote branch if it exists
if git ls-remote --exit-code --heads origin "$branch_name" >/dev/null 2>&1; then
git fetch origin "$branch_name:$branch_name"
fi
# switch to the branch (or create it if it doesn't exist)
if git show-ref --verify --quiet "refs/heads/$branch_name"; then
git switch "$branch_name"
else
git switch -c "$branch_name"
fi
# Copy context dir
mkdir -p "${{ env.CONTEXT_DIR }}"
rsync -a --exclude=".git/" dispatch-repo/ "${{ env.CONTEXT_DIR }}/"
cp "dispatch-repo/${{ env.SRC_DOCKERFILE_PATH }}" "${{ env.NEW_DOCKERFILE_PATH }}"
# Only commit new dockerfile, not entire context dir
git add ${{ env.NEW_DOCKERFILE_PATH }}
if git diff --cached --quiet; then
echo "nothing to commit"
git switch "$original_branch"
else
git commit -m "feat: add Dockerfile for ${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG }}"
git push -u origin "${{ steps.process-payload.outputs.new_branch }}"
fi
- uses: CCBR/actions/build-docker@latest
id: build-docker
with:
dockerfile: ${{ steps.process-payload.outputs.new_dockerfile_path }}
dockerhub-namespace: nciccbr
dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME_VK }}
dockerhub-token: ${{ secrets.DOCKERHUBRW_TOKEN_VK }}
github-token: ${{ steps.generate-token.outputs.token }}
push: true
- name: Open PR w/ new Dockerfile
if: steps.build-docker.outputs.push_success == 'true'
env:
DOCKER_IMAGE_NAME: ${{ steps.process-payload.outputs.docker_image_name }}
DOCKER_IMAGE_TAG: ${{ steps.process-payload.outputs.docker_image_tag }}
DOCKERFILE_PATH: ${{ steps.process-payload.outputs.new_dockerfile_path }}
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
gh pr create \
--title "feat: add/update Dockerfile for ${{ env.DOCKER_IMAGE_NAME }}:${{ env.DOCKER_IMAGE_TAG }}" \
--body "Adds a Dockerfile located at `${{ env.DOCKERFILE_PATH }}`." \
--reviewer @CCBR/AdminTeam \
|| echo "PR unsuccessful"