Skip to content

Commit 36ab232

Browse files
author
Tuomas Kaittola
committed
Add extract-metadata github action
1 parent db39cfc commit 36ab232

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test extract-metadata action
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches: [main]
7+
8+
jobs:
9+
test-extract-metadata:
10+
name: Retrieves metadata
11+
runs-on: ubuntu-20.04
12+
13+
steps:
14+
- name: Check that environment is empty
15+
run: '[[ -z "$IMAGE_NAME" && -z "$COMMIT_ID" && -z "$BRANCH_NAME" ]]'
16+
17+
- name: Call action to initialize environment
18+
uses: HSLdevcom/jore4-tools/github-actions/extract-metadata@extract-metadata-v1
19+
20+
- name: Environment variables are set after action is called
21+
run: '[[ -n "$IMAGE_NAME" && -n "$COMMIT_ID" && -n "$BRANCH_NAME" ]]'
22+
23+
- name: Check that IMAGE_NAME environment variable is created and is valid
24+
run: '[[ "$IMAGE_NAME" == "hsldevcom/jore4-tools" ]]'
25+
26+
# (tests for BRANCH_NAME and COMMIT_ID omitted for now as they might change on each run so its a little bit tricky to test those)
27+

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Tools which are commonly used by other JORE4 projects
99
- [Tools for Docker](#tools-for-docker)
1010
- [read-secrets.sh](#read-secretssh)
1111
- [Github Actions](#github-actions)
12-
- [Healthcheck](#healthcheck)
12+
- [extract-metadata](#extract-metadata)
13+
- [healthcheck](#healthcheck)
1314

1415
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1516

@@ -59,7 +60,23 @@ CMD /bin/bash -c "source /tmp/read-secrets.sh && java -jar /.../xxx.jar"
5960

6061
## Github Actions
6162

62-
### Healthcheck
63+
### extract-metadata
64+
65+
Extracts Github repository metadata in Docker Hub friendly format.
66+
67+
Example usage:
68+
69+
```
70+
steps:
71+
- uses: HSLdevcom/jore4-tools/github-actions/extract-metadata@extract-metadata-v1
72+
```
73+
74+
After calling this action defines following environment variables that are available in later steps:
75+
`BRANCH_NAME`: branch name, e.g. `main`. Should work also in pull requests unlike `GITHUB_HEAD_REF` that Github automatically provides.
76+
`IMAGE_NAME`: repository name in lowercase format, including organization. E.g. `hsldevcom/jore4-tools`. Can be used as docker image name.
77+
`COMMIT_ID`: commit details in format `<branch name>--<git commit hash>`. Can be used when tagging docker images.
78+
79+
### healthcheck
6380

6481
Runs a user-defined script to check whether a service is up and running
6582

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: "Extract git metadata"
2+
description:
3+
"Extracts repository metadata to environment variables. Useful e.g. when tagging docker images according to jore4 project conventions."
4+
5+
runs:
6+
using: "composite"
7+
steps:
8+
- name: Extract branch name
9+
id: branch-name
10+
run: |
11+
# In pull requests
12+
BRANCH_NAME="${GITHUB_HEAD_REF}"
13+
[[ ${BRANCH_NAME} = "" ]] && {
14+
# In branch pushes
15+
BRANCH_NAME="$(echo "${GITHUB_REF}" | cut -d '/' -f 3-)"
16+
}
17+
echo "BRANCH_NAME=${BRANCH_NAME}" >> "${GITHUB_ENV}"
18+
shell: bash
19+
20+
- name: Extract docker image name
21+
id: image-name
22+
run: |
23+
# GITHUB_REPOSITORY env variable is provided by GitHub and it
24+
# contains owner and repository name, e.g. `HSLdevcom/jore4-tools`.
25+
# Jore4 project convention is that docker images are named accordingly.
26+
#
27+
# Docker Hub uses lowercase so use a bashism to convert.
28+
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
29+
shell: bash
30+
31+
- name: Extract commit id
32+
id: commit-id
33+
run: |
34+
# Docker tags can use only some special characters.
35+
echo "COMMIT_ID=$(echo "${BRANCH_NAME}" | tr -C '0-9a-zA-Z._' '-')-${GITHUB_SHA}" >> $GITHUB_ENV
36+
shell: bash

0 commit comments

Comments
 (0)