Reflecting Heroku push version within the app

后端 未结 9 625
故里飘歌
故里飘歌 2021-01-30 09:17

Every time I push my app to heroku I see the line

-----> Launching... done, v43

Is there a way to make that version number apear within the

9条回答
  •  失恋的感觉
    2021-01-30 09:57

    In Node.js using JavaScript fetch (and the forthcoming async/await), you can do it with the following code (no push hooks!):

    const fetch = require('node-fetch');
    const url = 'https://api.heroku.com/apps/myapp/releases';
    const headers = {
        Accept:        'application/vnd.heroku+json; version=3',
        Authorization: 'Basic '+base64Encode(':'+process.env.HEROKU_API_TOKEN)
    };
    const response = await fetch(url, { headers });
    const releases = await response.json();
    const lastRelease = releases[releases.length-1];
    const version = lastRelease.version;
    const created = lastRelease.created_at;
    

    using

    function base64Encode(str) {
        return new Buffer(str, 'binary').toString('base64');
    }
    

    Note this requires

    $ heroku config:set HEROKU_API_TOKEN=\`heroku auth:token`.
    

    See devcenter.heroku.com/articles/platform-api-reference#release-list.

提交回复
热议问题