datacite/volpino

View on GitHub
.github/workflows/release.yml

Summary

Maintainability
Test Coverage
name: Release
on:
  release:
    types: [published]
jobs:
  test:
    uses: ./.github/workflows/ci.yml
    secrets: inherit
  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-
      - name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Get git tag
        run: |
          echo "::set-output name=GIT_TAG::$(git tag --points-at HEAD)"
        id: set_git_vars
      - name: Build and push 
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ github.repository }}:${{ steps.set_git_vars.outputs.GIT_TAG }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
  
  deploy:
    needs: [test, build]
    runs-on: ubuntu-latest
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Extract variables
        shell: bash
        run: |
          echo "::set-output name=BRANCH::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
          echo "::set-output name=TAG::$(git tag --points-at HEAD)"
          echo "::set-output name=GIT_SHA::$(git rev-parse HEAD)"
          echo "::set-output name=GIT_SHA_SHORT::$(git rev-parse --short HEAD)"  
        id: extract_variables

      - name: Checkout terraform config repo
        uses: actions/checkout@v2
        with:
          # public repo with terraform configuration
          repository: 'datacite/mastino'
          persist-credentials: false
      - name: Commit changes to terraform config repository
        # use go template in terraform config repository to update git sha and tag
        # commit and push changes to trigger terraform workflow
        run: |
          export GIT_SHA=${{ steps.extract_variables.outputs.GIT_SHA_SHORT }}
          export GIT_TAG=${{ steps.extract_variables.outputs.TAG }}
          wget https://github.com/jwilder/dockerize/releases/download/v0.6.0/dockerize-linux-amd64-v0.6.0.tar.gz
          tar -xzvf dockerize-linux-amd64-v0.6.0.tar.gz
          rm dockerize-linux-amd64-v0.6.0.tar.gz
          ./dockerize -template prod-eu-west/services/profiles/_volpino.auto.tfvars.tmpl:prod-eu-west/services/profiles/_volpino.auto.tfvars
          
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add prod-eu-west/services/profiles/_volpino.auto.tfvars
          git commit -m "Adding volpino git variables for tag ${{ steps.extract_variables.outputs.GIT_TAG }}"
      - name: Push changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          repository: 'datacite/mastino'
          branch: 'refs/heads/master'
          tags: false

      - name: Notify Slack
        uses: edge/simple-slack-notify@master
        with:
          channel: '#ops'
          color: 'good'
          text: 'Version <https://github.com/${{ github.repository }}/releases/tag/${{ steps.extract_variables.outputs.TAG }}|${{ steps.extract_variables.outputs.TAG }}> of the <https://profiles.datacite.org|Profiles Service> is being released to production.'
          failure_text: '${env.GITHUB_WORKFLOW} (${env.GITHUB_RUN_NUMBER}) build failed'
          fields: |
            [{ "title": "Committed by", "value": "<https://github.com/${{ github.repository }}/commits?author=${{ github.actor }}|${{ github.actor }}>", "short": true },
             { "title": "Commit SHA", "value": "<https://github.com/${{ github.repository }}/commit/${{ steps.extract_variables.outputs.GIT_SHA }}|${{ steps.extract_variables.outputs.GIT_SHA_SHORT }}>", "short": true },
             { "title": "Repository", "value": "<https://github.com/${{ github.repository }}|${{ github.repository }}>", "short": true },
             { "title": "Release", "value": "<https://github.com/${{ github.repository }}/releases/tag/${{ steps.extract_variables.outputs.TAG }}|${{ steps.extract_variables.outputs.TAG }}>", "short": true }]