|
| 1 | +# Code Courtesy : Mathias Ricken |
| 2 | +# This workflow triggers when a new release has been made. |
| 3 | + |
| 4 | +name: ReleasePublished |
| 5 | + |
| 6 | +# Controls when the action will run. |
| 7 | +on: |
| 8 | + # Triggers the workflow on push or pull request events but only for the main branch |
| 9 | + release: |
| 10 | + types: [published] |
| 11 | + |
| 12 | + # Allows you to run this workflow manually from the Actions tab. |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + release: |
| 16 | + description: 'Release version number ("latest" or "v1.34.0")' |
| 17 | + required: true |
| 18 | + default: latest |
| 19 | + |
| 20 | +# A workflow run is made up of one or more jobs that can run sequentially or in parallel |
| 21 | +jobs: |
| 22 | + # This workflow contains a single job called "build" |
| 23 | + build: |
| 24 | + # NOTE: If this runs in an environment, set this value (where "MC-Action" is the environment name) |
| 25 | + # environment: MC-Action |
| 26 | + |
| 27 | + # The type of runner that the job will run on |
| 28 | + runs-on: ubuntu-latest |
| 29 | + |
| 30 | + # Steps represent a sequence of tasks that will be executed as part of the job |
| 31 | + steps: |
| 32 | + - name: Report the parameters |
| 33 | + run: | |
| 34 | + echo "GITHUB_REF is \"$GITHUB_REF\"" |
| 35 | + echo "GITHUB_SHA is \"$GITHUB_SHA\"" |
| 36 | + echo "inputs.release is \"${{github.event.inputs.release}}\"" |
| 37 | + if [ -z "${{github.event.inputs.release}}" ]; then |
| 38 | + echo "Release not set manually, using GITHUB_REF" |
| 39 | + export RELEASE=`echo "$GITHUB_REF" | sed "s#refs/tags/##"` |
| 40 | + else |
| 41 | + echo "Release set manually, using inputs.release" |
| 42 | + export RELEASE=${{github.event.inputs.release}} |
| 43 | + fi |
| 44 | + echo "Release is $RELEASE" |
| 45 | + pat="^v.*" |
| 46 | + if [ "$RELEASE" = "latest" ]; then |
| 47 | + echo "Using latest release" |
| 48 | + elif [[ $RELEASE =~ $pat ]]; then |
| 49 | + echo "Specified '$RELEASE' as release" |
| 50 | + else |
| 51 | + echo "Unsupported release, should be 'latest' or something like 'v1.35.0'; was '$RELEASE'" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + echo "RELEASE=$RELEASE" >> $GITHUB_ENV |
| 55 | + - name: Check credentials |
| 56 | + run: | |
| 57 | + set -e |
| 58 | + echo "Checking NPM ACCESS_TOKEN" |
| 59 | + curl --fail -H 'Authorization: Bearer ${{ secrets.NPM_ACCESS_TOKEN }}' https://registry.npmjs.org/ |
| 60 | + echo "Checking GitHub ACCESS_TOKEN" |
| 61 | + curl -f -H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" -H 'Accept: application/vnd.github.v3.raw' -s https://api.github.com/repos/oracle/oci-typescript-sdk > /dev/null |
| 62 | + - name: Install packages |
| 63 | + run: | |
| 64 | + sudo apt-get install -y jq |
| 65 | + shell: bash |
| 66 | + - name: Download the npm artifacts asset |
| 67 | + env: |
| 68 | + ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} |
| 69 | + run: | |
| 70 | + if [ "$RELEASE" = "latest" ]; then |
| 71 | + echo "Using latest release" |
| 72 | + set +e |
| 73 | + curl -f -H "Authorization: Bearer $ACCESS_TOKEN" -H 'Accept: application/vnd.github.v3.raw' -s https://api.github.com/repos/oracle/oci-typescript-sdk/releases > releases.json |
| 74 | + result="$?" |
| 75 | + if [ "$result" -ne 0 ]; then |
| 76 | + echo "curl returned '$result': Bad access token" |
| 77 | + exit $result |
| 78 | + fi |
| 79 | + asset_id=`cat releases.json | jq ".[0].assets | map(select(.name|test(\"npm_artifacts.zip\")))[0].id"` |
| 80 | + result="$?" |
| 81 | + if [ "$result" -ne 0 ]; then |
| 82 | + echo "jq returned '$result': No releases or bad file (was 'npm_artifacts.zip')" |
| 83 | + cat releases.json |
| 84 | + exit $result |
| 85 | + fi |
| 86 | + if [ "$asset_id" == "null" ]; then |
| 87 | + echo "jq returned asset id '$asset_id': No releases or bad file (was 'npm_artifacts.zip')" |
| 88 | + cat releases.json |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + set -e |
| 92 | + else |
| 93 | + echo "Using release $RELEASE" |
| 94 | + set +e |
| 95 | + curl -f -H "Authorization: Bearer $ACCESS_TOKEN" -H 'Accept: application/vnd.github.v3.raw' -s https://api.github.com/repos/oracle/oci-typescript-sdk/releases > releases.json |
| 96 | + result="$?" |
| 97 | + if [ "$result" -ne 0 ]; then |
| 98 | + echo "curl returned '$result': Bad access token" |
| 99 | + exit $result |
| 100 | + fi |
| 101 | + asset_id=`cat releases.json | jq ". | map(select(.tag_name == \"$RELEASE\"))[0].assets | map(select(.name|test(\"npm_artifacts.zip\")))[0].id"` |
| 102 | + result="$?" |
| 103 | + if [ "$result" -ne 0 ]; then |
| 104 | + echo "jq returned '$result': Bad release (was '$RELEASE') or bad file (was 'npm_artifacts.zip')" |
| 105 | + cat releases.json |
| 106 | + exit $result |
| 107 | + fi |
| 108 | + if [ "$asset_id" == "null" ]; then |
| 109 | + echo "jq returned asset id '$asset_id': Bad release (was '$RELEASE') or bad file (was 'npm_artifacts.zip')" |
| 110 | + cat releases.json |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + set -e |
| 114 | + fi |
| 115 | + echo "Asset id for release $RELEASE file npm_artifacts.zip is $asset_id" |
| 116 | + wget -q --header="Authorization: Bearer $ACCESS_TOKEN" --auth-no-challenge --header='Accept:application/octet-stream' https://api.github.com/repos/oracle/oci-typescript-sdk/releases/assets/$asset_id -O npm_artifacts.zip |
| 117 | + - name: Examine npm artifact files |
| 118 | + run: | |
| 119 | + ls |
| 120 | + unzip -q npm_artifacts.zip |
| 121 | + cd npm_artifacts |
| 122 | + ls |
| 123 | + cd .. |
| 124 | + - uses: actions/checkout@v2 |
| 125 | + - uses: actions/setup-node@v2 |
| 126 | + with: |
| 127 | + node-version: "12.x" |
| 128 | + registry-url: "https://registry.npmjs.org/" |
| 129 | + - name: Publish tarball artifacts to npm |
| 130 | + env: |
| 131 | + NODE_AUTH_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}} |
| 132 | + run: | |
| 133 | + ls |
| 134 | + cd npm_artifacts |
| 135 | + ls |
| 136 | + find . -exec npm publish {} \; |
0 commit comments