Merge pull request #33 from ncsa/main #93
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: smile-graphql | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
- 'release/*' | |
tags: | |
- '*' | |
pull_request: | |
branches: | |
- main | |
- develop | |
- 'release/*' | |
# Certain actions will only run when this is the main repo. | |
env: | |
MAIN_REPO: ncsa/standalone-smm-smile-graphql | |
DOCKERHUB_ORG: socialmediamacroscope | |
NCSAHUB: hub.ncsa.illinois.edu/socialmediamacroscope | |
PLATFORM: "linux/amd64" | |
jobs: | |
# ---------------------------------------------------------------------- | |
# DOCKER BUILD | |
# ---------------------------------------------------------------------- | |
docker: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
steps: | |
# checkout source code | |
- uses: actions/checkout@v3 | |
# calculate some variables that are used later | |
- name: version information | |
run: | | |
# find out what the BRANCH is, in case of a PR we will use the PR-<number> | |
if [ "${{ github.event.release.target_commitish }}" != "" ]; then | |
BRANCH="${{ github.event.release.target_commitish }}" | |
elif [[ $GITHUB_REF =~ pull ]]; then | |
BRANCH="$(echo $GITHUB_REF | sed 's#refs/pull/\([0-9]*\)/merge#PR-\1#')" | |
else | |
BRANCH=${GITHUB_REF##*/} | |
fi | |
OWNER=${{ github.repository_owner }} | |
OWNER_LC=${OWNER,,} | |
# calculate the version and all tags | |
if [ "$BRANCH" == "main" ]; then | |
VERSION=$(grep '"version"' package.json | sed 's/.*"\([0-9\.]*\)", *$/\1/') | |
tags="latest" | |
oldversion="" | |
tmpversion="${VERSION}" | |
while [ "${oldversion}" != "${tmpversion}" ]; do | |
oldversion="${tmpversion}" | |
tags="${tags} ${tmpversion}" | |
tmpversion=${tmpversion%.*} | |
done | |
else | |
VERSION="$BRANCH" | |
tags="$BRANCH" | |
fi | |
echo "GITHUB_BRANCH=${BRANCH}" >> $GITHUB_ENV | |
# should we push to dockerhub, and is there a README | |
DOCKERHUB_PUSH="false" | |
DOCKERHUB_README="false" | |
if [ "${{ github.repository }}" == "${{ env.MAIN_REPO }}" ]; then | |
if [ ${BRANCH} == "main" ]; then | |
if [ "${{ secrets.DOCKERHUB_USERNAME }}" != "" -a "${{ secrets.DOCKERHUB_PASSWORD }}" != "" ]; then | |
DOCKERHUB_PUSH="true" | |
if [ -e "README.md" ]; then | |
DOCKERHUB_README="true" | |
fi | |
fi | |
fi | |
fi | |
# should we push to Github | |
GITHUB_PUSH="false" | |
if [ "${{ github.repository }}" == "${{ env.MAIN_REPO }}" ]; then | |
GITHUB_PUSH="true" | |
fi | |
# should we push to NCSA | |
NCSAHUB_PUSH="false" | |
if [ "${{ github.repository }}" == "${{ env.MAIN_REPO }}" ]; then | |
if [ "${{ secrets.HUB_USERNAME }}" != "" -a "${{ secrets.HUB_PASSWORD }}" != "" ]; then | |
NCSAHUB_PUSH="true" | |
fi | |
fi | |
# create a list of all images to be pushed | |
IMAGE="smile_graphql" | |
IMAGES="" | |
for tag in ${tags}; do | |
if [ "$DOCKERHUB_PUSH" == "true" ]; then | |
IMAGES="${IMAGES}${{ env.DOCKERHUB_ORG }}/${IMAGE}:${tag}," | |
fi | |
if [ "$NCSAHUB_PUSH" == "true" ]; then | |
IMAGES="${IMAGES}${NCSAHUB}/${IMAGE}:${tag}," | |
fi | |
if [ "$GITHUB_PUSH" == "true" ]; then | |
IMAGES="${IMAGES}ghcr.io/${OWNER_LC}/${IMAGE}:${tag}," | |
fi | |
done | |
IMAGES="${IMAGES%,*}" | |
# save the results in env | |
echo "BRANCH=${BRANCH}" | |
echo "VERSION=${VERSION}" | |
echo "DOCKERHUB_README=${DOCKERHUB_README}" | |
echo "DOCKERHUB_PUSH=${DOCKERHUB_PUSH}" | |
echo "NCSAHUB_PUSH=${NCSAHUB_PUSH}" | |
echo "GITHUB_PUSH=${GITHUB_PUSH}" | |
echo "IMAGES=${IMAGES}" | |
echo "BRANCH=${BRANCH}" >> $GITHUB_ENV | |
echo "VERSION=${VERSION}" >> $GITHUB_ENV | |
echo "DOCKERHUB_README=${DOCKERHUB_README}" >> $GITHUB_ENV | |
echo "DOCKERHUB_PUSH=${DOCKERHUB_PUSH}" >> $GITHUB_ENV | |
echo "GITHUB_PUSH=${GITHUB_PUSH}" >> $GITHUB_ENV | |
echo "NCSAHUB_PUSH=${NCSAHUB_PUSH}" >> $GITHUB_ENV | |
echo "IMAGES=${IMAGES}" >> $GITHUB_ENV | |
# setup docker build | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Inspect Builder | |
run: | | |
echo "Name: ${{ steps.buildx.outputs.name }}" | |
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" | |
echo "Status: ${{ steps.buildx.outputs.status }}" | |
echo "Flags: ${{ steps.buildx.outputs.flags }}" | |
echo "Platforms: ${{ steps.buildx.outputs.platforms }}" | |
# login to registries | |
- name: Login to DockerHub | |
if: env.DOCKERHUB_PUSH == 'true' | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Login to NCSA Hub | |
if: env.NCSAHUB_PUSH == 'true' | |
uses: docker/login-action@v2 | |
with: | |
registry: hub.ncsa.illinois.edu | |
username: ${{ secrets.HUB_USERNAME }} | |
password: ${{ secrets.HUB_PASSWORD }} | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# build the docker images | |
- name: Build and push docker | |
if: env.DOCKERHUB_PUSH == 'true' || env.NCSAHUB_PUSH == 'true' || env.GITHUB_PUSH == 'true' | |
uses: docker/build-push-action@v3 | |
with: | |
context: ./ | |
push: true | |
platforms: ${{ env.PLATFORM }} | |
cache-from: type=gha | |
cache-to: type=gha | |
tags: ${{ env.IMAGES }} | |
build-args: | | |
BRANCH: ${{ env.BRANCH }} | |
VERSION=${{ env.VERSION }} | |
BUILDNUMBER=${{ github.run_number }} | |
GITSHA1=${{ github.sha }} | |
# this will update the README of the dockerhub repo | |
- name: Docker Hub Description | |
if: env.DOCKERHUB_README == 'true' | |
uses: peter-evans/dockerhub-description@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
repository: ${{ env.DOCKERHUB_ORG }}/${{ github.event.repository.name }} | |
readme-filepath: README.md |