Move image to Github Container Registry #40
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: Publish release Docker image | |
on: | |
release: | |
types: | |
- created | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
test-build-push-image: | |
name: 'Run tests, build artifacts and push image' | |
runs-on: ubuntu-latest | |
services: | |
mongo: | |
image: mongo:4.4-bionic | |
env: | |
TZ: "Asia/Shanghai" | |
ports: | |
- 27017:27017 | |
steps: | |
- name: Setup timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v2 | |
- name: Setup Java JDK | |
uses: actions/setup-java@v2 | |
with: | |
distribution: 'adopt' | |
java-package: 'jdk' | |
java-version: '11' | |
check-latest: true | |
- name: Cache Gradle packages | |
uses: actions/cache@v2 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Run backend tests | |
uses: ./.github/actions/backend_test | |
with: | |
working-dir: ./backend | |
- name: Setup Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: '14' | |
- name: Cache Node.js packages | |
uses: actions/cache@v2 | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Run frontend tests | |
uses: ./.github/actions/frontend_test | |
with: | |
working-dir: ./frontend | |
- name: Collect artifacts | |
run: | | |
ARTIFACT_DIR="./ci/artifacts" | |
mkdir -p $ARTIFACT_DIR/backend | |
mkdir $ARTIFACT_DIR/frontend | |
cp -rp ./frontend/dist $ARTIFACT_DIR/frontend/ | |
cp -p ./backend/build/libs/metrik-backend-*.jar $ARTIFACT_DIR/backend/ | |
cp -p ./backend/run.sh $ARTIFACT_DIR/backend/ | |
- name: Log in to the Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build Docker meta | |
id: build-docker-meta | |
run: | | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
echo "TAGS=latest,$VERSION" >> $GITHUB_OUTPUT | |
- name: Build and push image | |
run: | | |
IMAGE_BASE=${{ env.REGISTRY }}/${{env.IMAGE_NAME}} | |
IFS="," read -ra TAGS <<< ${{ steps.build-docker-meta.outputs.TAGS }} | |
docker build . --file Dockerfile --tag $IMAGE_BASE:${TAGS[0]} --label "runnumber=${GITHUB_RUN_ID}" --label "hash=${GITHUB_SHA}" | |
for TAG in ${TAGS[@]}; do | |
if [ "${TAG}" != "${TAGS[0]}" ]; then | |
docker tag "${IMAGE_BASE}:${TAGS[0]}" "${IMAGE_BASE}:${TAG}" | |
fi | |
docker push "${IMAGE_BASE}:${TAG}" | |
done | |
working-directory: ./ci |