Merge pull request #5 from spring-media/add-github-action #53
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: Docker | |
on: | |
push: | |
branches: | |
- main | |
env: | |
IMAGE_NAME: universe-2020-demo | |
jobs: | |
push: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Build image | |
run: docker build . --file Dockerfile --tag $IMAGE_NAME | |
- name: Log into registry | |
run: echo "${{ secrets.DEMO_GITHUB_TOKEN }}" | docker login ghcr.io -u dfassbender --password-stdin | |
- name: Push image | |
run: | | |
IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME | |
# Change all uppercase to lowercase | |
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') | |
# Strip git ref prefix from version | |
VERSION=${GITHUB_SHA::7} | |
echo IMAGE_ID=$IMAGE_ID | |
echo VERSION=$VERSION | |
docker tag $IMAGE_NAME $IMAGE_ID:main-$VERSION | |
docker push $IMAGE_ID:main-$VERSION | |
- name: Create deployment | |
run: | | |
# create a deployment associated with the created docker-image | |
SHORT_SHA=${GITHUB_SHA::7} | |
LABEL=$(echo ${{github.event.ref}} | sed 's/refs\/heads\///g' | tr '[:upper:]' '[:lower:]' | awk -F "-" '{print $1}') | |
PAYLOAD="{\"ref\":\"$SHORT_SHA\",\"environment\":\"$LABEL.prd\",\"label\":\"$LABEL\",\"project\":\"${{ env.project }}\",\"description\":\"Deploy commit $SHORT_SHA\",\"auto_merge\":false,\"required_contexts\":[]}" | |
echo $(echo $PAYLOAD) | |
DEPLOYMENT_ID=$(curl -s -X POST -d "$PAYLOAD" -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -H "Accept:application/vnd.github.ant-man-preview+json" https://api.github.com/repos/spring-media/github-demo/deployments | jq '.id') | |
# set state of the deployment to "queued" | |
DEPLOYMENT_PAYLOAD="{\"environment\": \"$LABEL.prd\",\"state\": \"queued\",\"description\": \"Deployment created\"}" | |
echo $(echo $DEPLOYMENT_PAYLOAD) | |
curl -s -X POST -d "$DEPLOYMENT_PAYLOAD" -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -H "Accept:application/vnd.github.flash-preview+json" https://api.github.com/repos/spring-media/github-demo/deployments/"$DEPLOYMENT_ID"/statuses |